TensorFlow学习之placeholder

TensorFlow学习之placeholder

源码

def placeholder(dtype, shape=None, name=None):
“”"Inserts a placeholder for a tensor that will be always fed.
为将总是要被喂入的张量插入一个占位符

Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument to Session.run(), Tensor.eval(), or Operation.run().
如果计算这个张量会产生误差,它必须要通过可选参数 feed_dict喂入到Session.run(), Tensor.eval(), or Operation.run().

For example:

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.

@compatibility(eager)
Placeholders are not compatible with eager execution.
Placeholders 与eager execution不兼容。
@end_compatibility

Args:
dtype: The type of elements in the tensor to be fed.
dtype:张量的元素类型
shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.
shape:张量的形状(可选)。没有指定的情况下,可以喂入任何形状的张量。
name: A name for the operation (optional).
name:操作的名字(可选)

Returns:
A Tensor that may be used as a handle for feeding a value, but not evaluated directly.
一个“张量”,它可以用作一个句柄来输入一个值,但不直接计算。

Raises:
RuntimeError: if eager execution is enabled
运行时错误:如果eager execution启用了
“”"
if context.executing_eagerly():
raise RuntimeError("tf.placeholder() is not compatible with "
“eager execution.”)

return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)

理解分析

  • TensorFlow中的占位符,用于传入外部数据。
  • 相当于定义了一个位置,这个位置中的数据在程序运行时再指定
  • 不需要大龄常量来提供输入数据,而只需要将数据通过placeholder传入。
  • 类型确定,维度不一定要给出

你可能感兴趣的:(TensorFlow笔记)