我正在尝试使用Tensorflow训练一个带有正则化的多元线性回归模型。但是,出现了问题:在下面的代码中,我无法让训练部分计算出我想要用于梯度下降更新的误差。我在设置图形时是否做错了什么?
def normalize_data(matrix):
averages = np.average(matrix,0)
mins = np.min(matrix,0)
maxes = np.max(matrix,0)
ranges = maxes - mins
return ((matrix - averages)/ranges)
def run_regression(X, Y, X_test, Y_test, lambda_value = 0.1, normalize=False, batch_size=10):
x_train = normalize_data(X) if normalize else X
y_train = Y
x_test = X_test
y_test = Y_test
session = tf.Session()
# Calculate number of features for X and Y
x_features_length = len(X[0])
y_features_length = len(Y[0])
# Build Tensorflow graph parts
x = tf.placeholder('float', [None, x_features_length], name="X")
y = tf.placeholder('float', [None, y_features_length], name="Y")
theta = tf.Variable(tf.random_normal([x_features_length, y_features_length], stddev=0.01), name="Theta")
lambda_val = tf.constant(lambda_value)
# Trying to implement this way http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
y_predicted = tf.matmul(x, theta, name="y_predicted")
regularization_cost_part = tf.cast(tf.mul(lambda_val,tf.reduce_sum(tf.pow(theta,2)), name="regularization_param"), 'float')
polynomial_cost_part = tf.reduce_sum(tf.pow(tf.sub(y_predicted, y), 2), name="polynomial_sum")
# Set up some summary info to debug
with tf.name_scope('cost') as scope:
cost_func = tf.mul(tf.cast(1/(2*batch_size), 'float'), tf.cast(tf.add(polynomial_cost_part, regularization_cost_part), 'float'))
cost_summary = tf.scalar_summary("cost", cost_func)
training_func = tf.train.GradientDescentOptimizer(0.03).minimize(cost_func)
with tf.name_scope("test") as scope:
correct_prediction = tf.sub(tf.cast(1, 'float'), tf.reduce_mean(tf.sub(y_predicted, y)))
accuracy = tf.cast(correct_prediction, "float")
accuracy_summary = tf.scalar_summary("accuracy", accuracy)
saver = tf.train.Saver()
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/football_logs", session.graph_def)
init = tf.initialize_all_variables()
session.run(init)
for i in range(0, (len(x_train)/batch_size)):
session.run(training_func, feed_dict={x: x_train[i*batch_size:i*batch_size+batch_size], y: y_train[i*batch_size:i*batch_size+batch_size]})
if i % batch_size == 0:
result = session.run([merged, accuracy], feed_dict={x: x_test, y: y_test})
writer.add_summary(result[0], i)
print "step %d, training accuracy %g"%(i, result[1])
print "test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})
save_path = saver.save(session, "/tmp/football.ckpt")
print "Model saved in file: ", save_path
session.close()
我的输出看起来像这样:
step 0, training accuracy 39.1802
step 10, training accuracy 39.1802
step 20, training accuracy 39.1802
...
step 210, training accuracy 39.1802
test accuracy 39.1802
Model saved in file: /tmp/football.ckpt