1. 程式人生 > >TensorFlow學習——tf.placeholder

TensorFlow學習——tf.placeholder

函式原型:tf.placeholder(dtype, shape=None, name=None)

引數解釋:

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.(引數的shape)
name: A name for the operation (optional).

placeholder,中文意思是佔位符,在tensorflow中類似於函式引數,執行時必須傳入值。

下面複製官方文件中的示例:

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
    print(sess.run(y))  # ERROR: will fail because x was not fed.
    rand_array = np.random.rand(1024, 1024)
    print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

從官方示例來看,placeholder一定要在執行時候feed值。

下面看另一個示例:

x = tf.placeholder(tf.float32, [None, 784])

這個None,代表可以動態放入任意維數.