這個方法用於聲明一個需要被填充的張量;
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:這個運算的名字(可選)