Test the Network on the Whole Dataset
Test the neural network performance based on the score of each record.
We'll cover the following...
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 emptyscorecard = []# go through all the records in the test data setfor record in test_data_list:# split the record by the ',' commasall_values = record.split(',')# correct answer is first valuecorrect_label = int(all_values[0])print(correct_label, "correct label")# scale and shift the inputsinputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01# query the networkoutputs = n.query(inputs)# the index of the highest value corresponds to the labellabel = numpy.argmax(outputs)print(label, "network's answer")# append correct or incorrect to listif (label == correct_label):# network's answer matches correct answer, add 1 to scorecardscorecard.append(1)else:# network's answer doesn't match correct answer, add 0 to scorecardscorecard.append(0)passpass
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 ...