tf.placeholder

tf.placeholder

tf.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().

在构建graph的过程中,tensor是没有实际数据的,只是表达计算过程,那么通过placeholder函数对tensor变量进行占位表示。然后在Session执行过程中,通过Feed_dict对占位的tensor进行Feed

Args:

  • dtype: The type of elements in the tensor to be fed.指定数据类型
  • shape: The shape of the tensor to be fed (optional). If the shape is not specified,you can Feed a tensor of any shape.指定tensor的维度,如果没有指定,可以Feed任意维度的tensor
  • name: A name for the operation (optional).

Returns:

Tensor that may be used as a handle for Feeding a value,but not evaluated directly.

Raises:

  • RuntimeError: if eager execution is enabled
 
 1 import tensorflow as tf
 2 import numpy as np
 3 
 4                                                                 
 5 x = tf.placeholder(tf.float32,shape=(1024,1024))
 6 y = tf.matmul(x,x)
 7 
 8 with tf.Session() as sess:
 9   #print(sess.run(y))  # ERROR: will fail because x was not fed.
10   rand_array = np.random.rand(1024,1024)
11   print(sess.run(y,Feed_dict={x: rand_array}))  # Will succeed.

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...