Skip to content

Commit 24b9e48

Browse files
committed
Entrega 1
1 parent 2eae26e commit 24b9e48

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jupyter.jupyterServerType": "local"
3+
}

Método SGD.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
import random
3+
4+
def SGD(self, training_data, epochs, mini_batch_size, eta,
5+
test_data=None):
6+
7+
if test_data:
8+
test_data = list(test_data)
9+
n_test = len(test_data)
10+
11+
for j in range(epochs):
12+
random.shuffle(training_data)
13+
mini_batches = [ training_data[k:k+mini_batch_size]
14+
for k in range(0, n, mini_batch_size)]
15+
16+
for mini_batch in mini_batches:
17+
self.update_mini_batch(mini_batch, eta)
18+
19+
if test_data:
20+
print("Epoch {} : {} / {}".format(j,self.evaluate(test_data),n_test))
21+
else:
22+
print("Epoch {} complete".format(j))
23+
24+
def update_mini_batch(self, mini_batch, eta):
25+
26+
nabla_b = [np.zeros(b.shape) for b in self.biases]
27+
nabla_w = [np.zeros(w.shape) for w in self.weights]
28+
29+
for x, y in mini_batch:
30+
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
31+
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
32+
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
33+
34+
self.weights = [w-(eta/len(mini_batch))*nw
35+
for w, nw in zip(self.weights, nabla_w)]
36+
37+
self.biases = [b-(eta/len(mini_batch))*nb
38+
for b, nb in zip(self.biases, nabla_b)]
39+
40+
def update_mini_batch(self, mini_batch, eta):
41+
42+
nabla_b = [np.zeros(b.shape) for b in self.biases]
43+
nabla_w = [np.zeros(w.shape) for w in self.weights]
44+
for x, y in mini_batch:
45+
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
46+
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
47+
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
48+
self.weights = [w-(eta/len(mini_batch))*nw
49+
for w, nw in zip(self.weights, nabla_w)]
50+
self.biases = [b-(eta/len(mini_batch))*nb
51+
for b, nb in zip(self.biases, nabla_b)]

network.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,4 @@ def sigmoid(z):
147147
def sigmoid_prime(z):
148148
"""Derivative of the sigmoid function."""
149149
return sigmoid(z)*(1-sigmoid(z))
150+

0 commit comments

Comments
 (0)