{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# python notebook for Make Your Own Neural Network\n# (c) Tariq Rashid, 2016\n# license is GPLv2\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy\n# scipy.special for the sigmoid function expit()\nimport scipy.special\n# library for plotting arrays\n##import matplotlib.pyplot\n# ensure the plots are inside this notebook, not an external window\n##%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# neural network class definition\nclass neuralNetwork:\n    \n    \n    # initialise the neural network\n    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):\n        # set number of nodes in each input, hidden, output layer\n        self.inodes = inputnodes\n        self.hnodes = hiddennodes\n        self.onodes = outputnodes\n        \n        # link weight matrices, wih and who\n        # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer\n        # w11 w21\n        # w12 w22 etc \n        self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))\n        self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))\n\n        # learning rate\n        self.lr = learningrate\n        \n        # activation function is the sigmoid function\n        self.activation_function = lambda x: scipy.special.expit(x)\n        \n        pass\n\n    \n    # train the neural network\n    def train(self, inputs_list, targets_list):\n        # convert inputs list to 2d array\n        inputs = numpy.array(inputs_list, ndmin=2).T\n        targets = numpy.array(targets_list, ndmin=2).T\n        \n        # calculate signals into hidden layer\n        hidden_inputs = numpy.dot(self.wih, inputs)\n        # calculate the signals emerging from hidden layer\n        hidden_outputs = self.activation_function(hidden_inputs)\n        \n        # calculate signals into final output layer\n        final_inputs = numpy.dot(self.who, hidden_outputs)\n        # calculate the signals emerging from final output layer\n        final_outputs = self.activation_function(final_inputs)\n        \n        # output layer error is the (target - actual)\n        output_errors = targets - final_outputs\n        # hidden layer error is the output_errors, split by weights, recombined at hidden nodes\n        hidden_errors = numpy.dot(self.who.T, output_errors) \n        \n        # update the weights for the links between the hidden and output layers\n        self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))\n        \n        # update the weights for the links between the input and hidden layers\n        self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))\n        \n        pass\n\n    \n    # query the neural network\n    def query(self, inputs_list):\n        # convert inputs list to 2d array\n        inputs = numpy.array(inputs_list, ndmin=2).T\n        \n        # calculate signals into hidden layer\n        hidden_inputs = numpy.dot(self.wih, inputs)\n        # calculate the signals emerging from hidden layer\n        hidden_outputs = self.activation_function(hidden_inputs)\n        \n        # calculate signals into final output layer\n        final_inputs = numpy.dot(self.who, hidden_outputs)\n        # calculate the signals emerging from final output layer\n        final_outputs = self.activation_function(final_inputs)\n        \n        return final_outputs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# number of input, hidden and output nodes\ninput_nodes = 784\nhidden_nodes = 200\noutput_nodes = 10\n\n# learning rate\nlearning_rate = 0.2\n\n# create instance of neural network\nn = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the mnist training data CSV file into a list\ntraining_data_file = open(\"../input/train.csv\", 'r')\ntraining_data_list = training_data_file.readlines()\ntraining_data_list = training_data_list[1:]\ntraining_data_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train the neural network\n\n# epochs is the number of times the training data set is used for training\nepochs = 5\n\nfor e in range(epochs):\n    # go through all records in the training data set\n    for record in training_data_list:\n        # split the record by the ',' commas\n        all_values = record.split(',')\n        # scale and shift the inputs\n        inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n        # create the target output values (all 0.01, except the desired label which is 0.99)\n        targets = numpy.zeros(output_nodes) + 0.01\n        # all_values[0] is the target label for this record\n        targets[int(all_values[0])] = 0.99\n        n.train(inputs, targets)\n        pass\n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the mnist test data CSV file into a list\ntest_data_file = open(\"../input/train.csv\", 'r')\ntest_data_list = test_data_file.readlines()\ntest_data_list = test_data_list[1:]\ntest_data_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test the neural network\n\n# scorecard for how well the network performs, initially empty\nscorecard = []\n\n# go through all the records in the test data set\nfor record in test_data_list:\n    # split the record by the ',' commas\n    all_values = record.split(',')\n    # correct answer is first value\n    correct_label = int(all_values[0])\n    # scale and shift the inputs\n    inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n    # query the network\n    outputs = n.query(inputs)\n    # the index of the highest value corresponds to the label\n    label = numpy.argmax(outputs)\n    # append correct or incorrect to list\n    if (label == correct_label):\n        # network's answer matches correct answer, add 1 to scorecard\n        scorecard.append(1)\n    else:\n        # network's answer doesn't match correct answer, add 0 to scorecard\n        scorecard.append(0)\n        pass\n    \n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# calculate the performance score, the fraction of correct answers\nscorecard_array = numpy.asarray(scorecard)\nprint (\"performance = \", scorecard_array.sum() / scorecard_array.size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the mnist test data CSV file into a list\ntest_data_file = open(\"../input/test.csv\", 'r')\ntest_data_list2 = test_data_file.readlines()\ntest_data_list2 = test_data_list2[1:]\ntest_data_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = {}\n\nimport csv\nwith open('results.csv', 'wt') as csvfile:\n    csvwriter = csv.writer(csvfile, delimiter=',')\n    \n    csvwriter.writerow([\"ImageId\", \"Label\"])\n    # go through all the records in the test data set\n    for i in range (len(test_data_list2)):\n        record = test_data_list2[i]\n        # split the record by the ',' commas\n        all_values = record.split(',')\n        # scale and shift the inputs\n        inputs = (numpy.asfarray(all_values[0:]) / 255.0 * 0.99) + 0.01\n        # query the network\n        outputs = n.query(inputs)\n        # the index of the highest value corresponds to the label\n        label = numpy.argmax(outputs)\n        # append correct or incorrect to list\n\n        csvwriter.writerow([i+1, label])\n    \n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the mnist test data CSV file into a list\nfile = open(\"results.csv\", 'r')\nfile_list = file.readlines()\nfile_list = file_list[0:]\nfile.close()\n\nfor row in file_list:\n    print(row)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.4"}},"nbformat":4,"nbformat_minor":1}