Reshma NirmalKumar
Reshma NirmalKumar

Reputation: 61

How accuracy calculated for train data is used for evaluating test data in tensorflow

In tensorflow the model is created with training data but i want to know how is the test data are evaluated with accuracy calculated with training data.

     correct_prediction = tf.equal(tf.argmax(pred, 1),tf.argmax(y_train, 1))
     # Calculate accuracy
     accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
     print "Accuracy:", accuracy.eval({x1: X_test, y1: y_test})

correct_prediction is done with training data and accuracy.eval() is used to pass test data set. Please explain how this is done.

Upvotes: 0

Views: 2278

Answers (1)

Olivier Moindrot
Olivier Moindrot

Reputation: 28198

In TensorFlow you create a graph, where data flows between nodes, from inputs to outputs.

Let's take a simple example, MNIST. Here are the TensorFlow nodes:

  • inputs:
    • batch of 28x28 images: images of shape [batch_size, 28, 28, 1]
    • label of the image: labels of shape [batch_size, 10]
  • outputs:
    • predictions of your network pred, of shape [batch_size, 10]
    • accuracy of the model: accuracy, shape [] (scalar)

Here is the graph:

         images
           |
         (CNN)
           |
  labels  pred
       \   |
        \  |
  correct_prediction
           |
        accuracy

The two inputs nodes are tf.placeholder, which means that you need to manually enter values in them.

Depending on whether you are training or testing the model, you can feed:

  • for training: feed images and labels with two numpy arrays X_train and y_train of corresponding shape, from your data
  • for testing: feed images and labels with two numpy arrays X_test and y_test which are the test data

With this architecture, the same graph is used both for training and testing.


The difference is that:

  • during training, you will likely run the train operation sess.run(train_op, feed_dict={images: X_train, labels: y_train})
  • during testing, you no longer train the model and just compute the accuracy: sess.run(accuracy, feed_dict={images: X_test, labels: y_test})

Upvotes: 2

Related Questions