roberto
roberto

Reputation: 1

Tensorflow, Cannot feed value of shape ..... for Tensor

I have a problem with linear regression and 3d matrices. They are all floating point numbers, with labels.

I got started from this code but I changed the matrix: https://aqibsaeed.github.io/2016-07-07-TensorflowLR/

With 2 dimensions, it is working well but, with 3, I can not get it running.

this is the shape

(387, 7, 10) shape train_x
(387, 1) shape train_x
(43, 7, 10) test_x.shape
(43, 1) test_y.shape

n_dim = f.shape[1]
train_x, test_x, train_y, test_y = train_test_split(f,l,test_size=0.1, shuffle =False)
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
print(test_y.shape)
learning_rate = 0.01
training_epochs = 1000
cost_history = np.empty(shape=[1],dtype=float)

X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,1])
W = tf.Variable(tf.ones([n_dim,1]))

#init = tf.initialize_all_variables()
init = tf.global_variables_initializer()

y_ = tf.matmul(X, W)
cost = tf.reduce_mean(tf.square(y_ - Y))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


sess = tf.Session()
sess.run(init)

for epoch in range(training_epochs):
    sess.run(training_step,feed_dict={X:train_x,Y:train_y})
    cost_history = np.append(cost_history,sess.run(cost,feed_dict={X: train_x,Y: train_y}))

    plt.plot(range(len(cost_history)),cost_history)
plt.axis([0,training_epochs,0,np.max(cost_history)])
plt.show()

pred_y = sess.run(y_, feed_dict={X: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse)) 

fig, ax = plt.subplots()
ax.scatter(test_y, pred_y)
ax.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw=3)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

 </ blink>

this is the mistake




  \session.py", line 1100, in _run
        % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))

    ValueError: Cannot feed value of shape (387, 7, 10) for Tensor 'Placeholder_12:0', which has shape '(?, 7)'

Upvotes: 0

Views: 1146

Answers (1)

lazyman
lazyman

Reputation: 123

Your error message shows exact reason why it is raised. The dimension between placeholder and train_x doesn't fit.

train_x has a (387, 7, 10) shape. In usual convention, you have 387 datapoint which has (7, 10) dimension.

But, X (placeholder, the bucket you will put train_x in) has a [None, n_dim] (I guess n_dim is 7) shape.

Using [None, ~] in the first element is only accepted as the number of datapoints, not dimension of your data.

So you need to change [None, n_dim] to [None, 7, 10] in this case.

edited)

In this case, X is not exacty 3D data. just a bunch of 2D data. For direct weight multiplication of 2D data, you need convolution step. That is CNN. But you only have very small dimension data matrix, you just need to reshape (7,10) matrix shape data to (7*10) vector shape data.

Using tf.reshape function.tf.reshape(X, shape=[387, 7*10]) will be works, and also change your W to right dimension to multiply. like, tf.Variable(tf.ones([7*10,1])).

Upvotes: 1

Related Questions