TensorFlow api: placeholder

tf.placeholder():
有三个参数:

dtype, 填充tensor的数据类型
shape=None, 指定填充的tensor的形状(比如1024*1024)
name=None,为此操作命名

函数定义:为tensor创建一个placeholder占位符用于之后填充。
注意:placeholder不能直接赋值求值,应使用feed_dict。

例子:

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  
  # ERROR: will fail because x was not fed.

  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  
  # Will succeed.

猜你喜欢

转载自blog.csdn.net/cyr429/article/details/89249479