我正在使用使用Tensorflow的线性回归模型。我在sess.run部分中遇到一些错误。请帮助我解决这个问题。
第一部分:
for epoch in range(training_epochs):
#for (x,y) in zip(train_X,train_Y):
sess.run(training_step,feed_dict={X:train_X,Y:train_Y})
错误: 不兼容的形状:[5,1]与[40,1]
第二部分:
pred_Y = sess.run(pred, feed_dict={X: test_X})
错误: 不兼容的形状:[10,1]与[5,1]
我的整个代码是:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
# Importing the dataset
dataset = pd.read_csv("50_Startups.csv")
X_data = dataset.iloc[:, :-1].values
Y_data = dataset.iloc[:, 4].values
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder = LabelEncoder()
X_data[:, 3] = labelencoder.fit_transform(X_data[:, 3])
onehotencoder = OneHotEncoder(categorical_features = [3])
X_data = onehotencoder.fit_transform(X_data).toarray()
# Avoiding the Dummy Variable Trap
X_data = X_data[:, 1:] #excluding the 1st dummy column
from sklearn.cross_validation import train_test_split
train_X, test_X, train_Y, test_Y = train_test_split(X_data, Y_data, test_size = 0.2, random_state = 0)
train_Y = np.array(train_Y).reshape(40,1)
test_Y = np.array(test_Y).reshape(10,1)
# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
cost_history = np.empty(shape=[1],dtype=float)
m_dim = train_X.shape[0]
n_dim = train_X.shape[1]
X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,1])
W = tf.Variable(tf.ones((n_dim,1)))
B = tf.Variable(tf.ones((n_dim,1)))
init = tf.global_variables_initializer()
pred = tf.add(tf.matmul(X, W),B)
cost = tf.reduce_mean(tf.square(pred - Y))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
sess.run(init)
for epoch in range(training_epochs):
#for (x,y) in zip(train_X,train_Y):
sess.run(training_step,feed_dict={X:train_X,Y:train_Y})
# Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
"W=", sess.run(W), "b=", sess.run(B))
pred_Y = sess.run(pred, feed_dict={X: test_X})
mse = tf.reduce_mean(tf.square(pred_Y - test_Y))
print("MSE: %.4f" % sess.run(mse))