-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.py
29 lines (23 loc) · 993 Bytes
/
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
import numpy as np
class NeuralNetwork():
def __init__(self, layer_sizes):
# TODO
# layer_sizes example: [4, 10, 2]
self.w1 = np.random.normal(0, 1, size=(layer_sizes[1], layer_sizes[0]))
self.b1 = np.random.normal(0, 1, size=(layer_sizes[1], 1))
self.w2 = np.random.normal(0, 1, size=(layer_sizes[2], layer_sizes[1]))
self.b2 = np.random.normal(0, 1, size=(layer_sizes[2], 1))
self.sizes = layer_sizes
def activation(self, x):
return 1 / (1 + np.exp(-x))
# TODO
def forward(self, x):
# print(self.w1, self.b1)
input_layer = np.reshape(x, (self.sizes[0], 1))
hidden_layer = self.activation(np.dot(self.w1, input_layer) + self.b1)
output_layer = self.activation(np.dot(self.w2, hidden_layer) + self.b2)
# file = open("log.txt", 'w')
# file.write(output_layer)
return output_layer
# TODO
# x example: np.array([[0.1], [0.2], [0.3]])