tensorflow学习笔记(2)-feed_dict

来源:互联网 发布:比价软件哪个好 编辑:程序博客网 时间:2024/05/23 00:10
x = tf.placeholder(tf.float32, shape=(1024, 1024))  y = tf.matmul(x, x)  with tf.Session() as sess:    print(sess.run(y))  # ERROR: 此处x还没有赋值.    rand_array = np.random.rand(1024, 1024)    print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed. 
import tensorflow as tfw1=tf.Variable(tf.random_normal([1,2],stddev=1,seed=1))  #因为需要重复输入x,而每建一个x就会生成一个结点,计算图的效率会低。所以使用占位符  x=tf.placeholder(tf.float32,shape=(2,2))  x1=tf.constant([[0.7,0.9]])  a=x+w1  b=x1+w1  sess=tf.Session()  sess.run(tf.global_variables_initializer())  #运行y时将占位符填上,feed_dict为字典,变量名不可变  y_1=sess.run(a,feed_dict={x:[[0.7,0.9],[1,1]]})  y_2=sess.run(b)  print(y_1)  print(y_2)  sess.close 
原创粉丝点击