2016-12-06 52 views
0

我想解決Titanic Problem on Kaggle,我不確定如何獲得給定測試數據的輸出。TensorFlow - 無法獲得預測

我成功地訓練網絡,並調用該方法make_prediction(x, test_x)

x = tf.placeholder('float', [None, ip_features]) 
... 
def make_prediction(x, test_data): 
    with tf.Session() as sess : 
    sess.run(tf.global_variables_initializer()) 
    prediction = sess.run(y, feed_dict={x: test_data}) 
    return prediction 


我不知道如何在這種情況下test_data傳遞np.array找回一個np.array其中包含預測0/1

Link to Full Code

回答

1

我把你的train_neural_networkmake_prediction功能合併爲一個單一功能。將tf.nn.softmax應用於模型函數將使得數值範圍從0〜1(解釋爲概率),然後tf.argmax以較高的概率提取列數。請注意,在這種情況下yplaceholder需要進行一次熱編碼。 (如果你不是編碼一個熱-Y這裏,然後pred_y=tf.round(tf.nn.softmax(model))會的softmax輸出轉換爲0或1)

def train_neural_network_and_make_prediction(train_X, test_X): 

    model = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 
    pred_y=tf.argmax(tf.nn.softmax(model),1) 

    ephocs = 10 

    with tf.Session() as sess : 
     tf.initialize_all_variables().run() 
     for epoch in range(ephocs): 
      epoch_cost = 0 

      i = 0 
      while i< len(titanic_train) : 
       start = i 
       end = i+batch_size 
       batch_x = np.array(train_x[start:end]) 
       batch_y = np.array(train_y[start:end]) 

       _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y}) 
       epoch_cost += c 
       i+=batch_size 
      print("Epoch",epoch+1,"completed with a cost of", epoch_cost) 
     # make predictions on test data 
     predictions = pred_y.eval(feed_dict={x : test_X}) 
    return predictions 
+0

謝謝你這麼多'pred_y = tf.round(tf.nn.softmax(模型))'是我一直在尋找:) –