猿问

“(?, 8)”的张量“Placeholder_21:0”提供形状 (538, 1) 的值?

我试图实现一个神经网络来解决分类问题,但是我的程序:


 _, c = sess.run([train_op, loss_op], feed_dict={X: x_train,Y: y_train})

我试图重塑数据并尝试了堆栈中给出的许多解决方案来解决我的问题,但对我不起作用,我想知道我该怎么办?


最重要的部分:


...

n_output = 8

n_input = 9 # Max number of input that may have features of one single program

################################ Dfine data ####################################

from google.colab import files

import io


uploaded = files.upload()

x_train_ = pd.read_csv(io.StringIO(uploaded['x_train.csv'].decode('utf-8')), skiprows=1, header=None)

uploaded1 = files.upload()

y_train_ = pd.read_csv(io.StringIO(uploaded1['y_train.csv'].decode('utf-8')), skiprows=1, header=None)

x_train.fillna(-1, inplace=True)


x_train = np.array(x_train)

y_train = np.array(y_train)


################################ Input, weights, biases ########################

# tf Graph input

X = tf.placeholder(shape=[None, n_input], dtype=tf.float32)

Y = tf.placeholder(shape=[None, n_output], dtype=tf.float32)

.....

################################ Construct model ###############################

logits = multilayer_perceptron(X)

# Define loss and optimizer

loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)

train_op = optimizer.minimize(loss_op)

...


# Initializing the variables

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    # Training cycle

    for epoch in range(training_epochs):

        avg_cost = 0.

        _, c = sess.run([train_op, loss_op], feed_dict={X: x_train,Y: y_train})

        ...

    print("Optimization Finished!")

编辑:一旦我打印出来:print(y_train_.head()) 它给出:


    0

0   2

1   4

2   8

3  16

4  32


qq_遁去的一_1
浏览 183回答 2
2回答

翻阅古今

Y = tf.placeholder(shape=[None, n_output], dtype=tf.float32) 所以你的 Y 占位符的形状是 [m, 8]。显然 y_train 构造不正确,请尝试使用 y_train.values() 而不是 np.array(y_train)。

拉丁的传说

我后来意识到,因为我的 y_train csv 文件只包含一列,所以我必须像这样声明它Y = tf.placeholder(shape=[None,1], dtype=tf.float32)我不应该混淆类的数量和如何声明“Y”。所以就像他说品雪一样,如果我这样声明 Y:Y = tf.placeholder(shape=[None, n_output], dtype=tf.float32)我的 Y 占位符的形状是 [m, 8] 而不是 [m,1]。所以我不得不像我在上面的解决方案中提到的那样声明它来修复它。
随时随地看视频慕课网APP

相关分类

Python
我要回答