2016-10-12 11 views
0

Kaggle CompetionのTensorflow DNNを使用しようとしています。データは約100列のカテゴリデータ、29列の数値データ、および1列の出力です。私がしたのは、Scikitの列車テスト分割関数を使ってXとYを使ってトレーニングとテストに分割したことです。ここで、Xは "id"や予測する必要がない値のない各行のリスト、yは値予測する必要があります。Tensorflowで予測を出力する方法は?

Epoch 1 completed out of 1 loss: 1498498282.5 
Time: 1.3765859603881836 seconds 

Accuracy: 1.0 

が、私は損失が非常に高いことを認識しない、と私はテスト目的のために1つのエポックを使用しています、そして:

import tensorflow as tf 
import numpy as np 
import time 
import pickle 
with open('pickle.pickle', 'rb') as f: 
    trainX, trainy, testX, testy = pickle.load(f) 
trainX = np.array(trainX) 
trainy = np.array(trainy) 
trainy = trainy.reshape(trainy.shape[0], 1) 
testX = np.array(testX) 
testy = np.array(testy) 
print (trainX.shape) 
print (trainy.shape) 
testX = testX.reshape(testX.shape[0], 130) 
testy = testy.reshape(testy.shape[0], 1) 
print (testX.shape) 
print (testy.shape) 
n_nodes_hl1 = 256 
n_nodes_hl2 = 256 
n_nodes_hl3 = 256 

n_classes = 1 

batch_size = 100 


# Matrix = h X w 
X = tf.placeholder('float', [None, len(trainX[0])]) 
y = tf.placeholder('float') 

def model(data): 

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([trainX.shape[1], n_nodes_hl1])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])), 
         'biases':tf.Variable(tf.random_normal([n_classes]))} 

    # (input_data * weights) + biases 

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases']) 
    l1 = tf.nn.sigmoid(l1) 

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases']) 
    l2 = tf.nn.sigmoid(l2) 

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases']) 
    l3 = tf.nn.sigmoid(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return output 


def train(x): 

    pred = model(x) 
    #loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(pred, y)) 
    loss = tf.reduce_mean(tf.square(pred - y)) 
    optimizer = tf.train.AdamOptimizer(0.01).minimize(loss) 

    epochs = 1 

    with tf.Session() as sess: 
     sess.run(tf.initialize_all_variables()) 
     print ('Beginning Training \n') 
     for e in range(epochs): 
      timeS = time.time() 
      epoch_loss = 0 

      i = 0 
      while i < len(trainX): 

       start = i 
       end = i + batch_size 
       batch_x = np.array(trainX[start:end]) 
       batch_y = np.array(trainy[start:end]) 

       _, c = sess.run([optimizer, loss], feed_dict = {x: batch_x, y: batch_y}) 
       epoch_loss += c 
       i += batch_size 
      done = time.time() - timeS 
      print ('Epoch', e + 1, 'completed out of', epochs, 'loss:', epoch_loss, "\nTime:", done, 'seconds\n') 
     correct = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1)) 
     acc = tf.reduce_mean(tf.cast(correct, 'float')) 
     print("Accuracy:", acc.eval({x:testX, y:testy})) 


train(X) 

出力の1エポック:私はその後、以下に示すモデルを、構築されましたはい、私のコードはかなり面倒です。しかし、私がしたいことは、予測をプリントアウトすることだけです。どうすればいい?私はXのための機能のリストをフィードする必要があることを知っているが、私はそれを行う方法を理解していない。なぜ私の精度が1.0になっているのかも分かりません。そのことについての提案やコードを変更する方法があれば、私はどんなアイデアも聞いて嬉しいです。 ありがとうございます

答えて

4

予測を取得するには、モデルの出力を定義する操作であるpredを評価するだけです。

どうすればよいですか? pred.eval()。しかし、その予測を評価するための入力が必要なので、処理するサンプル(またはサンプル)をeval()feed_dict辞書を提供する必要があります。考え方は同じであるので、これは、acc.eval({x:testX, y:testy})と非常によく似ていますどのように

predictions = pred.eval(feed_dict = {x:testX}) 

お知らせ:

結果のコードは次のようになります。いくつかの入力を評価する必要がある操作(この場合はacc)があります。acc.eval()またはsess.run(acc)を対応するfeed_dictと入力して評価することができます。

+0

ありがとう!それは今働きます! – tgs266

1

最も簡単な方法は、(反復間)トレーニングしながら、既存のセッションを使用することであろう:X_exampleは、いくつかのnumpyの例テンソルである

print (sess.run(model, {x:X_example})) 

+0

ここにモデルは何ですか? –

+0

上部を見てください。モデルは、テンソルフロー計算用のグラフを作成する関数です。 {x:X_example}は 'feed_dict'引数であり、実際のデータをいわゆるテンソルフロー「プレースホルダ」に置きます。 – OZ13

0

以下の行は、すべてのクラスの確率スコアを示します。たとえば、3つのクラスの場合、下の行は1x3のシェイプの配列を返します。X_testあなたは次のことができます:そのために私たちは、上記のステートメントを変更しますので、

output = sess.run(pred, {x:X_test}) 

上記変数outputの最大数は予測されます:

output = sess.run(tf.argmax(pred, 1), {x:X_test}) 
print("your prediction for X_test is :", output[0]) 

その他の薄いですあなたができることは:

output = sess.run(pred, {x:X_test}) 
output = np.argmax(output) 
print("your prediction for X_test is :", output) 
関連する問題