首页 文章

Tensorflow重塑问题,ValueError:无法为Tensor u 'Placeholder:0'提供形状(1,2)的值,其形状为'(?, 1, 2)'

提问于
浏览
1

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

我的培训和测试数据有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文件的部分,......等 .

对代码的任何评论都很棒,谢谢 .

1 回答

  • 1

    您正在尝试提供不遵守占位符维度的数组 . 例如 . 对于batch_x,您尝试将[1,2]输入[?,1,2],而对于batch_y,您尝试将[2]输入[?,2] . 如果batch_size为1,您希望将[1,1,2]和[1,2]分别导入[?,2],分别用于batch_x和batch_y .

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

相关问题