2017-08-24 99 views
0
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,將其放入模型中,然後修改輸出以告訴我它的值是多少。所有的Tensorflow輸出都是nan

下面是結果:

A: [ nan] W: [ nan] b: [ nan] loss: nan 

什麼你們都以爲是這裏的問題?它在椅子和鍵盤之間嗎?

回答

0

如果您爲每次迭代打印A,Wb的值,則會看到它們是交替的(即正值和負值相互跟隨)。這通常是由於學習率很高。在你的榜樣,你應該能夠通過減少學習率約0.001來解決此問題:

optimizer = tf.train.GradientDescentOptimizer(0.001) 

有了這個學習率,我實現了減少損失,而A傾向於1和Wb趨於如預期的那樣爲零。

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