1. 程式人生 > >tensorflow講堂 tf.placeholder()

tensorflow講堂 tf.placeholder()

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

此函式可以理解為形參,用於定義過程,在執行的時候再賦具體的值

引數:

  • dtype:資料型別。常用的是tf.float32,tf.float64等數值型別
  • shape:資料形狀。預設是None,就是一維值,也可以是多維,比如[2,3], [None, 3]表示列是3,行不定
  • name:名稱。
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}))  # Will succeed.