python - 为什么 tensorflow 线性回归预测全为0?

标签 python machine-learning tensorflow linear-regression gradient-descent

我想使用 tensorflow 实现线性回归。但我不知道这是怎么回事。如果我只训练一次,预测结果将全部为0。如果我训练更多,损失会增加而不是减少。 谁能帮我?非常感谢!

# Step2
x = tf.placeholder(tf.float64, [None, 14])
y_ = tf.placeholder(tf.float64, [None])
# Step3
feature_size = int(x.shape[1])
label_size = int(1)
w = tf.Variable(tf.zeros([feature_size, label_size], dtype='float64'), name='weight')
b = tf.Variable(tf.zeros([label_size], dtype='float64'), name='bias')
# Step4
y = tf.matmul(x, w) + b
# Step5
loss = tf.reduce_sum(tf.square(y-y_))# + tf.matmul(tf.transpose(w), w)
# Step6
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
with tf.Session() as sess:
    # Step7
    tf.global_variables_initializer().run()
    train_loss, _, w_, b_, y_pred = sess.run([loss, optimizer, w , b , y],
                                            {x: X_train.as_matrix(), y_: y_train.as_matrix()})

如果我用下面的代码显示结果:

    print("The train_loss is :{0}".format(train_loss))
    print("y_pred shape:{1}\n y_pred value{0}".format(y_pred, y_pred.shape))
    print("w_:{0}".format(w_))
    print("b_:{0}".format(b_))

结果为:

The train_loss is :25366.999902840118
y_pred shape:(151, 1)
 y_pred value[[ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
...
 [ 0.]]
w_:[[ -4197.62931207]
 [ -5012.08767412]
 [-12005.66678623]
 [ 16558.73513235]
 [ -7305.34601191]
 [ -5714.5346788 ]
 [ -9633.25591793]
 [-12477.03557256]
 [ -9630.39349598]
 [ -7365.70395179]
 [-11168.48902116]
 [ -6483.21729379]
 [  2177.84048453]
 [ -3059.72968574]]
b_:[ 24045.6024]

但是数据使用libsvm Babyfat_scale数据集:

1.0708 1:-0.482105 2:-0.966102 3:-0.707746 4:0.585492 5:-0.492537 6:-0.514938 7:-0.598475 8:-0.69697 9:-0.411471 10:-0.465839 11:-0.621622 12:-0.287129 13:-0.0791367 14:-0.535714 
1.0853 1:-0.743158 2:-1 3:-0.552422 4:0.772021 5:-0.263682 6:-0.497364 7:-0.654384 8:-0.562998 9:-0.426434 10:-0.465839 11:-0.418919 12:-0.435644 13:0.136691 14:-0.142857 

如果我尝试使用相同的数据训练 100 次:

for i in range(100):
        train_loss, _, w_, b_, y_pred = sess.run([loss, optimizer, w , b , y],
                                                {x: X_train.as_matrix(), y_: y_train.as_matrix()})

损失不降反增!!!为什么? 请帮我!非常感谢!

最佳答案

您使用哪些数据来训练模型? 尝试减小梯度下降的步长值 Optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) 并运行 1000 次或更多

关于python - 为什么 tensorflow 线性回归预测全为0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385974/

相关文章:

python - 逻辑回归模型不是学习

machine-learning - 决策树 split 策略

python - 为什么会出现这个错误 "input_2_1:0 is both fed and fetched"?

python - 使用python根据经度和纬度匹配两个数据集

python - 无法使按钮(使用 tkinter 创建)自动退出

machine-learning - 有没有一种机器学习方法可以学习句子的结构而不是单词?

python - keras + tensorflow 中的高级自定义激活函数

tensorflow - 如何在tensorflow中初始化pb文件的变量而不附带ckpt文件?

python - 输出 : terminal attributes: No such device or address in Windows Pycharm debugger

python - Django,在wsgi.py中使用条件设置模块