最簡單的數據導入方式是使用Feeding:即把輸入設定成一個不需要初始化值的placeholder
。
input1 = tf.placeholder(tf.float32, shape=[2])
input2 = tf.placeholder(tf.float32, shape=[2])
placeholder創建的是一類特殊的Tensor
,可以直接用來構建Graph
output = input1 + input2
在每次執行output
之前,要準備好具體的輸入數據(例如numpy數組):
data1 = numpy.array([0.5, 0.6])
data2 = numpy.array([1.0, 2.0])
執行時,要把具體數據用feed_dict
參數傳入run
函數:
sess = tf.Session()
output_val = sess.run(output, feed_dict={input1: data1, input2: data2})
print(output_val)
實際應用中,往往在讀取數據函數中維護一個計數變量,每次調用返回一個batch的數據:
for step in xrange(100):
data1, data2 = read_next_batch()
output_val = sess.run(output, feed_dict={input1: data1, input2: data2})
這裏的read_next_batch函數返回的是真實數據,不是符號運算。