为什么张量流线性回归预测都是0?

2024-05-01 21:56:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用张量流实现一个线性回归。但我不知道怎么了。如果我只训练一次,预测结果都是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\u量表数据集:

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()})

损失不减反增!!!为什么? 请帮帮我!谢谢!你知道吗


Tags: runformatsizetfastrainmatrixlabel
1条回答
网友
1楼 · 发布于 2024-05-01 21:56:03

你用什么数据来训练模型? 尝试减小梯度下降的步长值 优化器=tf.train.GradientDescentOptimizer系列(0.01)。最小化(损失)并运行1000次或更多

相关问题 更多 >