|
| 1 | +# from https://www.geeksforgeeks.org/implementation-of-neural-network-from-scratch-using-numpy/ |
| 2 | + |
| 3 | + |
| 4 | +import numpy as _np |
| 5 | +import torch_np as np |
| 6 | + |
| 7 | +# To run on CUDA, change "cpu" to "cuda" below. |
| 8 | +import torch |
| 9 | +torch.set_default_device("cpu") |
| 10 | + |
| 11 | + |
| 12 | +# Creating data set |
| 13 | + |
| 14 | +# A |
| 15 | +a =[0, 0, 1, 1, 0, 0, |
| 16 | + 0, 1, 0, 0, 1, 0, |
| 17 | + 1, 1, 1, 1, 1, 1, |
| 18 | + 1, 0, 0, 0, 0, 1, |
| 19 | + 1, 0, 0, 0, 0, 1] |
| 20 | +# B |
| 21 | +b =[0, 1, 1, 1, 1, 0, |
| 22 | + 0, 1, 0, 0, 1, 0, |
| 23 | + 0, 1, 1, 1, 1, 0, |
| 24 | + 0, 1, 0, 0, 1, 0, |
| 25 | + 0, 1, 1, 1, 1, 0] |
| 26 | +# C |
| 27 | +c =[0, 1, 1, 1, 1, 0, |
| 28 | + 0, 1, 0, 0, 0, 0, |
| 29 | + 0, 1, 0, 0, 0, 0, |
| 30 | + 0, 1, 0, 0, 0, 0, |
| 31 | + 0, 1, 1, 1, 1, 0] |
| 32 | + |
| 33 | +# Creating labels |
| 34 | +y =[[1, 0, 0], |
| 35 | + [0, 1, 0], |
| 36 | + [0, 0, 1]] |
| 37 | + |
| 38 | + |
| 39 | +# converting data and labels into numpy array |
| 40 | + |
| 41 | +""" |
| 42 | +Convert the matrix of 0 and 1 into one hot vector |
| 43 | +so that we can directly feed it to the neural network, |
| 44 | +these vectors are then stored in a list x. |
| 45 | +""" |
| 46 | + |
| 47 | +x =[np.array(a).reshape(1, 30), np.array(b).reshape(1, 30), |
| 48 | + np.array(c).reshape(1, 30)] |
| 49 | + |
| 50 | + |
| 51 | +# Labels are also converted into NumPy array |
| 52 | +y = np.array(y) |
| 53 | + |
| 54 | + |
| 55 | +print(x, "\n\n", y) |
| 56 | + |
| 57 | + |
| 58 | +# activation function |
| 59 | + |
| 60 | +def sigmoid(x): |
| 61 | + return(1/(1 + np.exp(-x))) |
| 62 | + |
| 63 | +# Creating the Feed forward neural network |
| 64 | +# 1 Input layer(1, 30) |
| 65 | +# 1 hidden layer (1, 5) |
| 66 | +# 1 output layer(3, 3) |
| 67 | + |
| 68 | +def f_forward(x, w1, w2): |
| 69 | + # hidden |
| 70 | + z1 = x.dot(w1)# input from layer 1 |
| 71 | + a1 = sigmoid(z1)# out put of layer 2 |
| 72 | + |
| 73 | + # Output layer |
| 74 | + z2 = a1.dot(w2)# input of out layer |
| 75 | + a2 = sigmoid(z2)# output of out layer |
| 76 | + return(a2) |
| 77 | + |
| 78 | +# initializing the weights randomly |
| 79 | +def generate_wt(x, y): |
| 80 | + |
| 81 | + _np.random.seed(1234) |
| 82 | + |
| 83 | + l =[] |
| 84 | + for i in range(x * y): |
| 85 | + l.append(_np.random.randn()) |
| 86 | + return(np.array(l).reshape(x, y)) |
| 87 | + |
| 88 | +# for loss we will be using mean square error(MSE) |
| 89 | +def loss(out, Y): |
| 90 | + s =(np.square(out-Y)) |
| 91 | + s = np.sum(s)/len(y) |
| 92 | + return(s) |
| 93 | + |
| 94 | +# Back propagation of error |
| 95 | +def back_prop(x, y, w1, w2, alpha): |
| 96 | + |
| 97 | + # hidden layer |
| 98 | + z1 = x.dot(w1)# input from layer 1 |
| 99 | + a1 = sigmoid(z1)# output of layer 2 |
| 100 | + |
| 101 | + # Output layer |
| 102 | + z2 = a1.dot(w2)# input of out layer |
| 103 | + a2 = sigmoid(z2)# output of out layer |
| 104 | + # error in output layer |
| 105 | + d2 =(a2-y) |
| 106 | + d1 = np.multiply((w2.dot((d2.transpose()))).transpose(), |
| 107 | + (np.multiply(a1, 1-a1))) |
| 108 | + |
| 109 | + # Gradient for w1 and w2 |
| 110 | + w1_adj = x.transpose().dot(d1) |
| 111 | + w2_adj = a1.transpose().dot(d2) |
| 112 | + |
| 113 | + # Updating parameters |
| 114 | + w1 = w1-(alpha*(w1_adj)) |
| 115 | + w2 = w2-(alpha*(w2_adj)) |
| 116 | + |
| 117 | + return(w1, w2) |
| 118 | + |
| 119 | +def train(x, Y, w1, w2, alpha = 0.01, epoch = 10): |
| 120 | + acc =[] |
| 121 | + losss =[] |
| 122 | + for j in range(epoch): |
| 123 | + l =[] |
| 124 | + for i in range(len(x)): |
| 125 | + out = f_forward(x[i], w1, w2) |
| 126 | + l.append((loss(out, Y[i]))) |
| 127 | + w1, w2 = back_prop(x[i], y[i], w1, w2, alpha) |
| 128 | + print("epochs:", j + 1, "======== acc:", (1-(sum(l)/len(x)))*100) |
| 129 | + acc.append((1-(sum(l)/len(x)))*100) |
| 130 | + losss.append(sum(l)/len(x)) |
| 131 | + return(acc, losss, w1, w2) |
| 132 | + |
| 133 | +def predict(x, w1, w2): |
| 134 | + Out = f_forward(x, w1, w2) |
| 135 | + maxm = 0 |
| 136 | + k = 0 |
| 137 | + for i in range(len(Out[0])): |
| 138 | + if(maxm<Out[0][i]): |
| 139 | + maxm = Out[0][i] |
| 140 | + k = i |
| 141 | + if(k == 0): |
| 142 | + print("Image is of letter A.") |
| 143 | + elif(k == 1): |
| 144 | + print("Image is of letter B.") |
| 145 | + else: |
| 146 | + print("Image is of letter C.") |
| 147 | +# plt.imshow(x.reshape(5, 6)) |
| 148 | +# plt.show() |
| 149 | + |
| 150 | +w1 = generate_wt(30, 5) |
| 151 | +w2 = generate_wt(5, 3) |
| 152 | +print(w1, "\n\n", w2) |
| 153 | + |
| 154 | + |
| 155 | +"""The arguments of train function are data set list x, |
| 156 | +correct labels y, weights w1, w2, learning rate = 0.1, |
| 157 | +no of epochs or iteration.The function will return the |
| 158 | +matrix of accuracy and loss and also the matrix of |
| 159 | +trained weights w1, w2""" |
| 160 | + |
| 161 | +acc, losss, w1, w2 = train(x, y, w1, w2, 0.1, 100) |
| 162 | + |
| 163 | + |
| 164 | +""" |
| 165 | +The predict function will take the following arguments: |
| 166 | +1) image matrix |
| 167 | +2) w1 trained weights |
| 168 | +3) w2 trained weights |
| 169 | +""" |
| 170 | +predict(x[1], w1, w2) |
| 171 | + |
0 commit comments