placeholder佔位符 和 feed喂資料
阿新 • • 發佈:2018-12-12
在說feeds操作之前,先講一下tf.placeholder(dtype, shape=None, name=None),真正在feeds時 不必指定初始值,可在執行時,通過 Session.run 的函式的 feed_dict 引數指定;
引數: 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.
返回:Tensor 型別
例子: import tensorflow as tf input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) ouput = tf.multiply(input1, input2) with tf.Session() as sess: print(sess.run(ouput, feed_dict={input1: [7.], input2: [2.]}))
結果:[ 14.]
---------------------