-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.py
More file actions
33 lines (26 loc) · 980 Bytes
/
Network.py
File metadata and controls
33 lines (26 loc) · 980 Bytes
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
"""
Network constructor and feedforward method 'borrowed' from mnielsen
"""
import random
import numpy as np
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
def relu(z):
return z * (z > 0)
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 b, w in zip(self.biases, self.weights):
a = relu(np.dot(w, a)+b)
return a
def tweak(self):
d_biases = [np.random.randn(y, 1) * 0.1 for y in self.sizes[1:]]
d_weights = [np.random.randn(y, x) * 0.1
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
self.weights = np.add(self.weights, d_weights, dtype=object)
self.biases = np.add(self.biases,d_biases, dtype=object)