tbone
tbone

Reputation: 1322

Tensorflow value error : Cannot feed value of shape (8009,) for Tensor 'Placeholder_5:0', which has shape '(?, 1)'

The error occurs when trying to do linear regression using tensorflow. I tried to fix it with the flattern () function but it still doesn't work. Also I tried some solution from this website.

below is some code and full traceback:

# Convert test features and Labels to Numpy Arrays
X_test = np.array(X_test)
y_test = np.array(y_test)

# Converting from Pandas Dataframe to Numpy Arrays
X_train = np.array(X_train)
y_train = np.array(y_train)

# Define Training Parameters

# Learning Rate
lr = 0.1

# Number of epochs for which the model will run
epochs = 1000

# Define Features and Label Placeholders

# Features
X = tf.placeholder(tf.float32,[None,X_train.shape[1]])

# Labels 
y = tf.placeholder(tf.float32,[None,1])

# Define Hyperparameters

# Weight
W = tf.Variable(tf.ones([X_train.shape[1], 1]))

# Bias
b = tf.Variable(tf.ones(X_train.shape[1]))

# Initiaize all Variables
init = tf.global_variables_initializer()

# Define Cost Function, Optimizer and the Output Predicitons Function

# Predictions
# y_hat = (W*X + b)
y_hat = tf.add(tf.matmul(X, W), b)

# Cost Function
# MSE
cost = tf.reduce_mean(tf.square(y - y_hat))

# Gradient Descent Optimizer to Minimize the Cost
optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cost)

# Tensor to store the cost after every Epoch
# Comes in handy while plotting the cost vs epochs
cost_history = np.empty(shape=[1],dtype=float)



    with tf.Session() as sess:
        # Initialize all Variables
        sess.run(init)

        for epoch in range(0,epochs):
            # Run the optimizer and the cost functions
            result, err = sess.run([optimizer, cost], feed_dict={X: X_train, y: y_train})

        # Add the calculated cost to the array
        cost_history = np.append(cost_history,err)

        # Print the Loss/Error after every 100 epochs
        if epoch%100 == 0:
            print('Epoch: {0}, Error: {1}'.format(epoch, err))

    print('Epoch: {0}, Error: {1}'.format(epoch+1, err))

    # Values of Weight & Bias after Training
    new_W = sess.run(W)
    new_b = sess.run(b)

    # Predicted Labels
    y_pred = sess.run(y_hat, feed_dict={X: X_test})

    # Mean Squared Error
    mse = sess.run(tf.reduce_mean(tf.square(y_pred - y_test)))

Traceback:

ValueError                                Traceback (most recent call last)
<ipython-input-190-76177d01d272> in <module>
      5     for epoch in range(0,epochs):
      6         # Run the optimizer and the cost functions
----> 7         result, err = sess.run([optimizer, cost], feed_dict={X: X_train, y: y_train})
      8 
      9         # Add the calculated cost to the array

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1147                              'which has shape %r' %
   1148                              (np_val.shape, subfeed_t.name,
-> 1149                               str(subfeed_t.get_shape())))
   1150           if not self.graph.is_feedable(subfeed_t):
   1151             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (8009,) for Tensor 'Placeholder_5:0', which has shape '(?, 1)'

seems to mismatch between the shape of what I am feeding in and what the TensorFlow is expecting. I know that this is a mistake but I don't know how to fix it.

Upvotes: 0

Views: 55

Answers (1)

rvinas
rvinas

Reputation: 11895

The error is saying that the shape of y_train is (8009,) while TF is expecting a numpy array of shape (8009, 1) for placeholder y. Expanding dimensions of y_train along the last axis should solve the problem:

result, err = sess.run([optimizer, cost], feed_dict={X: X_train, y: y_train[:, None]})

Upvotes: 1

Related Questions