2017-11-24 2 views
1

私は、線形回帰を実行するためにかなり単純な2層人工ニューラルネットワークを構築するためにテンソルフローライブラリを使用しています。 私の問題は、結果が予想外であるように見えることです。私は間違いを何時間も見つけようとしていますが、希望はありません。私はテンソル流とニューラルネットワークに新しいので、それは些細な間違いかもしれません。誰かが私が間違っていることを考えていることがありますか?回帰のためのTensorflowニューラルネットワーク

from __future__ import print_function 

import tensorflow as tf 
import numpy as np 
# Python optimisation variables 
learning_rate = 0.02 

data_size=100000 
data_length=100 
train_input=10* np.random.rand(data_size,data_length); 
train_label=train_input.sum(axis=1); 
train_label=np.reshape(train_label,(data_size,1)); 

test_input= np.random.rand(data_size,data_length); 
test_label=test_input.sum(axis=1); 
test_label=np.reshape(test_label,(data_size,1)); 

x = tf.placeholder(tf.float32, [data_size, data_length]) 
y = tf.placeholder(tf.float32, [data_size, 1]) 

W1 = tf.Variable(tf.random_normal([data_length, 1], stddev=0.03), name='W1') 
b1 = tf.Variable(tf.random_normal([data_size, 1]), name='b1') 

y_ = tf.add(tf.matmul(x, W1), b1) 


cost = tf.reduce_mean(tf.square(y-y_))     
optimiser=tf.train.GradientDescentOptimizer(learning_rate=learning_rate) 
.minimize(cost) 

init_op = tf.global_variables_initializer() 

correct_prediction = tf.reduce_mean(tf.square(y-y_))  
accuracy = tf.cast(correct_prediction, tf.float32) 


with tf.Session() as sess: 
    sess.run(init_op) 
    _, c = sess.run([optimiser, cost], 
        feed_dict={x:train_input , y:train_label}) 
    k=sess.run(b1) 
    print(k)     
    print(sess.run(accuracy, feed_dict={x: test_input, y: test_label})) 

ありがとうございました!

+0

あなたはどのデータを訓練していますか? RMSEとRMSEをより単純な線形回帰(おそらくscikit-learnの実装を使用)と比較してみましたか?学習率を変えてみましたか?あなたのデータを一回だけ伝えているようです(おそらく、あなたはいくつかのエポックを訓練するべきでしょうか? – valentin

答えて

1

コードにはいくつか変更が加えられています。

まず、エポック数のトレーニングを実行し、オプティマイザのトレーニングデータをバッチでフィードする必要があります。あなたの学習率は非常に高かった。バイアスは、密集した(完全に接続された)層ごとに1つの入力だけであると想定されます。コスト(ロス)値をプロットして、ネットワークがどのように収束しているかを確認することができます。 データをバッチでフィードするために、プレースホルダでも変更を加えました。

from __future__ import print_function 

import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt 
# Python optimisation variables 
learning_rate = 0.001 

data_size=1000 # Had to change these value to fit in my memory 
data_length=10 
train_input=10* np.random.rand(data_size,data_length); 
train_label=train_input.sum(axis=1); 
train_label=np.reshape(train_label,(data_size,1)); 

test_input= np.random.rand(data_size,data_length); 
test_label=test_input.sum(axis=1); 
test_label=np.reshape(test_label,(data_size,1)); 

tf.reset_default_graph() 
x = tf.placeholder(tf.float32, [None, data_length]) 
y = tf.placeholder(tf.float32, [None, 1]) 

W1 = tf.Variable(tf.random_normal([data_length, 1], stddev=0.03), name='W1') 
b1 = tf.Variable(tf.random_normal([1, 1]), name='b1') 

y_ = tf.add(tf.matmul(x, W1), b1) 


cost = tf.reduce_mean(tf.square(y-y_))     
optimiser=tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) 

init_op = tf.global_variables_initializer() 

EPOCHS = 500 
BATCH_SIZE = 32 
with tf.Session() as sess: 
    sess.run(init_op) 

    loss_history = [] 
    for epoch_no in range(EPOCHS): 
     for offset in range(0, data_size, BATCH_SIZE): 
      batch_x = train_input[offset: offset + BATCH_SIZE] 
      batch_y = train_label[offset: offset + BATCH_SIZE] 

      _, c = sess.run([optimiser, cost], 
        feed_dict={x:batch_x , y:batch_y}) 
      loss_history.append(c) 


    plt.plot(range(len(loss_history)), loss_history) 
    plt.show() 

    # For running test dataset 
    results, test_cost = sess.run([y_, cost], feed_dict={x: test_input, y: test_label}) 
    print('test cost: {:.3f}'.format(test_cost)) 
    for t1, t2 in zip(results, test_label): 
     print('Prediction: {:.3f}, actual: {:.3f}'.format(t1[0], t2[0])) 
関連する問題