无法为Tensor'x_17:0'提供形状值(500,),其形状为'(?,500)'

时间:2016-04-12 22:18:14

标签: python tensorflow

我只是在学习TensorFlow,很抱歉,如果这很明显的话。我已经检查了文档并进行了相当多的实验,但我似乎无法让它工作。

def train_network():
    OUT_DIMS = 1
    FIN_SIZE = 500
    x = tf.placeholder(tf.float32, [OUT_DIMS, FIN_SIZE], name="x")
    w = tf.Variable(tf.zeros([FIN_SIZE, OUT_DIMS]), name="w")
    b = tf.Variable(tf.zeros([OUT_DIMS]), name="b")
    y = tf.tanh(tf.matmul(x, w) + b)

    yhat = tf.placeholder(tf.float32, [None, OUT_DIMS])
    cross_entropy = -tf.reduce_sum(yhat*tf.log(y))

    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

    # Launch the model
    init = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init)

    for this_x, this_y in yield_financials():
        sess.run(train_step, feed_dict={x:    this_x,
                                        yhat: this_y})
        print(end=".")
        sys.stdout.flush()

yield_financials()输出一个由500个数字组成的numpy数组以及我想要它猜测的数字。我试过洗掉OUT_DIMS和FIN_SIZE,我尝试将它们分批堆积以更接近地匹配教程的样子,我尝试将OUT_DIMS设置为0,完全删除它,我尝试用其他数字替换None,但是没有制作任何进展。

2 个答案:

答案 0 :(得分:2)

尝试

    this_x = np.reshape(this_x,(1, FIN_SIZE))
    sess.run(train_step, feed_dict={x:    this_x,
                                    yhat: this_y})

答案 1 :(得分:0)

我有同样的问题,我解决了这个问题。我希望它对你有帮助。

首先,我将加载数据转换为:

train_data = np.genfromtxt(train_data1, delimiter=',')
train_label = np.transpose(train_label1, delimiter=',')
test_data = np.genfromtxt(test_data1, delimiter=',')
test_label = np.transpose(test_label1, delimiter=',')

然后,将trX,trY,teX,teY数据转换为:

# convert the data
trX, trY, teX, teY = train_data,train_label, test_data, test_label
temp = trY.shape
trY = trY.reshape(temp[0], 1)
trY = np.concatenate((1-trY, trY), axis=1)
temp = teY.shape
teY = teY.reshape(temp[0], 1)
teY = np.concatenate((1-teY, teY), axis=1)

最后,我将会话中的图形转换为:

with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()

    for i in range(100):
            sess.run(train_op, feed_dict={X:  trX, Y: trY})        
            print(i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))

这就是全部。