DINA TAKLIT
DINA TAKLIT

Reputation: 8418

TensorFlow cannot feed value of shape (538, 1) for Tensor 'Placeholder_21:0', which has shape '(?, 8)'?

I tried to implement a neural network to solve classification problem however my program :

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

i tried to reshape the data and tried many solution given in stack to fix my problem but does not work for me, I wonder what should I do ?

the most important part:

...
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!")

Edit: Once I print that: print(y_train_.head()) it gives:

    0
0   2
1   4
2   8
3  16
4  32

Upvotes: 1

Views: 67

Answers (2)

DINA TAKLIT
DINA TAKLIT

Reputation: 8418

I realized later since i have my y_train csv file that contain only one column so i have to declare it like this

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

I should not confuse between the number of classes and how to declare "Y".

So like he said pinxue if i declare Y like this :

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

My Y placeholder is in shape of [m, 8] not [m,1]. So I had to declare it as i mentioned in the solution above to fix it.

Upvotes: 0

pinxue
pinxue

Reputation: 1746

Y = tf.placeholder(shape=[None, n_output], dtype=tf.float32) So that your Y placeholder is in shape of [m, 8]. And obviously y_train is not constructed properly, try y_train.values() instead of np.array(y_train).

Upvotes: 1

Related Questions