-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·65 lines (54 loc) · 1.88 KB
/
train.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
import numpy as np
import optimizer
import random
class NeuralNetwork(object):
""" This class assembles an engine and trains
our neural network by backpropogating
Args:
layers(list): sequence of layers in NN
learning_rate(float): a speed of gradient descent
loss(object): loss function
"""
def __init__(self,layers,loss,learning_rate=0.01):
# layers is sequence of layers!
self.layers = layers
# Learning rate to SGD
self.learning_rate = learning_rate
self.loss = loss
def forward(self,actual_tensor):
""" Forward progapation
Args:
actual_tensor - input data, like X_train,X_test
"""
result = actual_tensor
# for every layer in layers
for layer in self.layers:
# Makin forward propagation
result = layer.forward(result)
return result
def backward(self,actual):
""" Backward propagation
Args:
actual(np.ndarray): actual y's, right lables
"""
# for every layer reversed we backpropagate
error = None
# Radnoming i for extracting ith sample from tenosr
# for stochastic gradient descent
i = random.randint(0,actual.shape[0]-1)
for layer in reversed(self.layers):
error = layer.backward(i,error,actual,self.loss)
# Updating weights
optim = optimizer.SGD(layer,self.learning_rate)
optim.step()
def forward_random(self,new_input_tensor):
result = new_input_tensor
for layer in self.layers:
result = layer.forward_random(result)
return result
def train(self,actual_tensor,actual,epochs):
""" Trains our NN
"""
for i in range(epochs):
self.forward(actual_tensor)
self.backward(actual)