-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSgd.py
More file actions
222 lines (198 loc) · 9.82 KB
/
Sgd.py
File metadata and controls
222 lines (198 loc) · 9.82 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
rng = np.random.default_rng()
def sample_minibatch(X, y, batch_size):
random_indexes = rng.choice(X.shape[1], batch_size, False)
# print("X.T[random_indexes]", (X.T[random_indexes]))
#print("X.T[random_indexes].T", (X.T[random_indexes]).T)
#print(y.shape)
#print("y[random_indexes]", (y[random_indexes]))
return (X.T[random_indexes]).T, (y[random_indexes]) # this is because X is of shape 100,2 and y is of shape 100,2
# so in order to select some random rows from X and y we need to transpose X and then select the rows and then transpose it back
def sgd(X, y, X_test, y_test, layer, learning_rate, epochs, batch_size, accuracy_sample_size_train, accuracy_sample_size_test):
#num_samples = len(y)
train_loss = []
test_loss = []
accuracy_train = []
accuracy_test = []
num_points = X.shape[1]
for epoch in range(epochs):
# Shuffle the data at the beginning of each epoch
#epoch_train_loss = []
#epoch_test_loss = []
#epoch_accuracy_train = []
#epoch_accuracy_test = []
indexes = np.arange(num_points)
np.random.shuffle(indexes)
print("epoch", epoch)
for i in range(0, num_points, batch_size):
X_batch = X[:, indexes[i:i + batch_size]]
y_batch = y[indexes[i:i + batch_size]]
dw, dx, db = layer.gradient(X_batch, y_batch)
layer.W -= learning_rate * dw
layer.b -= learning_rate * db
#epoch_train_loss.append(layer.loss(X_batch, y_batch))
#epoch_accuracy_train.append(calcpercents(y_batch, layer.activation(X_batch)))
X_train_sample, y_train_sample = sample_minibatch(X, y, accuracy_sample_size_train)
X_test_sample, y_test_sample = sample_minibatch(X_test, y_test, accuracy_sample_size_test)
train_loss.append(layer.loss(X_train_sample, y_train_sample))
test_loss.append(layer.loss(X_test_sample, y_test_sample))
accuracy_train.append(calcpercents(y_train_sample, layer.activation(X_train_sample)))
accuracy_test.append(calcpercents(y_test_sample, layer.activation(X_test_sample)))
#train_loss.append(np.mean(epoch_train_loss))
#test_loss.append(layer.loss(X_test, y_test))
#accuracy_train.append(np.mean(epoch_accuracy_train))
#accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
#
# print("epoch", epoch)
# X_batch, y_batch = sample_minibatch(X, y, batch_size)
# X_batch_test, y_batch_test = sample_minibatch(X_test, y_test, batch_size)
# dw, dx, db = layer.gradient(X_batch, y_batch)
# layer.W -= learning_rate * dw
# layer.b -= learning_rate * db
#
# accuracy_train.append(calcpercents(y, layer.activation(X)))
# accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
#
# # Calculate and print the mean loss after each epoch
# #Loss = layer.loss(X_batch, y_batch)
# test_loss.append(layer.loss(X_batch_test, y_batch_test))
# train_loss.append(layer.loss(X_batch, y_batch))
# plt.plot(range(1, epochs + 1), losses)
# plt.show()
return accuracy_train, accuracy_test, train_loss, test_loss
# def sgd(X, y, X_test, y_test, layers, learning_rate, epochs, batch_size, accuracy_sample_size_train, accuracy_sample_size_test):
# #num_samples = len(y)
# train_loss = []
# test_loss = []
# accuracy_train = []
# accuracy_test = []
# num_points = X.shape[1]
# for epoch in range(epochs):
# # Shuffle the data at the beginning of each epoch
# #epoch_train_loss = []
# #epoch_test_loss = []
# #epoch_accuracy_train = []
# #epoch_accuracy_test = []
# indexes = np.arange(num_points)
# np.random.shuffle(indexes)
# print("epoch", epoch)
# for i in range(0, num_points, batch_size):
# X_batch = X[:, indexes[i:i + batch_size]]
# y_batch = y[indexes[i:i + batch_size]]
# for layer in layers:
# dw, dx, db = layer.gradient(X_batch, y_batch)
# layer.W -= learning_rate * dw
# layer.b -= learning_rate * db
#
# #epoch_train_loss.append(layer.loss(X_batch, y_batch))
# #epoch_accuracy_train.append(calcpercents(y_batch, layer.activation(X_batch)))
# X_train_sample, y_train_sample = sample_minibatch(X, y, accuracy_sample_size_train)
# X_test_sample, y_test_sample = sample_minibatch(X_test, y_test, accuracy_sample_size_test)
# train_loss.append(layer.loss(X_train_sample, y_train_sample))
# test_loss.append(layer.loss(X_test_sample, y_test_sample))
# accuracy_train.append(calcpercents(y_train_sample, layer.activation(X_train_sample)))
# accuracy_test.append(calcpercents(y_test_sample, layer.activation(X_test_sample)))
# #train_loss.append(np.mean(epoch_train_loss))
# #test_loss.append(layer.loss(X_test, y_test))
# #accuracy_train.append(np.mean(epoch_accuracy_train))
# #accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
# #
# # print("epoch", epoch)
# # X_batch, y_batch = sample_minibatch(X, y, batch_size)
# # X_batch_test, y_batch_test = sample_minibatch(X_test, y_test, batch_size)
# # dw, dx, db = layer.gradient(X_batch, y_batch)
# # layer.W -= learning_rate * dw
# # layer.b -= learning_rate * db
# #
# # accuracy_train.append(calcpercents(y, layer.activation(X)))
# # accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
# #
# # # Calculate and print the mean loss after each epoch
# # #Loss = layer.loss(X_batch, y_batch)
# # test_loss.append(layer.loss(X_batch_test, y_batch_test))
# # train_loss.append(layer.loss(X_batch, y_batch))
#
# # plt.plot(range(1, epochs + 1), losses)
# # plt.show()
#
# return accuracy_train, accuracy_test, train_loss, test_loss
def calcpercents(y, y_hat):
denominator = y_hat.shape[0]
# Find the indices of the maximum values in each row of y_hat
max_indices_y_hat = np.argmax(y_hat, axis=1)
# Find the indices where the value is 1 in each row of y
indices_y = np.argmax(y, axis=1)
# Count the number of matching rows
Numerator = np.sum(max_indices_y_hat == indices_y)
return Numerator / denominator
def General_sgd(X, y, X_test, y_test, layers, learning_rate, epochs, batch_size, accuracy_sample_size_train, accuracy_sample_size_test):
#num_samples = len(y)
train_loss = []
test_loss = []
accuracy_train = []
accuracy_test = []
num_points = X.shape[1]
last_layer = layers[-1]
first_Layers = layers[:-1]
for epoch in range(epochs):
# Shuffle the data at the beginning of each epoch
#epoch_train_loss = []
#epoch_test_loss = []
#epoch_accuracy_train = []
#epoch_accuracy_test = []
indexes = np.arange(num_points)
np.random.shuffle(indexes)
print("epoch", epoch)
for i in range(0, num_points, batch_size):
X_batch = X[:, indexes[i:i + batch_size]]
y_batch = y[indexes[i:i + batch_size]]
LastX = push_forward(X_batch, first_Layers)
#dealing with last layer (with softmax):
dw, dx, db = last_layer.gradient(LastX, y_batch)
last_layer.W -= learning_rate * dw
last_layer.b -= learning_rate * db
#dealing with the rest of the layers:
dx_holder = dx
for layer in first_Layers[::-1]:
dw, dw2, dx, db = layer.gradient(layer.X, dx_holder)
layer.W -= learning_rate * dw
layer.b -= learning_rate * db
if (dw2 is not None):
layer.W2 -= learning_rate * dw2
dx_holder = dx
#epoch_train_loss.append(layer.loss(X_batch, y_batch))
#epoch_accuracy_train.append(calcpercents(y_batch, layer.activation(X_batch)))
X_train_sample, y_train_sample = sample_minibatch(X, y, accuracy_sample_size_train)
X_test_sample, y_test_sample = sample_minibatch(X_test, y_test, accuracy_sample_size_test)
X_train_sample_last = push_forward(X_train_sample, first_Layers)
X_test_sample_last = push_forward(X_test_sample, first_Layers)
train_loss.append(last_layer.loss(X_train_sample_last, y_train_sample))
test_loss.append(last_layer.loss(X_test_sample_last, y_test_sample))
accuracy_train.append(calcpercents(y_train_sample, last_layer.activation(X_train_sample_last)))
accuracy_test.append(calcpercents(y_test_sample, last_layer.activation(X_test_sample_last)))
#train_loss.append(np.mean(epoch_train_loss))
#test_loss.append(layer.loss(X_test, y_test))
#accuracy_train.append(np.mean(epoch_accuracy_train))
#accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
#
# print("epoch", epoch)
# X_batch, y_batch = sample_minibatch(X, y, batch_size)
# X_batch_test, y_batch_test = sample_minibatch(X_test, y_test, batch_size)
# dw, dx, db = layer.gradient(X_batch, y_batch)
# layer.W -= learning_rate * dw
# layer.b -= learning_rate * db
#
# accuracy_train.append(calcpercents(y, layer.activation(X)))
# accuracy_test.append(calcpercents(y_test, layer.activation(X_test)))
#
# # Calculate and print the mean loss after each epoch
# #Loss = layer.loss(X_batch, y_batch)
# test_loss.append(layer.loss(X_batch_test, y_batch_test))
# train_loss.append(layer.loss(X_batch, y_batch))
# plt.plot(range(1, epochs + 1), losses)
# plt.show()
return accuracy_train, accuracy_test, train_loss, test_loss
def push_forward(X, layers):
for layer in layers:
X = layer.activation(X)
return X