版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_28729797/article/details/79013809
必导入的2个包
import tensorflow as tf
import numpy as np
#情形1 矩阵运算
#构建3*4的0矩阵
matrix_1 = np.zeros((3,4))
x=tf.constant([[2,2],[2,1]],name='a')
y=tf.constant([[0,1],[3,5]],name='b')
with tf.Session() assess:
#点乘运算
w=tf.multiply(x,y)
#矩阵运算
z = tf.matmul(x,y)
#构建List数组,赋值给a,b
a,b=sess.run([w,x])
print(a)
print(b)
print(matrix_1)
#情形2
a=tf.add(2,5)
b=tf.multiply(a,3)
with tf.Session() as sess:
#feed不同的值
replace_dict={a:15}
r =sess.run(b,feed_dict=replace_dict)
print(r)
#上面这么写等价于上方
# print(sess.run(c,{a:15}))
#情形3,赋值
a=tf.placeholder(tf.float32,shape=[3]) b=tf.constant([5,5,5],tf.float32) c=a+b with tf.Session() as sess: fd = {a: [1, 2, 3]} print(sess.run(c,feed_dict=fd))