我最近在我的计算机上安装了tensorflow,但我对第一个教程中获得的一些结果感到困惑。这是一个非常简单的线性回归模型,它找到W和b为W * x + b = y:
import tensorflow as tf
# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W*x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
结果:
W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11
有效!
但后来我改变了训练数据:
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
到:
x_train = [145, 146, 147, 148]
y_train = [151, 152, 153, 154]
我理论上应该得到W:[~1] b:[~6]损失:〜0,但我得到:
W: [ nan] b: [ nan] loss: nan
下面是经过10次训练后的i,W,b和损失的打印
[0, '0.3', '-0.3', '4.74e+04']
[1, '1276', '8.408', '1.396e+11']
[2, '-2.188e+06', '-1.494e+04', '4.111e+17']
[3, '3.755e+09', '2.563e+07', '1.211e+24']
[4, '-6.445e+12', '-4.399e+10', '3.566e+30']
[5, '1.106e+16', '7.549e+13', '1.05e+37']
[6, '-1.898e+19', '-1.296e+17', 'inf']
[7, '3.257e+22', '2.223e+20', 'inf']
[8, '-5.59e+25', '-3.816e+23', 'inf']
[9, '9.594e+28', '6.548e+26', 'inf']
有谁知道造成这种情况的原因是什么?我在Ubuntu 16.04上使用Tensorflow 1.4.0(仅限CPU)和Python 3.5.2
编辑:帮助规范化数据,谢谢!
答案 0 :(得分:1)
我建议第二。你可以尝试一下。