gpt4 book ai didi

python - tensorflow - 线性回归 : Not proper able to do proper plotting

转载 作者:行者123 更新时间:2023-11-30 09:46:39 27 4
gpt4 key购买 nike

我一直在使用 Tensorflow 解决线性回归问题。我得到一条平坦的曲线 pred_y。我应该如何将我的曲线与观察的训练示例拟合?

这是我的 tensorflow 代码:

# coding: utf-8

# In[146]:


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import 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 = 5
n_nodes_h12 = 5
n_nodes_h13 = 3
no_features = 17
learning_rate = 0.01
epochs = 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 Error
cost = tf.reduce_mean(tf.pow(output - Y, 2)) / (2 * train_features.shape[0])


# In[183]:


# Using Gradient Descent algorithm
optimizer = 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(输出 - Y, 2)/train_features.shape[0])

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

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

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

关于python - tensorflow - 线性回归 : Not proper able to do proper plotting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51602859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com