1

I have trained my models by calling the 'train_neural_network' function which trains the model and I store the model, the accuracy comes to around 83%, the problem I'm facing is how do I make predictions using my saved model? Which variable to restore and how to pass the input(in batches or whole at once)?

def make_model(data,train_x):

    n_nodes_hl1 = 2000
    n_nodes_hl2 = 2000
    n_nodes_hl3 = 2000

    n_classes = 2 # No of classification

    hidden_1_layer = {'weights': tf.Variable(tf.truncated_normal([len(train_x[0]), n_nodes_hl1], stddev=0.1),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl1]),name = 'biases')}

    hidden_2_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl1, n_nodes_hl2], stddev=0.1),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl2]),name = 'biases')}

    hidden_3_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl2, n_nodes_hl3], stddev=0.1,),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl3]),name = 'biases')}

    output_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl3, n_classes], stddev=0.1),name= 'weights'),
                    'biases': tf.Variable(tf.constant(0.1, shape=[n_classes]),name = 'biases'), }


    layer_1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    # now goes through an activation function - sigmoid function
    layer_1 = tf.nn.relu(layer_1)
    print ("Layer 1 done!!")
    # input for layer 2 = result of activ_func for layer 1
    layer_2 = tf.add(tf.matmul(layer_1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    layer_2 = tf.nn.relu(layer_2)
    print ("Layer 2 done!!")

    layer_3 = tf.add(tf.matmul(layer_2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    layer_3 = tf.nn.relu(layer_3)
    print ("Layer 3 done!!")

    output = tf.matmul(layer_3, output_layer['weights'],name = "output") + output_layer['biases']

    return output



def train_neural_network(train_x,train_y,test_x,test_y):
    tf.reset_default_graph()
    with tf.name_scope('input'):
        x = tf.placeholder('float', [None, len(train_x[0])],name= 'x_input')
        y = tf.placeholder('float',name = 'y-input')
    # Merge all the summaries and write them out to /tmp/mnist_logs (by default)


    prediction = make_model(x,train_x)
    print ('model ready!!')
    with tf.name_scope('pred'):
        pred = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)
    with tf.name_scope('cost'):
        cost = tf.reduce_mean(pred)
    with tf.name_scope('train'):
        optimizer = tf.train.AdamOptimizer().minimize(cost,name = 'optimizer')


    tf.summary.scalar("cost", cost)




    n_epochs = 10
    batch_size = 100


    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())  # initializes our variables. Session has now begun.
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter('train/2/',
                                              sess.graph)
        test_writer = tf.summary.FileWriter('test/')

        for epoch in range(n_epochs):
            epoch_loss = 0  # we'll calculate the loss as we go

            i = 0
            while i < len(train_x):
                #we want to take batches(chunks); take a slice, then another size)
                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})
                if i%200 == 0:
                    train_writer.add_summary(_, i)
                epoch_loss += c
                i+=batch_size
            print('Epoch', epoch, 'completed out of', n_epochs, 'loss:', epoch_loss)
            with tf.name_scope('accuracy'):
                correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
                accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
                tf.summary.scalar("accuracy", accuracy)


            print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))
            saver = tf.train.Saver()
            tf_log = 'tf.log'
            saver.save(sess, "model3.ckpt")

    return accuracy

This is my model This is how I am making predictions, but this fails everytime:

def test_neural_network(test_x):

    batch_size = 100
    i = 0
    batch_x = np.array(test_x[i:i+batch_size])
    tf.reset_default_graph()
    x = tf.placeholder('float', [len(batch_x),len(test_x[0])])
    y = tf.placeholder('float',[2])
    prediction = make_model(x,batch_x)
    # pred1 = tf.nn.softmax(logits=prediction)
    # weight = tf.get_variable("weights_3", shape=[len(batch_x),2],initializer = tf.zeros_initializer)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, "model3.ckpt")
        p = tf.argmax(prediction,1)
        print (p.eval({x:batch_x))

gives and array of shape(batch_size,2),expected values [0,1] or [1,0] but getting decimal values.

  • *How* does it fail? Provide a full problem description per the posting guidelines: [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Prune Oct 23 '17 at 18:39

2 Answers2

0

You have a problem because you launch a session on your variable « weight ». But in your case you want to know the output of your network. Try to launch a session on your last layer

JoBibu
  • 9
  • 2
  • which tensor do i use? can you specify the variable name from the graph?Two more tensors are saved in my graph after weights_3 which are weights_3/Adam, weights_3/Adam_1, which to use? – Subham Kapoor Oct 23 '17 at 19:35
  • So on your test_neural_network : First create an empty network by calling make_model. Then call your saver to restore all your weights learned on your train phase and after that you can call the prediction on the output returned by your function make_model :) – JoBibu Oct 23 '17 at 19:48
  • What parameters to pass while making an empty model? – Subham Kapoor Oct 23 '17 at 20:15
  • You must pass tensor which respect the shape of your variable data and train_x – JoBibu Oct 23 '17 at 20:32
  • (The shape you used during training) – JoBibu Oct 23 '17 at 20:33
  • have done the above but not getting desired output, see the changes above. – Subham Kapoor Oct 23 '17 at 21:34
0

How do I make predictions using my saved model? Which variable to restore and how to pass the input (in batches or whole at once)?

Several comments regarding your design. You don't have to rebuild the graph at test time, because it's saved right next to the session checkpoint. Take a look at this question.

With this, your code will be simplified a lot, because you don't have to keep the placeholders and cross-entropy loss function separately. Add the name to the softmax layer like this:

with tf.name_scope('pred'):
  pred = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y, name='softmax')

After you've restored the graph, you can find the target operation by:

graph = sess.graph
pred = graph.get_operation_by_name("pred/softmax")

If your test data is not big, you can freely feed all of it at once, but if it's significantly larger than your batch size, you can easily get out-of-memory. In this case, you should use mini-batches for testing as well.

As for your test accuracy, there can be plenty reasons for this, for instance, overfitting. Update the question with the full code, so that it could be reproduced.

Maxim
  • 52,561
  • 27
  • 155
  • 209