2017-06-30 10 views
0

私は、生成されたデータを使用してTensorflowで線形回帰モデルを訓練しようとしています。モデルは線の傾きを学習しているように見えますが、偏りを知ることはできません。線形回帰モデルテンソルフローではバイアスを学習できません

私は番号を変更しようとしました。 (傾き)、偏りなどの情報が含まれますが、たびにモデルによる学習バイアスはゼロになります。私はどこに間違っているのか分かりませんし、助けていただければ幸いです。

ここにコードがあります。

import numpy as np 
import tensorflow as tf 

# assume the linear model to be Y = W*X + b 
X = tf.placeholder(tf.float32, [None, 1]) 
Y = tf.placeholder(tf.float32, [None,1]) 
# the weight and biases 
W = tf.Variable(tf.zeros([1,1])) 
b = tf.Variable(tf.zeros([1])) 
# the model 
prediction = tf.matmul(X,W) + b 
# the cost function 
cost = tf.reduce_mean(tf.square(Y - prediction)) 
# Use gradient descent 

learning_rate = 0.000001 
train_step = 
tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 
sess = tf.Session() 
init = tf.global_variables_initializer() 
sess.run(init) 
steps = 1000 
epochs = 10 
Verbose = False 
# In the end, the model should learn these values 
test_w = 3 
bias = 10 

for _ in xrange(epochs): 
    for i in xrange(steps): 
    #  make fake data for the model 
    #  feed one example at a time 
#  stochastic gradient descent, because we only use one example at a time 
     x_temp = np.array([[i]]) 
     y_temp = np.array([[test_w*i + bias]]) 
    #  train the model using the data 
     feed_dict = {X: x_temp, Y:y_temp} 
     sess.run(train_step,feed_dict=feed_dict) 
     if Verbose and i%100 == 0: 
      print("Iteration No: %d" %i) 
      print("W = %f" % sess.run(W)) 
      print("b = %f" % sess.run(b)) 

print("Finally:") 
print("W = %f" % sess.run(W)) 
print("b = %f" % sess.run(b)) 
# These values should be close to the values we used to generate data 

https://github.com/HarshdeepGupta/tensorflow_notebooks/blob/master/Linear%20Regression.ipynb

出力は、コードの最後の行です。 モデルはtest_wとバイアスを学習する必要があります(ノートブックリンクでは、最初のコメントの後に3番目のセルにあります)。それぞれ3と10に設定されています。

モデルは正しく重量(傾き)を学習しますが、偏りを知ることはできません。エラーはどこですか?

+0

質問にコードが含まれています。ありがとう –

答えて

0

主な問題は、一度に1つのサンプルだけをモデルに供給していることです。これはあなたのオプティマイザを非常に不潔にします。そのため、あなたはそのような小さな学習率を使用しなければなりません。私は各ステップでより多くのサンプルを供給するように提案します。

一度に1つのサンプルを供給することを主張する場合は、tf.train.AdamOptimizer(learning_rate)のような勢いでオプティマイザを使用することを検討してください。こうすることで、学習率を上げてコンバージェンスに達することができます。

関連する問題