2017-01-19 15 views
1

私は基本的なMLPを実装し、私はそれがデータのユーザー生成のセットを予測したいのですが、次のように予測が見えます:TensorFlowニューラルネットの出力の線形関数

prediction

、様々なアーキテクチャ(より多くの層、より少ない層、...私は隠れ層の非直線性を持っている理由私はわからない、と私は、複数のアクティベーション(ReLUtanhsigmoid)を試してみました異なるoptimisers、異なる学習速度を試してみましたドロップアウト)、しかし私はこの権利を得たことはありません。

正しくない可能性があるので、最後に予測(pred = sess.run(out, feed_dict={inputs:X.reshape(n_input, 1)}))を計算する方法が原因である可能性がありますが、その理由はわかりません。私はまた、w = sess.run(weights)で重みを抽出し、入力と共にmodel()関数にそれらを供給するような他の方法を試みましたが、何も働いていませんでした。

また、エラーを監視する場合、エラーはエポック間で減少します。

アイデア?

import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt 

# Architecture 
input_size = 1 
output_size = 1 
h1_size = 20 
h2_size = 50 

# 2 hidden layers network 
def model(inputs, weights): 
    out1 = tf.nn.relu(tf.matmul(inputs, weights['h1'])) 
    out2 = tf.nn.relu(tf.matmul(out1, weights['h2'])) 
    return tf.matmul(out2, weights['h3']) 

# Inputs/label placeholders 
inputs = tf.placeholder('float', shape=(None, input_size)) 
labels = tf.placeholder('float', shape=(None, output_size)) 

# Learnable weights 
weights = { 
    'h1': tf.Variable(tf.random_normal(shape=(input_size, h1_size))), 
    'h2': tf.Variable(tf.random_normal(shape=(h1_size, h2_size))), 
    'h3': tf.Variable(tf.random_normal(shape=(h2_size, output_size))), 
} 

# Stores the result from the net 
out = model(inputs, weights) 

# Cost and optimisation 
cost = tf.reduce_mean(tf.square(out - labels)) 
opt = tf.train.AdadeltaOptimizer() 
opt_operation = opt.minimize(cost) 


# Generate some data 
n_input = 1000 

X = np.linspace(0, 1, n_input).astype('f') 
y = X + 5 * np.sin(X * 10) 
y /= max(y) 

# Train 
epochs = 2000 
lr = 0.0000001 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 

    for epoch in range(epochs):   
     _, c = sess.run([opt_operation, cost], feed_dict={ 
      inputs: X.reshape(n_input, 1), 
      labels: y.reshape(n_input, 1), 
     }) 

     if not epoch % int(epochs/20):  
      print(c) 

    pred = sess.run(out, feed_dict={inputs:X.reshape(n_input, 1)}) 
    plt.scatter(X, pred, color='red', label='prediction')  
    plt.scatter(X, y, label='data') 
    plt.legend() 
    plt.show() 

答えて

0

はバイアス項をお忘れ:new graph

、これはそれを修正した場合にそれが今ではなく、必ず動作しますか?

新しいコードは使用しています:

weights = { 
    'h1': tf.Variable(tf.random_normal(shape=(input_size, h1_size))), 
    'h2': tf.Variable(tf.random_normal(shape=(h1_size, h2_size))), 
    'h3': tf.Variable(tf.random_normal(shape=(h2_size, output_size))), 

    'b1': tf.Variable(tf.zeros(shape=[1])), 
    'b2': tf.Variable(tf.zeros(shape=[1])), 
    'b3': tf.Variable(tf.zeros(shape=[1])), 
} 

def model(inputs, weights): 
    out1 = tf.nn.relu(tf.matmul(inputs, weights['h1']) + weights['b1']) 
    out2 = tf.nn.relu(tf.matmul(out1, weights['h2']) + weights['b2']) 
    return tf.matmul(out2, weights['h3'] + weights['b3']) 
関連する問題