TensorFlow function finishing - tf.placeholder

Construction method:

tf.placeholder(
    dtype
,
    shape
=None,
    name
=None
)

Function description:

Insert a tensor (Tensor) placeholder to be initialized (it can be understood as opening up a space and assigning a specific value during execution )

Parameter Description:

dtype: The element type of the filled tensor (Tensor).

shape: The shape of the filled Tensor (optional parameter). If you do not specify a tensor (Tensor) shape, you can fill the tensor (Tensor) with any shape.

name: Provide a name for the operation (optional parameter).

return value:

A Tensor. Assignment must be done using a handle, but it cannot be evaluated directly.

Example:

Randomly generate a 1024*1024 matrix and find its square.

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y
= tf.matmul(x, x)

with tf.Session() as sess:
 
print(sess.run(y))  # ERROR: 由于x没有分配具体的数据所以该句将报错,注释掉后运行下面的语句即可

  rand_array
= np.random.rand(1024, 1024)
 
print(sess.run(y, feed_dict={x: rand_array}))  # feed_dict以字典的方式将生成的rand_array填充进x中

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325755021&siteId=291194637