|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 37, |
| 6 | + "metadata": { |
| 7 | + "collapsed": true |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "import torch\n", |
| 12 | + "from torch.autograd import Variable\n", |
| 13 | + "import torch.nn as nn\n", |
| 14 | + "\n", |
| 15 | + "import numpy" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 38, |
| 21 | + "metadata": { |
| 22 | + "collapsed": true |
| 23 | + }, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "class NeuralNetwork(nn.Module):\n", |
| 27 | + "\n", |
| 28 | + " def __init__(self, inodes, hnodes, onodes, learning_rate):\n", |
| 29 | + " # call the base class's initialisation too\n", |
| 30 | + " super().__init__()\n", |
| 31 | + " \n", |
| 32 | + " # dimensions\n", |
| 33 | + " self.inodes = inodes\n", |
| 34 | + " self.hnodes = hnodes\n", |
| 35 | + " self.onodes = onodes\n", |
| 36 | + " \n", |
| 37 | + " # learning rate\n", |
| 38 | + " self.lr = learning_rate\n", |
| 39 | + " \n", |
| 40 | + " # define the layers and their sizes, turn off bias\n", |
| 41 | + " self.linear_ih = nn.Linear(inodes, hnodes, bias=False)\n", |
| 42 | + " self.linear_ho = nn.Linear(hnodes, onodes, bias=False)\n", |
| 43 | + " \n", |
| 44 | + " # define activation function\n", |
| 45 | + " self.activation = nn.Sigmoid()\n", |
| 46 | + " \n", |
| 47 | + " # create error function\n", |
| 48 | + " self.error_function = torch.nn.MSELoss(size_average=False)\n", |
| 49 | + "\n", |
| 50 | + " # create optimiser, using simple stochastic gradient descent\n", |
| 51 | + " self.optimiser = torch.optim.SGD(self.parameters(), self.lr)\n", |
| 52 | + "\n", |
| 53 | + " pass\n", |
| 54 | + "\n", |
| 55 | + " \n", |
| 56 | + " def forward(self, inputs_list):\n", |
| 57 | + " # convert list to a 2-D FloatTensor then wrap in Variable \n", |
| 58 | + " # also shift to GPU, remove .cuda. if not desired\n", |
| 59 | + " # inputs = Variable(torch.cuda.FloatTensor(inputs_list).view(1, self.inodes))\n", |
| 60 | + " inputs = Variable(torch.FloatTensor(inputs_list).view(1, self.inodes))\n", |
| 61 | + " \n", |
| 62 | + " # combine input layer signals into hidden layer\n", |
| 63 | + " hidden_inputs = self.linear_ih(inputs)\n", |
| 64 | + " # apply sigmiod activation function\n", |
| 65 | + " hidden_outputs = self.activation(hidden_inputs)\n", |
| 66 | + " \n", |
| 67 | + " # combine hidden layer signals into output layer\n", |
| 68 | + " final_inputs = self.linear_ho(hidden_outputs)\n", |
| 69 | + " # apply sigmiod activation function\n", |
| 70 | + " final_outputs = self.activation(final_inputs)\n", |
| 71 | + " \n", |
| 72 | + " return final_outputs\n", |
| 73 | + "\n", |
| 74 | + " \n", |
| 75 | + " def train(self, inputs_list, targets_list):\n", |
| 76 | + " # calculate the output of the network\n", |
| 77 | + " output = self.forward(inputs_list)\n", |
| 78 | + "\n", |
| 79 | + " # create a Variable out of the target vector, doesn't need gradients calculated\n", |
| 80 | + " # also shift to GPU, remove .cuda. if not desired\n", |
| 81 | + " # target_variable = Variable(torch.cuda.FloatTensor(targets_list).view(1, self.onodes), requires_grad=False)\n", |
| 82 | + " target_variable = Variable(torch.FloatTensor(targets_list).view(1, self.onodes), requires_grad=False)\n", |
| 83 | + " \n", |
| 84 | + " # calculate error\n", |
| 85 | + " loss = self.error_function(output, target_variable)\n", |
| 86 | + " #print(loss.data[0])\n", |
| 87 | + "\n", |
| 88 | + " # zero gradients, perform a backward pass, and update the weights.\n", |
| 89 | + " self.optimiser.zero_grad()\n", |
| 90 | + " loss.backward()\n", |
| 91 | + " self.optimiser.step()\n", |
| 92 | + " pass\n", |
| 93 | + "\n", |
| 94 | + " pass" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 39, |
| 100 | + "metadata": { |
| 101 | + "collapsed": true |
| 102 | + }, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "# number of input, hidden and output nodes\n", |
| 106 | + "input_nodes = 784\n", |
| 107 | + "hidden_nodes = 200\n", |
| 108 | + "output_nodes = 10\n", |
| 109 | + "\n", |
| 110 | + "# learning rate\n", |
| 111 | + "learning_rate = 0.1\n", |
| 112 | + "\n", |
| 113 | + "# create instance of neural network\n", |
| 114 | + "n = NeuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)\n", |
| 115 | + "\n", |
| 116 | + "# move neural network to the GPU, delete if not desired\n", |
| 117 | + "# n.cuda()" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 40, |
| 123 | + "metadata": { |
| 124 | + "collapsed": true |
| 125 | + }, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "# load the mnist training data CSV file into a list\n", |
| 129 | + "training_data_file = open(\"mnist_dataset/mnist_train.csv\", 'r')\n", |
| 130 | + "training_data_list = training_data_file.readlines()\n", |
| 131 | + "training_data_file.close()" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 41, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "# %%timeit -n1 -r1 -c\n", |
| 141 | + "\n", |
| 142 | + "# train the neural network\n", |
| 143 | + "\n", |
| 144 | + "# epochs is the number of times the training data set is used for training\n", |
| 145 | + "epochs = 5\n", |
| 146 | + "\n", |
| 147 | + "for e in range(epochs):\n", |
| 148 | + " # go through all records in the training data set\n", |
| 149 | + " for record in training_data_list:\n", |
| 150 | + " # split the record by the ',' commas\n", |
| 151 | + " all_values = record.split(',')\n", |
| 152 | + " # scale and shift the inputs\n", |
| 153 | + " inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n", |
| 154 | + " # create the target output values (all 0.01, except the desired label which is 0.99)\n", |
| 155 | + " targets = numpy.zeros(output_nodes) + 0.01\n", |
| 156 | + " # all_values[0] is the target label for this record\n", |
| 157 | + " targets[int(all_values[0])] = 0.99\n", |
| 158 | + " n.train(inputs, targets)\n", |
| 159 | + " pass\n", |
| 160 | + " pass" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 42, |
| 166 | + "metadata": { |
| 167 | + "collapsed": true |
| 168 | + }, |
| 169 | + "outputs": [], |
| 170 | + "source": [ |
| 171 | + "## load the mnist test data CSV file into a list\n", |
| 172 | + "test_data_file = open(\"mnist_dataset/mnist_test.csv\", 'r')\n", |
| 173 | + "test_data_list = test_data_file.readlines()\n", |
| 174 | + "test_data_file.close()" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": 43, |
| 180 | + "metadata": { |
| 181 | + "collapsed": true |
| 182 | + }, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "# test the neural network\n", |
| 186 | + "\n", |
| 187 | + "# scorecard for how well the network performs, initially empty\n", |
| 188 | + "scorecard = []\n", |
| 189 | + "\n", |
| 190 | + "# go through all the records in the test data set\n", |
| 191 | + "for record in test_data_list:\n", |
| 192 | + " # split the record by the ',' commas\n", |
| 193 | + " all_values = record.split(',')\n", |
| 194 | + " # correct answer is first value\n", |
| 195 | + " correct_label = int(all_values[0])\n", |
| 196 | + " # scale and shift the inputs\n", |
| 197 | + " inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n", |
| 198 | + " # query the network\n", |
| 199 | + " outputs = n.forward(inputs)\n", |
| 200 | + " # the index of the highest value corresponds to the label\n", |
| 201 | + " m, label = outputs.max(1)\n", |
| 202 | + " # append correct or incorrect to list\n", |
| 203 | + " # need to extract from pytorch tensor via numpy to compare to python integer\n", |
| 204 | + " # print(\"label.data:\",label.data[0])\n", |
| 205 | + " # print(\"correct_label:\",correct_label)\n", |
| 206 | + " if (label.data[0] == correct_label):\n", |
| 207 | + " # network's answer matches correct answer, add 1 to scorecard\n", |
| 208 | + " scorecard.append(1)\n", |
| 209 | + " else:\n", |
| 210 | + " # network's answer doesn't match correct answer, add 0 to scorecard\n", |
| 211 | + " scorecard.append(0)\n", |
| 212 | + " pass\n", |
| 213 | + " \n", |
| 214 | + " pass" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "code", |
| 219 | + "execution_count": 44, |
| 220 | + "metadata": {}, |
| 221 | + "outputs": [ |
| 222 | + { |
| 223 | + "name": "stdout", |
| 224 | + "output_type": "stream", |
| 225 | + "text": [ |
| 226 | + "performance = 0.5\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "# calculate the performance score, the fraction of correct answers\n", |
| 232 | + "scorecard_array = numpy.asarray(scorecard)\n", |
| 233 | + "print (\"performance = \", scorecard_array.sum() / scorecard_array.size)" |
| 234 | + ] |
| 235 | + } |
| 236 | + ], |
| 237 | + "metadata": { |
| 238 | + "kernelspec": { |
| 239 | + "display_name": "Python 3", |
| 240 | + "language": "python", |
| 241 | + "name": "python3" |
| 242 | + }, |
| 243 | + "language_info": { |
| 244 | + "codemirror_mode": { |
| 245 | + "name": "ipython", |
| 246 | + "version": 3 |
| 247 | + }, |
| 248 | + "file_extension": ".py", |
| 249 | + "mimetype": "text/x-python", |
| 250 | + "name": "python", |
| 251 | + "nbconvert_exporter": "python", |
| 252 | + "pygments_lexer": "ipython3", |
| 253 | + "version": "3.11.1" |
| 254 | + } |
| 255 | + }, |
| 256 | + "nbformat": 4, |
| 257 | + "nbformat_minor": 2 |
| 258 | +} |
0 commit comments