-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
29 lines (21 loc) · 1.02 KB
/
neural_network.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
from layers import FeedForwardLayer
from losses import MSELoss, SoftmaxCrossEntropyLoss, WeightedSoftmaxCrossEntropyLoss, WeightedMSELoss
class NeuralNetwork:
def __init__(self, ):
self.layers = []
def add_layer(self, layer_name='', input_size=None, units=10, activation_fn='linear', initializer='random'):
if self.layers == [] and input_size is None:
raise Exception('First layer must have input shape!')
if self.layers == []:
layer = FeedForwardLayer(input_size=input_size, output_size=units, activation_fn=activation_fn, initializer=initializer)
else:
layer = FeedForwardLayer(input_size=self.layers[-1]._output_size, output_size=units, activation_fn=activation_fn, initializer=initializer)
self.layers.append(layer)
def forward(self, X):
for layer in self.layers:
X = layer.forward(X)
return X
def backward(self, e):
for layer in self.layers[::-1]:
e = layer.backward(e)