1
$\begingroup$

I am trying to do a multivariate linear regression and I am having some issues. Namely, I am getting the following error:

ValueError: Cannot feed value of shape (3,) for Tensor 'X:0', which has shape '(1, 3)'

I have 3 feature variables, which I call trainX and 1 label, which I call trainY. Their shapes are the following (they are numpy arrays):

trainX.shape:
(2500, 3)
trainY.shape:
(2500,)

The following piece of code defines the tensors that I use to compute the model:

X = tf.compat.v1.placeholder("float", [1, 3], name="X")
Y = tf.compat.v1.placeholder("float", [1], name="Y")

W = tf.Variable(tf.zeros([3, 1]), name="W")
b = tf.Variable(tf.zeros([1]), name="b")

I calculate the predicted label and the cost function and the optimizer by doing:

predicted_y = tf.matmul(X, W) + b
cost = tf.reduce_sum(tf.pow(predicted_y-Y, 2)) / (2 * n)
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize(cost)

I am getting the error in the tensor-flow session, namely in the following piece of code:

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        for (_x, _y) in zip(trainX, trainY):
            sess.run(optimizer, feed_dict={X: _x, Y: _y})
        if (epoch + 1) % 100 == 0:
            c = sess.run(cost, feed_dict={X: trainX, Y: trainY})
            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))
    # Storing necessary values to be used outside the Session
    training_cost = sess.run(cost, feed_dict={X: trainX, Y: trainY})
    weight = sess.run(W)
    bias = sess.run(b)

Any help would be greatly appreciated.

$\endgroup$
1
$\begingroup$

trainX has shape (2500, 3), so when you iterate over trainX you get values with shape (3,). To match the shape of your placeholder "X", you need them to have shape (1, 3). This can be accomplished with numpy.reshape:

# do this after loading trainX
trainX = trainX.reshape((-1, 1, 3))
# new shape: (2500, 1, 3)
$\endgroup$
  • $\begingroup$ Thank you for commenting! I now get the following error: "ValueError: Cannot feed value of shape () for Tensor 'Y:0', which has shape '(1,)'" $\endgroup$ – iamatrain Jul 16 at 14:07
  • 1
    $\begingroup$ I fixed it by reshaping trainY (-1, 1, 1) and by adding "None, 1" in Y. Thank you! $\endgroup$ – iamatrain Jul 16 at 14:11
  • $\begingroup$ Oops, sorry I didn't catch that one. Try a similar reshape for trainY: trainY = trainY.reshape(-1, 1) $\endgroup$ – zachdj Jul 16 at 14:11
  • $\begingroup$ sorry but it doesn't actually work. I get the following error "ValueError: Cannot feed value of shape (2500, 1, 3) for Tensor 'X:0', which has shape '(1, 3)'". I get that error when "c = sess.run(cost, feed_dict={X: trainX, Y: trainY})" runs... Any suggestions? $\endgroup$ – iamatrain Jul 16 at 14:31
  • $\begingroup$ Your graph is configured to accept one example at a time, so when you pass in the entire array trainX and trainY, you get the error. I think you need to loop through the training examples and compute the cost for each one, then take the average cost outside of the loop. Does that make sense? $\endgroup$ – zachdj Jul 16 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.