Tensorflow – 线性回归:无法正确绘图

我正在使用Tensorflow进行线性回归问题的工作。我得到的曲线pred_y是平的。我应该如何用观察的训练样本来拟合我的曲线?

这是我的Tensorflow代码:

# coding: utf-8# In[146]:import numpy as npimport matplotlib.pyplot as pltimport tensorflow as tfimport pandas as pd# In[147]:train_features = pd.read_csv("training_set_X.csv", delimiter=',').as_matrix()train_observations = pd.read_csv("training_set_Y.csv", delimiter=',').as_matrix()print("Training features: ")train_features# In[148]:print("Training observations: ")train_observations# In[149]:print("Shape of training features = ", train_features.shape)print("Shape of training observations = ", train_observations.shape)# In[150]:# Normalization of training data.train_features_stddev_arr = np.std(train_features, axis=0)train_features_mean_arr = np.mean(train_features, axis=0)normalized_train_features = (train_features - train_features_mean_arr) / train_features_stddev_arr# In[151]:print("Training features: Standard deviation....")train_features_stddev_arr# In[152]:print("Training featues: Mean....")train_features_mean_arr# In[153]:print("Normalized training features....")normalized_train_features# In[154]:# Layer parameters.n_nodes_h11 = 5n_nodes_h12 = 5n_nodes_h13 = 3no_features = 17learning_rate = 0.01epochs = 200# In[155]:cost_history = []# In[156]:X = tf.placeholder(tf.float32, name='X')Y = tf.placeholder(tf.float32, name='Y')# In[157]:# Defining weights and biases.first_weight = tf.Variable(tf.random_normal([no_features, n_nodes_h11], stddev=np.sqrt(2/no_features)))second_weight = tf.Variable(tf.random_normal([n_nodes_h11, n_nodes_h12], stddev=np.sqrt(2/n_nodes_h11)))third_weight = tf.Variable(tf.random_normal([n_nodes_h12, n_nodes_h13], stddev=np.sqrt(2/n_nodes_h12)))output_weight = tf.Variable(tf.random_normal([n_nodes_h13, 1], stddev=np.sqrt(2/n_nodes_h13)))# In[158]:first_bias = tf.Variable(tf.random_uniform([n_nodes_h11], -1.0, 1.0))second_bias = tf.Variable(tf.random_uniform([n_nodes_h12], -1.0, 1.0))third_bias = tf.Variable(tf.random_uniform([n_nodes_h13], -1.0, 1.0))output_bias = tf.Variable(tf.random_uniform([1], -1.0, 1.0))# In[159]:# Defining activations of each layer.first = tf.sigmoid(tf.matmul(X, first_weight) + first_bias)second = tf.sigmoid(tf.matmul(first, second_weight) + second_bias)third = tf.sigmoid(tf.matmul(second, third_weight) + third_bias)output = tf.matmul(third, output_weight) + output_bias# In[182]:# Using Mean Squared Errorcost = tf.reduce_mean(tf.pow(output - Y, 2)) / (2 * train_features.shape[0])# In[183]:# Using Gradient Descent algorithmoptimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)# In[184]:init = tf.global_variables_initializer()# In[194]:# Running the network.with tf.Session() as sess:    sess.run(init)    for step in np.arange(epochs):        sess.run(optimizer, feed_dict={X:normalized_train_features, Y:train_observations})        cost_history.append(sess.run(cost, feed_dict={X:normalized_train_features, Y:train_observations}))    pred_y = sess.run(output, feed_dict={X:normalized_train_features})    plt.plot(range(len(pred_y)), pred_y)    plt.plot(range(len(train_observations)), train_observations)# In[195]:plt.show()

训练特征的形状为(967, 17),训练观察的形状为(967, 1)

我观察到直线(pred_y)是由于pred_y值生成的大负数。而train_observation值已经是正数。

如果有人能帮助我解决这个问题就太好了。我不希望pred_y线是直的。我觉得我做错了什么。如果有人能指出我的错误就太好了。谢谢!

解决方案1.

你有一个17维的特征,因此在没有进行降维的情况下很难绘制出有意义的曲线。因此,你不能指望用你的代码得到有意义的图表。

解决方案2.

由@lincr提供的解决方案


回答:

你在这里使用了错误的损失函数。

你应该使用的是均方误差,应该是

tf.reduce_sum(tf.pow(output - Y, 2)/train_features.shape[0])

如果你想使用tf.reduce_mean,应该是

tf.reduece_mean(tf.squared_difference(output, Y))

请注意,reduce_sum中的除法操作已经执行了平均(mean)操作。

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注