使用方法
官方的解释:(我这里为了清晰直接在代码上进行注释)
>>> import tensorflow as tf
>>> help(tf.placeholder)
#它的功能主要在于制定一个数据,并且在其中填充占位符,指定数据类型之后更加方便内存空间的分配
Help on function placeholder in module tensorflow.python.ops.array_ops:
#可以指定的参数类型:数据类型,数组形状,名称
placeholder(dtype, shape=None, name=None)
#先定义一个张量,后续再对数据进行填充
Inserts a placeholder for a tensor that will be always fed.
#重点:它的值需要用feed_dict功能的参数指定,并且用Session.run(),Tensor.eval(),operation.run()等来运行和赋值
**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()`.
#实例
For example:
```python
#首先给定占位符(tensorflow的图节点,x,y都是节点)
x = tf.placeholder(float, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
#由于没有用feed_dict来进行赋值,因此无法进行运算,会报错
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.
```
#参数说明
Args:
#数据类型(numpy数组中的数据都是同一类型的)
dtype: The type of elements in the tensor to be fed.
#指定形状(利用一个元组的形式,如(10,10))
shape: The shape of the tensor to be fed (optional). If the shape is not
specified, you can feed a tensor of any shape.
#指定名称
name: A name for the operation (optional).
#返回值
Returns:
#返回一个张量的句柄,可以对其进行赋值,但是不能直接进行运算
A `Tensor` that may be used as a handle for feeding a value, but not
evaluated directly.