Python Tensorflow值错误:无法为具有形状“(?,1)”的Tensor“占位符_5:0”输入形状(8009,)的值
尝试使用tensorflow进行线性回归时会出现错误。我试着用flattern函数修复它,但它仍然不起作用。我还尝试了一些解决方案,从这个网站 以下是一些代码和完整的回溯:Python Tensorflow值错误:无法为具有形状“(?,1)”的Tensor“占位符_5:0”输入形状(8009,)的值,python,tensorflow,Python,Tensorflow,尝试使用tensorflow进行线性回归时会出现错误。我试着用flattern函数修复它,但它仍然不起作用。我还尝试了一些解决方案,从这个网站 以下是一些代码和完整的回溯: # 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
# 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)))
回溯:
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)'
似乎在我输入的形状和TensorFlow期望的形状之间不匹配。我知道这是一个错误,但我不知道如何修复它。错误是说y_列的形状是8009,而TF期望一个形状为8009的numpy数组,1表示占位符y。沿最后一个轴扩展y_列的尺寸应能解决以下问题:
result, err = sess.run([optimizer, cost], feed_dict={X: X_train, y: y_train[:, None]})
您需要沿最后一个轴展开y_列的维度:result,err=sess。运行[optimizer,cost],feed_dict={X:X_列,y:y_列[:,None]}。这能解决问题吗?