我正在尝试在张量流中训练单层感知器(基于this上的代码)在以下数据文件中:
1,1,0.05,-1.05
1,1,0.1,-1.1
....
其中最后一列是标签(3个参数的功能),前三列是函数参数。读取数据并训练模型的代码(为了便于阅读,我将其简化):
import tensorflow as tf
... # some basics to read the data
example, label = read_file_format(filename_queue)
... # model construction and parameter setting
# Launch the graph
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
_, c = sess.run([optimizer, cost], feed_dict={x: example, y: label})
print("Optimization Finished!")
但是当我运行它时,会出现以下错误:
Traceback (most recent call last):
File "nn.py", line 85, in <module>
_, c = sess.run([optimizer, cost], feed_dict={x: example, y: label})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 710, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 887, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (3,) for Tensor u'Placeholder:0', which has shape '(?, 3)'
答案 0 :(得分:1)
你的图表期望X是一个形状的张量(?,3)。您的示例数据具有形状(3,),即长度为3的1维向量。可以将示例重塑为(1,3),或者在一次拍摄中传递一批示例(例如,10,形状为(10, 3))