Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # load the mnist training data CSV file into a list
- training_data_file = open("mnist_dataset/mnist_train.csv", 'r')
- training_data_list = training_data_file.readlines()
- training_data_file.close()
- # train the neural network
- # epochs is the number of times the training data set is used for training
- epochs = 5
- for e in range(epochs):
- # go through all records in the training data set
- for record in training_data_list:
- # split the record by the ',' commas
- all_values = record.split(',')
- # scale and shift the inputs
- inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
- # create the target output values (all 0.01, except the desired label which is 0.99)
- targets = numpy.zeros(output_nodes) + 0.01
- # all_values[0] is the target label for this record
- targets[int(all_values[0])] = 0.99
- n.train(inputs, targets)
- pass
- pass
- # load the mnist test data CSV file into a list
- test_data_file = open("mnist_dataset/mnist_test.csv", 'r')
- test_data_list = test_data_file.readlines()
- test_data_file.close()
- # 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])
- # 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)
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement