...

/

Test the Network on the Whole Dataset

Test the Network on the Whole Dataset

Test the neural network performance based on the score of each record.

Maintain the score of each record

Let’s keep going and write the code to see how the neural network performs against the rest of the dataset, and keep a score so we can see later if our own ideas for improving the learning worked and also to compare with how well others have done this.

Before we jump into the loop that works through all the test dataset records, we’ll create an empty list called scorecard, which will be the scorecard that we update after each record.

Press + to interact
# test the neural network
# scorecard for how well the network performs, initially empty
scorecard = []
# go through all the records in the test data set
for record in test_data_list:
# split the record by the ',' commas
all_values = record.split(',')
# correct answer is first value
correct_label = int(all_values[0])
print(correct_label, "correct label")
# scale and shift the inputs
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# query the network
outputs = n.query(inputs)
# the index of the highest value corresponds to the label
label = numpy.argmax(outputs)
print(label, "network's answer")
# append correct or incorrect to list
if (label == correct_label):
# network's answer matches correct answer, add 1 to scorecard
scorecard.append(1)
else:
# network's answer doesn't match correct answer, add 0 to scorecard
scorecard.append(0)
pass
pass

Code explanation

Lines 9–16: We can see that inside the loop, we do what we did before. We split the text record by the commas to separate out the values. We keep a note of the first value as the correct ...