-
Notifications
You must be signed in to change notification settings - Fork 1
/
Layers.py
54 lines (42 loc) · 1.81 KB
/
Layers.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
import numpy as np
import utorch.simplegrad as sg
from utorch.nets.Model import Model, NetworkParameter
class LinearLayer(Model):
def __init__(self, n_input, n_hidden, has_bias=False,name=None):
layer_name = "_".join([name, "weights"])
# I can move the layer initialization into the different method, right now it is what it is.
self.weights = NetworkParameter(np.random.normal(0,1, size=(n_hidden, n_input)),name=layer_name)
self.has_bias = has_bias
if has_bias:
layer_name = "_".join([name, "bias"])
self.bias = NetworkParameter(np.zeros(n_hidden),layer_name)
def forward(self, x, *args, **kwargs):
if self.has_bias:
output = [email protected](self.weights) + self.bias
else:
output = [email protected](self.weights)
return output
class ReLULayer(Model):
def __init__(self):
self.relu = sg.Variable.relu
def forward(self, x, *args, **kwargs):
return self.relu(x)
class StackedLayers(Model):
def __init__(self, layers):
StackedLayers._validate_layers(layers)
self.layers = layers
def forward(self, x, *args, **kwargs):
x = self.layers[0](x)
for layer in self.layers[1:]:
x = layer(x)
return x
def __getitem__(self, index):
return self.layers[index]
@classmethod
def _validate_layers(cls, layers):
if len(layers) < 2:
raise RuntimeError(
"You should use *StackedLayers* if you want to stack a couple of layers, no need to use it with a single layer!")
if not all([map(lambda layer: isinstance(layer, Model), layers)]):
raise RuntimeError(
"The input to the *StackedLayers* is a list of layers or Models, please provide a correct one!")