2017-01-02 64 views
3

我是Tensorflow和機器學習的新手。我試圖修改基本Tensorflow example以模仿批量輸入,但無法達到收斂。張量不與nx1整數輸入(列向量)收斂

如果我將x_data更改爲[0,1),它能夠正確計算W。

x_data = np.random.rand(numelements,1).astype(np.float32) 

我的代碼有什麼問題嗎?這裏是副本:

import tensorflow as tf 
import numpy as np 

# number of training samples 
numelements = 100 

# define input, and labled values 
# note the inptu and output are actually scalar value 
#x_data = np.random.rand(numelements,1).astype(np.float32) 
x_data = np.random.randint(0, 10, size=(numelements,1)).astype(np.float32) 
y_data = x_data * 10 

# Try to find values for W and b that compute y_data = W * x + b 
x = tf.placeholder(tf.float32, [None, 1]) 
W = tf.Variable(tf.zeros([1])) 
b = tf.Variable(tf.zeros([1])) 
y = tf.mul(x, W) + b 

# Minimize the mean squared errors. 
loss = tf.reduce_mean(tf.square(y - y_data)) 
optimizer = tf.train.GradientDescentOptimizer(0.5) 
train = optimizer.minimize(loss) 

# Before starting, initialize the variables. We will 'run' this first. 
init = tf.global_variables_initializer() 

# Launch the graph. 
sess = tf.Session() 
sess.run(init) 

# Fit the line. 
for step in range(81): 
sess.run(train, feed_dict={x: x_data}) 
if step % 20 == 0: 
    print(step, sess.run(W), sess.run(b)) 

回答

0

我的朋友幫我弄清楚我的漸變下降訓練率太高。使用這個post的提示,我可以清楚地看到損失越來越大,最終開始溢出。

我改變了學習率爲0.005,它開始收斂。