阅读(665) (1)

TensorFlow占位符:tf.placeholder

2017-11-09 11:25:20 更新
tf.placeholder 函数
placeholder(
    dtype,
    shape=None,
    name=None
)

定义在:tensorflow/python/ops/array_ops.py

请参阅指南:输入和读取器>占位符

插入一个张量的占位符,这个张量将一直被提供.

注意:如果计算,该张量将产生一个错误,其值必须使用 feed_dict 可选参数来进行 session . run()、Tensor.eval() 或 oper.run().

例如:

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.

参数:

  • dtype:要输入的张量中元素的类型.
  • shape:要输入的张量的形状(可选).如果未指定形状,则可以输入任何形状的张量.
  • name:操作的名称(可选).

返回:

一个可能被用作提供一个值的句柄的张量,但不直接计算.