目的:为将始终输入的张量插入占位符。
tf.compat.v1.placeholder(
dtype, shape=None, name=None
)
关键点:如果评估,该张量将产生错误。 它的值必须使用feed_dict可选参数输入到Session.run(),Tensor.eval()或Operation.run()。
示例程序:
import tensorflow as tf
import numpy as np
# 以下两种shape方法都是可以的[] 和 ()
# x = tf.placeholder(tf.float32, shape=[1024, 1024])
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
参数:
- dtype:张量中要输入的元素的类型。
- shape:要输入的张量的形状(可选)。 如果未指定形状,则可以输入任何形状的张量。
- name:操作的名称(可选)。
返回值:张量可以用作输入值的句柄,但不能直接评估。
提出:
RuntimeError:如果启用了急切执行