-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.py
More file actions
138 lines (126 loc) · 4.66 KB
/
Copy pathlayer.py
File metadata and controls
138 lines (126 loc) · 4.66 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from utils import (
random_uniform_generator,
sigmoid,
softmax,
xavier_uniform_generator,
xavier_uniform_initializer,
)
class Layer:
def __init__(
self,
input_shape,
lenght,
activation,
weights_initializer=None,
index=None,
layer_info=None,
):
self.index = index
self.lenght = lenght
self.input_shape = input_shape
self.activations = np.zeros(shape=(1, lenght))
self.weights_initializer = weights_initializer
self.acc = None
if layer_info is not None:
self.weights = layer_info["weights"]
self.biases = layer_info["biases"]
self.activation = layer_info["activation"]
else:
self.activation = activation
self.__init_weights(input_shape, weights_initializer)
if self.weights_initializer is not None:
self.biases = np.zeros(shape=(lenght, 1))
else:
self.biases = None
def __init_weights(self, input_shape, initializer):
if initializer == "Uniform":
rand = random_uniform_generator(
-1,
1,
self.lenght * input_shape,
)
self.weights = rand.reshape(self.lenght, input_shape)
elif initializer == "XavierUniform":
weights = xavier_uniform_initializer(
self.input_shape,
self.lenght,
)
self.weights = weights
else:
self.weights = None
def __repr__(self) -> str:
return (
f"layer n{self.index}\n"
+ f"activation: {self.activation}\nactivations: {self.activations}\nactivations shape: {self.activations.shape}\nbiases: {self.biases}\ninitializer: {self.weights_initializer}\n"
+ (
f"weights: {self.weights.shape}\nweights content: {self.weights}"
if self.weights_initializer is not None
else ""
)
)
def get_infos(self):
return {
"index": self.index,
"weights": self.weights,
"biases": self.biases,
"activation": self.activation,
"input_shape": self.input_shape,
"lenght": self.lenght,
}
def reset_acc(self):
self.acc = None
def get_activations(self):
return self.activations
def forward(self, previous_activations, training=True, momentum=None):
new_activations = None
weights = self.weights
biases = self.biases
if momentum and weights is not None:
if self.acc is None:
self.acc = {"weights": 0, "biases": 0}
self.acc["weights"] = 0
self.acc["biases"] = 0
self.acc["weights"] *= momentum
self.acc["biases"] *= momentum
weights -= self.acc["weights"]
biases -= self.acc["biases"]
if self.activation == "sigmoid":
# print(
# f"weights: {weights}\n",
# f"act: {previous_activations}",
# f"biases: {biases}",
# )
new_activations = sigmoid(weights.dot(previous_activations) + biases)
# print(f"new activation: {new_activations}")
elif self.activation == "softmax":
tmp = weights.dot(previous_activations) + biases
new_activations = softmax(tmp)
elif (
self.activation is None
): # if is the input layer so it will store the features
new_activations = previous_activations
if training is True:
self.activations = new_activations
return new_activations
def backward(self, dz, previous_activations, targets):
targets_lenght = targets.shape[0]
if dz is None:
dz = self.activations - targets
dw = (1 / targets_lenght) * dz.dot(previous_activations.T)
db = (1 / targets_lenght) * np.sum(dz, axis=1, keepdims=True)
next_dz = (
self.weights.T.dot(dz) * previous_activations * (1 - previous_activations)
)
return next_dz, dw, db
def update(self, dw, db, learning_rate):
if self.acc is not None:
self.weights += self.acc["weights"] - learning_rate * dw
self.biases += self.acc["biases"] - learning_rate * db
self.acc["weights"] = self.acc["weights"] - learning_rate * dw
self.acc["biases"] = self.acc["biases"] - learning_rate * db
else:
self.weights -= learning_rate * dw
self.biases -= learning_rate * db