如何在Tensorflow中获得线性回归的正确答案?

时间:2018-03-18 18:02:39

标签: python-3.x tensorflow machine-learning linear-regression nan

我没有得到关于线性回归问题的输出。 这是一个简单的变量线性回归问题。 我使用过Kaggle的线性回归数据集,
从这里:Linear Regression on Random Dataset

它没有给出所需的输出。它给出了权重和偏差的纳米值

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


# In[20]:


#Getting DataFrames
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')

#Dropping NaN rows
train_data.dropna()
test_data.dropna()

#Getting features and labels
X_train = train_data.iloc[:,0].values
Y_train = train_data.iloc[:,1].values

test_X = test_data.iloc[:,0].values
test_Y = test_data.iloc[:,1].values

#Plotting Training Data
plt.scatter(X_train,Y_train)


# In[58]:


#Training the model

X = tf.placeholder(tf.float32,name='X')
Y = tf.placeholder(tf.float32,name='Y')

W = tf.Variable(tf.random_normal([]),dtype=tf.float32,name='weights')
b = tf.Variable(tf.random_normal([]),dtype=tf.float32,name='bias')

Y_pred = W*X + b

cost = tf.square(Y_pred,name='cost')

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)
init = tf.global_variables_initializer()


# In[61]:


with tf.Session() as sess:
    sess.run(init)
    for i in range(1000):
        sess.run(optimizer,feed_dict={X:X_train,Y:Y_train})
    W_out,b_out = sess.run([W,b])
    writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph)


print(W_out,b_out)


# In[60]:


#plt.plot(X_train, W_out*X_train + b_out, color='red')
plt.scatter(X_train,Y_train)
plt.plot(X_train, W_out*X_train + b_out, color='red')

它正在提供输出:

nan nan

权重和偏见正在变为纳米值。

2 个答案:

答案 0 :(得分:0)

您尚未指定任何权重和偏见。您将权重定义为

W = tf.Variable(tf.random_normal([]),dtype=tf.float32,name='weights')
b = tf.Variable(tf.random_normal([]),dtype=tf.float32,name='bias')

这里,tf.random_normal的输入是一个空数组。因此,Wb都是空的。您需要指定Wb的形状,W[in_dim, out_dim]b[out_dim]。检查tf.random_normal文档,这里第一个参数是所需张量的形状。

答案 1 :(得分:0)

首先,在您的成本函数中有一个小错误,它可能应该是cost = tf.reduce_mean(tf.square(Y_pred - Y,name='cost'))

其次,layog有一个观点,在定义参数时应提供Wb的形状。无论如何,因为您正在使用Y_pred = W*X + b,因此避免tf.matmul,使用新的成本函数无法明确提供形状。

第三,使用你的代码我发现GradientDescentOptimizer似乎不能很好地使用给定的学习率和一些我手动尝试的,即使我真的认为它应该,嗯。但是,AdamOptimizer没问题。

下面的代码稍微调整了您的示例,应该可以正常工作:

# casting data into rank-2 form for tensorflow
X_train = X_train.reshape((-1,1))
test_X = test_X.reshape((-1,1))

Y_train = Y_train.reshape((-1,1))
test_Y = test_Y.reshape((-1,1))

tf.reset_default_graph()

D = X_train.shape[1] # number of non-bias features

#Training the model
X = tf.placeholder(tf.float32, shape=(None,D), name='X')
Y = tf.placeholder(tf.float32, shape=(None,1), name='Y')

# with explicit definition of shapes
# W = tf.Variable(tf.random_normal([D,1], mean=0., stddev=1.), dtype=tf.float32, name='weights')
# b = tf.Variable(tf.random_normal([1], mean=0., stddev=1.), dtype=tf.float32, name='bias')

# without explicit definition of shapes
W = tf.Variable(tf.random_normal([], mean=0., stddev=1.), dtype=tf.float32, name='weights')
b = tf.Variable(tf.random_normal([], mean=0., stddev=1.), dtype=tf.float32, name='bias')

#Y_pred = tf.matmul(X,W) + b # doesn't like undefined shapes
Y_pred = X*W + b # fine with undefined shapes
error = Y_pred - Y
cost = tf.reduce_mean(tf.square(error), name="cost")

# optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-1)
update = optimizer.minimize(cost)

init = tf.global_variables_initializer()

Niter = 500
Nprint = 50
with tf.Session() as sess:

    sess.run(init)

    for i in range(Niter):
        sess.run(update, feed_dict={X:X_train, Y:Y_train})
        if i%Nprint == 0:
            print("\n%i/%i" % (i+1,Niter))
            print("W",W.eval())
            print("b",b.eval())
            print("cost",cost.eval(feed_dict={X:X_train, Y:Y_train}))

    W_out, b_out = sess.run([W, b])