线性回归模型对张量流不能学习偏差

时间:2017-06-30 08:57:34

标签: tensorflow

我正在尝试使用一些生成的数据在Tensorflow中训练线性回归模型。该模型似乎学习了线的斜率,但无法了解偏差。

我试过改变号码。时间,重量(斜率)和偏差,但每次,模型的学习偏差都是零。我不知道我哪里出错了,一​​些帮助将不胜感激。

这是代码。

import numpy as np
import tensorflow as tf

# assume the linear model to be Y = W*X + b
X = tf.placeholder(tf.float32, [None, 1])
Y = tf.placeholder(tf.float32, [None,1])
#  the weight and biases
W = tf.Variable(tf.zeros([1,1]))
b = tf.Variable(tf.zeros([1]))
# the model
prediction = tf.matmul(X,W) + b
# the cost function
cost = tf.reduce_mean(tf.square(Y - prediction))
# Use gradient descent

learning_rate = 0.000001
train_step = 
tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
steps = 1000
epochs = 10
Verbose = False
# In the end, the model should learn these values
test_w = 3
bias = 10

for _ in xrange(epochs):  
    for i in xrange(steps):
    #     make fake data for the model
    #     feed one example at a time
#     stochastic gradient descent, because we only use one example at a time
        x_temp = np.array([[i]])
        y_temp = np.array([[test_w*i + bias]])
    #     train the model using the data
        feed_dict = {X: x_temp, Y:y_temp}
        sess.run(train_step,feed_dict=feed_dict)
        if Verbose and i%100 == 0:
            print("Iteration No: %d" %i)
            print("W = %f" % sess.run(W))
            print("b = %f" % sess.run(b))

print("Finally:")
print("W = %f" % sess.run(W))
print("b = %f" % sess.run(b))
# These values should be close to the values we used to generate data

https://github.com/HarshdeepGupta/tensorflow_notebooks/blob/master/Linear%20Regression.ipynb

输出位于最后一行代码中。 该模型需要学习test_w和bias(在笔记本链接中,它位于第3个单元格中,在第一个注释之后),分别设置为3和10。

模型正确地学习了重量(斜率),但无法学习偏差。错误在哪里?

1 个答案:

答案 0 :(得分:1)

主要问题是您一次只向模型喂食一个样品。这使得优化器非常不稳定,这就是为什么你必须使用如此小的学习速率。我建议你在每一步中加入更多的样品。

如果您坚持一次喂一个样本,也许您应该考虑使用具有动量的优化器,如tf.train.AdamOptimizer(learning_rate)。通过这种方式,您可以提高学习率并达到收敛。

相关问题