0

I have trained a neural network with 100% of my training data set. Now I want to test the network with a new data set not included in the original data set.

My code is given here...

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from scipy.io import loadmat
%matplotlib inline
import tensorflow as tf
from tensorflow.contrib import learn

import sklearn
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from warnings import filterwarnings
filterwarnings('ignore')
sns.set_style('white')
from sklearn import datasets
from sklearn.preprocessing import scale
from sklearn.cross_validation import train_test_split
from sklearn.datasets import make_moons

X = np.array(loadmat("Data/DataIn.mat")['TrainingDataIn'])
Y = np.array(loadmat("Data/DataOut.mat")['TrainingDataOut'])

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=1, random_state=42)
total_len = X_train.shape[0]
# Parameters
learning_rate = 0.001
training_epochs = 2500
batch_size = 100
display_step = 1
dropout_rate = 0.9
# Network Parameters
n_hidden_1 = 19 # 1st layer number of features
n_hidden_2 = 26 # 2nd layer number of features
n_input = X_train.shape[1]
n_classes = 1

# tf Graph input
X = tf.placeholder("float32", [None, 37])
Y = tf.placeholder("float32", [None, 1])

def multilayer_perceptron(X, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)

    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

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


# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 0.1)),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], 0, 0.1))
}

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

# Construct model
pred = multilayer_perceptron(X, weights, biases)
tf.shape(pred)
tf.shape(Y)
print("Prediction matrix:", pred)
print("Output matrix:", Y)

# Define loss and optimizer
cost = tf.reduce_mean(tf.square(pred-Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Launch the graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(total_len/batch_size)
        print(total_batch)
        # Loop over all batches
        for i in range(total_batch-1):
            batch_x = X_train[i*batch_size:(i+1)*batch_size]
            batch_y = Y_train[i*batch_size:(i+1)*batch_size]
            # Run optimization op (backprop) and cost op (to get 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

        # sample prediction
        label_value = batch_y
        estimate = p
        err = label_value-estimate
        print ("num batch:", total_batch)
        print ("num training samples", total_len)


        # Display logs per epoch step
        if epoch % display_step == 0:
            print ("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
            print ("[*]----------------------------")
            for i in range(10):
                print ("label value:", label_value[i], \
                    "estimated value:", estimate[i])
            print ("[*]============================")

    print ("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(pred), tf.argmax(Y))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print ("Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

The results are here...

    Epoch: 2500 cost= 43.952847526
[*]----------------------------
label value: [120] estimated value: [ 123.91127777]
label value: [120] estimated value: [ 119.02476501]
label value: [200] estimated value: [ 204.662323]
label value: [120] estimated value: [ 124.79893494]
label value: [60] estimated value: [ 62.79090881]
label value: [20] estimated value: [ 18.09486198]
label value: [200] estimated value: [ 203.56544495]
label value: [20] estimated value: [ 17.48654938]
label value: [20] estimated value: [ 21.10329819]
label value: [60] estimated value: [ 60.81886673]
[*]============================
Optimization Finished!
Accuracy: 1.0

As you can see in used 100% data i.e. test_size=1. Lets say I have a new data set X_new and Y_new, how do I call the trained model to test the new data set?

Bright
  • 23
  • 1
  • 5

2 Answers2

2

OK. You need to save your modle and variables values by: Put your content in . You'll need to 'import os'.

NN_name= <Name of model>
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    <Train model>
    file_path= './'+ NN_name + '/'
    if not os.path.exists(file_path):
        os.mkdir(file_path)
    saver = tf.train.Saver()
    saver.save(sess, file_path+ 'model.checkpoint')
    print('Model saved')

Then load it and test:

NN_name= <Name of model>
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    file_path = './' + NN_name + '/'
    saver = tf.train.Saver()
    saver.restore(sess, file_path+ 'model.checkpoint')
    print('Model loaded')
    <Sess run model accuracy on test dataset>

Note the configuration of the models variables cannot change between the saves and loads (other features can).

Previous answer: You need to feed your test data in and re-run your accuracy metrics.

At the end of the code add:

_, c, p = sess.run([optimizer, cost, pred], feed_dict={X: X_new ,
                                                       Y: Y_new})
correct_prediction = tf.equal(tf.argmax(pred), tf.argmax(Y))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

Make sure it is still in the computation graph (ie. still indented).

Alternatively, see how to save the parameters weights (https://www.tensorflow.org/programmers_guide/variables) which will enable to save paramters/ weights values, close the graph and load it for the test or any other prediction.

James Shiztar
  • 488
  • 5
  • 10
  • @ James Shiztar thanks for your comment. What I actually mean is given a new data set X_new, how do I make predictions from this data set from my trained model? – Bright Aug 02 '17 at 15:57
0

One simple line:

test_prediction = sess.run(pred, feed_dict={X: batch_test_x})

I assume that you load your data in the same way and have it in a variable called batch_test_x.

Let me know if it works!

rmeertens
  • 4,383
  • 3
  • 17
  • 42