首页 > 技术文章 > tf.compat.v1.placeholder

xmd-home 2020-12-19 15:41 原文

这个方法用于声明一个需要被填充的张量;

1 tf.compat.v1.placeholder(
2     dtype, shape=None, name=None
3 )

重点:这个张量如果直接调用的话会产生错误,必须使用feed_dict可选参数将其值提供给Session.run()、Tensor.eval()或operations .run()。

下面举例说明:

1 x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
2 y = tf.matmul(x, x)
3 
4 with tf.compat.v1.Session() as sess:
5   print(sess.run(y))  # ERROR: will fail because x was not fed.
6 
7   rand_array = np.random.rand(1024, 1024)
8   print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

参数:

dtype: 传入的张量因素;

shape:张量的形状;如果你没有指定特征的形状,你可以喂给张量任意的形状;

name:这个运算的名字(可选)

 

推荐阅读