-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_assignment2.py
165 lines (137 loc) · 5.02 KB
/
ML_assignment2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# importing some useful libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
#from sklearn.preprocressing import StandardScaler
from sklearn import metrics
#from sklearn.metrics import confusion_matrix
import itertools
import pickle
# loads object from file
def unpickle(fileName):
"""
takes filename as argument, returns dict object from pickle
"""
import pickle
with open(fileName, 'rb') as fileObj:
dict = pickle.load(fileObj, encoding='bytes')
return dict
# defining a class for our 2-layer neuralnet
class neuralNet:
"""
A class for implemeting a 2 layer neural network
"""
def __init__(self, x, y):
"""
CONSTRUCTOR FUNCTION FOR NEURAL NETWORK
---------------------------------------
X = input
Y = output (ideal)
Yd = output (predicted)
size = dimensions [input units, 1-L units, 2-L(out) units]
lays = number of layers
dwnb = dict to hold weights and bias
temp = a temp variable for calculations
lrte = learning rate
nsmp = number of samples
loss = stores Y-Yd every x iterations
"""
self.X = x
self.Y = y
self.lays = 2 # 2-layerd
self.size = [3072, 3072, 1]
# self.dwnb = {'W1': [], 'B1': [], 'W2': [], 'B2': []}
self.dwnb = {}
self.temp = {}
self.loss = []
self.lrte = 0.002
self.nsmp = (self.Y.shape[0])
self.Yd = np.zeros((1, self.nsmp))
return
def randomInitializer(self):
"""
Initializes the weights and biases with random values.
"""
np.random.seed(1)
self.dwnb['W1'] = (np.random.randn(self.size[1], self.size[0]) / np.sqrt(self.size[0]))
self.dwnb['B1'] = (np.zeros((self.size[1], 1))) #column vector of height= n(1L units)
self.dwnb['W2'] = (np.random.randn(self.size[2], self.size[1]) / np.sqrt(self.size[1]))
self.dwnb['B2'] = (np.zeros((self.size[2], 1)))
return
def calcSigmoid(self, z):
"""
calculates and returns sigmoid function for a particular input
"""
return (1/(1 + np.exp(-z)))
def calcNetLoss(self, yP):
"""
calculate and return calculated loss
"""
lossT = (1./self.nsmp) * ((0 - np.dot(self.Y, np.log(yP).T)) - np.dot(1-self.Y, np.log(1-yP).T))
return lossT
def calcDerSigmoid(self, z):
s = 1 / (1 + np.exp(-z))
dZ = s * (1-s)
return dZ
def passForward(self):
"""
"""
z1 = (np.dot(self.dwnb['W1'], (self.X)) + self.dwnb['B1'])
a1 = self.calcSigmoid(z1)
self.temp['Z1'] = z1
self.temp['A1'] = a1
z2 = (np.dot(self.dwnb['W2'], (self.X)) + self.dwnb['B2'])
a2 = self.calcSigmoid(z2)
self.temp['Z2'] = z2
self.temp['A2'] = a2
self.Yd = a2
loss = self.calcNetLoss(a2)
return self.Yd, loss
def passBackward(self):
"""
"""
dlossYd = -(np.divide(self.Y, self.Yd) - np.divide(1-self.Y, 1-self.Yd))
dlossZ2 = dlossYd * self.calcDerSigmoid(self.temp['Z2'])
dlossA1 = np.dot(np.transpose(self.dwnb['W2']), dlossZ2)
dlossW2 = ((1./(self.temp['A1'].shape[1])) * np.dot(dlossZ2, np.transpose(self.temp['A1'])))
dlossB2 = ((1./(self.temp['A1'].shape[1])) * np.dot(dlossZ2, np.ones([dlossZ2.shape[1], 1])))
dlossZ1 = dlossA1 * self.calcDerSigmoid(self.temp['Z1'])
dlossA0 = np.dot(np.transpose(self.dwnb['W1']), dlossZ1)
dlossW1 = ((1./(self.X.shape[1])) * np.dot(dlossZ1, np.transpose(self.X)))
dlossB1 = ((1./(self.X.shape[1])) * np.dot(dlossZ1, np.ones([dlossZ1.shape[1], 1])))
self.dwnb['W1'] = self.dwnb['W1'] - self.lrte * dlossW1
self.dwnb['B1'] = self.dwnb['B1'] - self.lrte * dlossB1
self.dwnb['W2'] = self.dwnb['W2'] - self.lrte * dlossW2
self.dwnb['B2'] = self.dwnb['B2'] - self.lrte * dlossB2
return
def gradientDescent(self, n):
"""
"""
np.random.seed(1)
self.randomInitializer()
for i in range(n):
yP, loss = self.passForward()
self.passBackward()
if(i % 200):
print("Cost after iteration %i: %f" %(i, loss))
self.loss.append(loss)
return
#--- END of CLASS neuralNet
dataA = unpickle('data_batch_2')
# print(' >> KEYS:')
# for i in dataA:
# print(i)
# KEYS: b'batch_label', b'labels', b'data', b'filenames'
#y = np.transpose(dataA[b'labels'])
y = (np.array(dataA[b'labels']))
xtemp = dataA[b'data']
scaler = preprocessing.MinMaxScaler()
x = scaler.fit_transform(xtemp)
print(xtemp)
print(x)
print(y.shape)
print(x.shape)
testClass = neuralNet(np.transpose(x), np.transpose(y))
testClass.gradientDescent(5)
# print(x[0].shape)