尝试使用张量流进行线性回归时发生错误。我试图用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 = 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所期望的形状之间不匹配。我知道这是一个错误,但我不知道如何解决。
答案 0 :(得分:1)
错误是说y_train
的形状为(8009,)
,而TF期望占位符(8009, 1)
的形状为y
的数字数组。沿最后一个轴扩展y_train
的尺寸应该可以解决以下问题:
result, err = sess.run([optimizer, cost], feed_dict={X: X_train, y: y_train[:, None]})