Python/Tensorflow - I have trained the convolutional neural network, how to test it?

北城以北 提交于 2019-12-06 02:59:23
Miriam Farber

General discussion

In general, in order to test neural network, you need to take new labeled data that you did not use for training, apply the network on this data (that is, apply the feed forward process), and evaluate the accuracy of the result (in comparison to the labels that you know to be true).

If you don't have such new data (that is, if you used all your data for training) and you cannot produce new data, I would suggest to take your training data, separate it to training and testing, and rerun your training procedure on the training data from the beginning. It is important that the test data would be an unused data in order to be able to evaluate the performance of your model.

Evaluating accuracy

Now, assuming you are talking about the network from this question, You can do something like that to measure the accuracy of your test data:

accuracy_test = sess.run(accuracy, feed_dict={x: test_data, y: test_onehot_vals})

where test_data and test_onehot_vals are your test pictures (and corresponding labels).

Recall that for training you run the following:

_, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals})

Note that I did not use train_op in the evaluation of accuracy_test. This is due to the fact that when you test your performance, you do not optimize weights or anything like that (which train_op does). You just apply the network that you currently have.

Getting the labels that the network produced for test data

Finally, if you want the actual labels of your test data, you need to get the value of tf.argmax(model_op, 1). So you can set it into a separate variable, for example right above the line

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))

You can do:

res_model=tf.argmax(model_op, 1)
correct_pred = tf.equal(res_model, tf.argmax(y,1))

and then evaluate it together with accuracy_test as follows:

res, accuracy_test = sess.run([res_model,accuracy], feed_dict={x: test_data, y: test_onehot_vals}).

Applying the network on unlabeled data

After you finished testing the network, and assuming you are satisfied with the results, you can move on and apply the network on new and unlabeled data. For example by doing

res_new = sess.run(res_model, feed_dict={x: new_data}).

Note that in order to produce res_model (which basically means just applying the network on the input) you don't need any labels, so you don't need y values in your feed_dict. res_new will be the new labels.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!