DollarAkshay
DollarAkshay

Reputation: 2122

TensorFlow - Unable to get Prediction

I am trying to solve the Titanic Problem on Kaggle and I am unsure of how to get the output for a given test data.

I successfully train the network and call the method 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


I am not sure how to pass a np.array in this case test_data to get back a np.array which contains the prediction 0/1

Link to Full Code

Upvotes: 1

Views: 918

Answers (1)

Zhongyu Kuang
Zhongyu Kuang

Reputation: 5354

I combined your train_neural_network and make_prediction function into one single function. Applying tf.nn.softmax to the model function would make the value range into from 0~1 (interpreted as probability), then tf.argmax extracts the column number with the higher probability. Note that the placeholder for y in this case needs to be one-hot-encoded. (If you are not one-hot-encoding y here, then pred_y=tf.round(tf.nn.softmax(model)) would convert the output of softmax into 0 or 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

Upvotes: 1

Related Questions