Tensorflow reshape issue, ValueError: Cannot feed value of shape (1, 2) for Tensor u'Placeholder:0', which has shape '(?, 1, 2)'

Tensorflow reshape issue, ValueError: Cannot feed value of shape (1, 2) for Tensor u'Placeholder:0', which has shape '(?, 1, 2)'

我收到此错误消息: ValueError:无法为 Tensor u'Placeholder:0' 提供形状 (1, 2) 的值,其形状为 '(?, 1, 2)'

我的训练和测试数据有两个特征

[[10, 10],[1,2],[3,2]...]

我的目标数据是这样的:

[[0, 1], [1, 0], [1, 0]...]

这是我的代码:

training_data = np.vstack(training_data)
training_target = np.vstack(training_target)
test_data = np.vstack(test_data)
test_target = np.vstack(test_target)

learning_rate = 0.001

n_input = 2  
n_steps = 1  
n_hidden = 128  
n_classes = 2  

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])

# Define weights
weights = {
    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([n_classes]))
}

def RNN(x, weights, biases):
    x = tf.unstack(x, n_steps, 1)

    # Define a lstm cell with tensorflow
    lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)

    # Get lstm cell output
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

pred = RNN(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    step = 1

    for i in range(len(training_data)):
        batch_x = training_data[i]
        batch_y = training_target[i]
        print(batch_x)
        print(batch_y)
        batch_x = tf.reshape(batch_x, [1, 2]).eval()
        print(batch_x)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
        acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
        loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
        print("Iter " + str(step) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))

    print("Optimization Finished!")

    print("Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_target}))

我需要重塑方面的帮助,我还没有实现下一个批处理功能,只是想让它正常工作。

没有包含我正在加载 CSV 文件的部分,...等等

对代码的任何评论都很好,谢谢。

您正在尝试提供不符合占位符尺寸的数组。例如。对于 batch_x,您尝试将 [1, 2] 送入 [?, 1, 2],对于 batch_y,您尝试将 [2] 送入 [?, 2]。如果你的 batch_size 是 1.

batch_size = 1
with tf.Session() as sess:
    sess.run(init)
    step = 1

    for i in range(len(training_data)):
        batch_x = training_data[i]
        batch_y = training_target[i]
        batch_x = np.reshape(batch_x, [batch_size, 1, 2])
        batch_y = np.reshape(batch_y, [batch_size, 2])
        [_, acc, loss] = sess.run([optimizer, accuracy, cost], feed_dict={x: batch_x, y: batch_y})
        print("Iter " + str(step) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))