不能为张量'x_17:0'(有形状)提供形状(500)的值。,500)

时间:2021-02-07 21:24:37

I'm just learning TensorFlow, so sorry if this is obvious. I've checked the documentation and experimented quite a bit and I just can't seem to get this to work.

我只是在学习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() outputs an numpy array of 500 numbers and the number that I want it to guess. I've tried shuffling OUT_DIMS and FIN_SIZE around, I tried accumulating them into batches to more closely match what the tutorial looked like, I tried setting OUT_DIMS to 0, removing it entirely, and I tried replacing None with other numbers, but have not made any progress.

yield_financials()输出一个包含500个数字的numpy数组,以及我希望它猜测的数字。我尝试过对OUT_DIMS和FIN_SIZE进行拖放,我尝试将它们累积成批次,以便更紧密地匹配教程的外观,我尝试将OUT_DIMS设置为0,完全删除它,我尝试用其他数字替换None,但没有取得任何进展。

2 个解决方案

#1


2  

Try

试一试

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

#2


0  

I had the same problem and I solved this problem.I hope that it's helpful for u.

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

firstly,I transformed load data into :

首先,我将load数据转换为:

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=',')

Then,transformed trX, trY, teX, teY data into:

然后将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)

Finally,I transformed launching the graph in a session into:

最后,我将在会话中启动图形转换为:

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})))

That's all.

这是所有。

#1


2  

Try

试一试

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

#2


0  

I had the same problem and I solved this problem.I hope that it's helpful for u.

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

firstly,I transformed load data into :

首先,我将load数据转换为:

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=',')

Then,transformed trX, trY, teX, teY data into:

然后将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)

Finally,I transformed launching the graph in a session into:

最后,我将在会话中启动图形转换为:

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})))

That's all.

这是所有。