import tensorflow as tf
# Model parameters
A = tf.Variable([.3], dtype=tf.float32)
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
q_model = A * (x**2) + W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(q_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [0, 1, 2, 3, 4]
y_train = [0, 1, 4, 9, 16]
# 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_A, curr_W, curr_b, curr_loss = sess.run([A, W, b, loss], {x: x_train, y: y_train})
print("A: %s W: %s b: %s loss: %s"%(curr_A, curr_W, curr_b, curr_loss))
在他们的网站上,tf给出了执行线性回归的模型代码。但是,我想玩,看看我是否也可以让它做二次回归。为此,我添加了一个tf.Variable A,将其放入模型中,然后修改输出以告诉我它的值是什么。
结果如下:
A: [ nan] W: [ nan] b: [ nan] loss: nan
你们认为这里的问题是什么?它在椅子和键盘之间吗?
答案 0 :(得分:1)
如果您为每次迭代打印A
,W
和b
的值,您会看到它们是交替的(即正值和负值彼此相继)。这通常是由于学习率很高。在您的示例中,您应该能够通过将学习率降低到大约0.001
来修复此行为:
optimizer = tf.train.GradientDescentOptimizer(0.001)
凭借此学习率,我实现了减少的损失,而A
趋于1,而W
和b
趋于零,正如预期的那样。
A: [ 0.7536] W: [ 0.42800003] b: [-0.26100001] loss: 7.86113
A: [ 0.8581112] W: [ 0.45682004] b: [-0.252166] loss: 0.584708
A: [ 0.88233441] W: [ 0.46283191] b: [-0.25026742] loss: 0.199126
...
A: [ 0.96852171] W: [ 0.1454313] b: [-0.11387932] loss: 0.0183883
A: [ 0.96855479] W: [ 0.14527865] b: [-0.11376046] loss: 0.0183499
A: [ 0.96858788] W: [ 0.14512616] b: [-0.11364172] loss: 0.0183113
A: [ 0.9686209] W: [ 0.14497384] b: [-0.1135231] loss: 0.0182731