-
Notifications
You must be signed in to change notification settings - Fork 0
/
digit_classifier_nn.py
98 lines (70 loc) · 3.58 KB
/
digit_classifier_nn.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
# -----------------------------------------------------------------------------
# This file includes portions of code from DeepLearningPython by Michał Dobrzański
# Original Source: https://github.com/MichalDanielDobrzanski/DeepLearningPython
#
# MIT License
# Copyright (c) 2023 Michał Dobrzański
# -----------------------------------------------------------------------------
import numpy as np
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y,1) for y in sizes[1:]]
self.weights = [np.random.randn(y,x) for x, y in zip(sizes[:-1], sizes[1:])]
def feedforward(self, a):
for w, b in zip(self.weights, self.biases):
a = sigmoid(np.dot(w, a)+b)
return a
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
training_data = list(training_data)
training_data_len = len(training_data)
if(test_data):
test_data = list(test_data)
test_data_len = len(test_data)
for epoch in range(epochs):
np.random.shuffle(training_data)
mini_batches = [training_data[i: i+mini_batch_size] for i in range(0, training_data_len, mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta)
if test_data:
print("Epoch {}: {} / {}".format(epoch, self.evaluate(test_data), test_data_len))
else:
print("Epoch {} complete!".format(epoch))
def update_mini_batch(self, mini_batch, eta):
total_nabla_weight = [np.zeros(w.shape) for w in self.weights]
total_nabla_bias = [np.zeros(b.shape) for b in self.biases]
for x, y in mini_batch:
delta_nabla_weight, delta_nabla_bias = self.backprop(x, y)
total_nabla_weight = [w + dw for w, dw in zip(total_nabla_weight, delta_nabla_weight)]
total_nabla_bias = [b + db for b, db in zip(total_nabla_bias, delta_nabla_bias)]
self.weights = [w - (eta / len(mini_batch))*dw for w, dw in zip(self.weights, total_nabla_weight)]
self.biases = [b - (eta / len(mini_batch))*db for b, db in zip(self.biases, total_nabla_bias)]
def backprop(self, x, y):
nabla_weight = [np.zeros(w.shape) for w in self.weights]
nabla_bias = [np.zeros(b.shape) for b in self.biases]
activation = x
activations = [x]
zs = []
for w, b in zip(self.weights, self.biases):
z = np.dot(w, activation) + b
zs.append(z)
activation=sigmoid(z)
activations.append(activation)
delta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])
nabla_weight[-1] = np.dot(delta, activations[-2].transpose())
nabla_bias[-1] = delta
for layer in range(2, self.num_layers):
delta = np.dot(self.weights[-layer+1].transpose(), delta) * sigmoid_prime(zs[-layer])
nabla_weight[-layer] = np.dot(delta, activations[-layer-1].transpose())
nabla_bias[-layer] = delta
return (nabla_weight, nabla_bias)
def evaluate(self, test_data):
test_results = [(np.argmax(self.feedforward(x)), y) for (x,y) in test_data]
return sum(int(x==y) for (x,y) in test_results)
def cost_derivative(self, output_activations, y):
return (output_activations - y)
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
def sigmoid_prime(z):
return sigmoid(z)*(1-sigmoid(z))