2017-02-23 6 views
1

朝、私はTensorFlowでいくつかの進歩を遂げていますが、それでもまだ苦労しています。私は、多くのオンラインサンプルが実行されないことを発見しています。TensorFlowアルゴリズムによる回帰のデバッグ - Python

とにかく、私は、回帰問題を解決するために多層ニューラルネットワークを適用するためにいくつかのコードを書こうとしました。しかし、私はゼロを出すだけです。誰も私のコードと私の理解の中で私が間違っている場所を見つけるのを助けることができますか?

私は、Windows 10でのPython 3.5で

をTensorflow 0.12を実行している多くの感謝

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

# Parameters 
learning_rate = 0.001 
training_epochs = 100 
batch_size = 5000 

# Network Parameters 
n_hidden_1 = 256 
n_hidden_2 = 10 
n_input = 9 
n_classes = 1 
n_samples = dataVar.shape[0] 

# TensorFlow Graph Input 
x = tf.placeholder("float", [None, n_input]) 
if (n_classes > 1): 
    y = tf.placeholder("float", [None, n_classes]) 
else: 
    y = tf.placeholder("float", [None,]) 

# Create Multilayer Model 
def multilayer_perceptron(x, weights, biases): 
    ''' 
    x: Place holder for data input 
    weights: Dictionary of weights 
    biases: Dictionary of biases 
    ''' 

    # First hidden layer with RELU activation 
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) 
    layer_1 = tf.nn.relu(layer_1) 

    # Second hidden layer with RELU activation 
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) 
    layer_2 = tf.nn.relu(layer_2) 

    # Last output layer with linear activation 
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] 
    return out_layer 

# weights and biases 
weights = { 
     'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 
     'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 
     'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes])) 
} 

biases = { 
     'b1' : tf.Variable(tf.random_normal([n_hidden_1])), 
     'b2': tf.Variable(tf.random_normal([n_hidden_2])), 
     'out': tf.Variable(tf.random_normal([n_classes])) 
} 

# Construct Model 
pred = multilayer_perceptron(x, weights, biases) 


# Define loss and optimizer 
cost = tf.reduce_mean(tf.square(pred - y)) 
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) 
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost) 

# Initialize variables 
init = tf.initialize_all_variables() 

# RUNNING THE SESSION 

# launch the session 
sess = tf.InteractiveSession() 


# Initialize all the variables 
sess.run(init) 

# Training Epochs 
for epoch in range(training_epochs): 
    # Start with cost = 0 
    avg_cost = 0.0 

    # Convert total number of batches to integer 
    total_batch = int(n_samples/batch_size) 

    # Loop over all batches 
    for i in range(total_batch): 
     # Grab the next batch of training data and labels 
     ind = np.random.randint(0, high=dataVar_scaled.shape[0], size=(batch_size)) 
     batch_x = dataVar_scaled[ind,:] 
     batch_y = depth[ind] 

     # Feed dictionary for optimization and loss value 
     _, c,p = sess.run([optimizer, cost,pred], feed_dict={x: batch_x, y: batch_y}) 

     # Compute average loss 
     avg_cost += c/total_batch 

    print("Epoch: {} cost = {:.4f}".format(epoch+1, avg_cost)) 

print("Model has completed {} Epochs of training".format(training_epochs)) 


prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval() 
print(prediction) 
plt.plot(prediction,depth,'b.') 

答えて

0

私は回帰によって確認していない、あなたは、線形回帰やロジスティック回帰を意味しました。しかし、あなたのコードを見れば、私は線形回帰を意味すると信じています。

それは線形回帰である場合には、あなたの問題は、このコード行である、:

prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval()

あなたはあなたのコード内の現在価値(tf.argmax()使用)の最大値の位置を取っています。 n_classes = 1以降、あなたの出力には1つのクラスしか存在しません。常にゼロの値を得ているのは常に最初です。 その行を

prediction = sess.run(pred, feed_dict={x:dataVar_scaled})に変更してください。

これで、最大値の位置ではなく出力の値が取得されます。

ロジスティック回帰の場合は、明らかに1つのクラスしか持たないことになります。出力が1つのクラスとしてのみである場合、どのように分類できますか?

希望すると、これが役立ちます。

+0

お返事ありがとうございます。申し訳ありません、はい、私は線形回帰を意味しました。私はそのコード行を変更しましたが、私はまだ全ての入力を同じ出力にしています。私は間違っている何か他にありますか? – jlt199

+0

私はあなたのコードを私の変更と私自身の入力で走らせました。それは私に非ゼロ値を与えました。私はあなたの 'dataVar'値を持っていませんでした。だから私は確信しています、その変数の何かが間違っています。それらがすべてゼロでないことを確認します。それがあなたを助けてくれたら、私の答えを受け入れてください。 – user1190882

+0

お時間をいただきありがとうございます。私は自分の入力データが問題だとは思わないが、二重チェックするだろう – jlt199

関連する問題