Reputation: 41
I am using TFLearn Alexnet sample with my own dataset.
Next I want to perform classification on test data and to determine the accuracy of the model.
model.predict()
and model.evaluate()
. model.predict()
gives prediction result for each image in the test data set. How can I use the result to get the accuracy?model.evaluate()
gives the accuracy score on the test data. Is there a way to get the accuracy for each batch as well? Upvotes: 4
Views: 4053
Reputation: 969
Accuracy from Prediction results
As stated by @Martin, the maximum value in the predictions array is the class predicted by model. you compare that class to the actual value: a match increases accuracy while mismatch decreases.
#METHOD 1
accuracy = model.evaluate(x_test, y_test)
#METHOD 2
predictions = model.predict(x_test)
accuracy = 0
for prediction, actual in zip(predictions, y_test):
predicted_class = numpy.argmax(prediction)
actual_class = numpy.argmax(actual)
if(predicted_class == actual_class):
accuracy+=1
accuracy = accuracy / len(y_test)
Upvotes: 1
Reputation: 136177
# Evaluate model
score = model.evaluate(test_x, test_y)
print('Test accuarcy: %0.4f%%' % (score[0] * 100))
# Run the model on one example
prediction = model.predict([test_x[0]])
print("Prediction: %s" % str(prediction[0][:3])) # only show first 3 probas
batch_index = 42
batch_size = 128
batch_x = test_x[batch_index * batch_size : (batch_index + 1) * batch_size]
batch_y = test_y[batch_index * batch_size : (batch_index + 1) * batch_size]
score = model.evaluate(batch_x, batch_y)
print('Batch accuarcy: %0.4f%%' % (score[0] * 100))
Upvotes: 2
Reputation: 1304
Below the responses:
model.predict()
Upvotes: 0