-
Notifications
You must be signed in to change notification settings - Fork 1
/
groupnetworks.py
45 lines (37 loc) · 1.6 KB
/
groupnetworks.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
import numpy as np
from itertools import izip
from genericlayer import GenericLayer, WithElements
class SumGroup(WithElements, GenericLayer):
def forward(self, x_group, update = False):
y_group = []
for (x, element) in izip(x_group, self.elements):
y_group.append(element.forward(x, update))
return np.sum(np.array(y_group),0)
def backward(self, dJdy, optimizer = None):
dJdx_group = []
for element in self.elements:
dJdx_group.append(element.backward(dJdy, optimizer))
return dJdx_group
class MulGroup(WithElements, GenericLayer):
def forward(self, x_group, update = False):
self.y_group = []
for (x, element) in zip(x_group, self.elements):
self.y_group.append(element.forward(x, update))
return np.prod(np.array(self.y_group),0)
def backward(self, dJdy, optimizer = None):
dJdx_group = []
for i,element in enumerate(self.elements):
aux_dJdy = np.prod(np.array(self.y_group[:i]+self.y_group[i+1:]),0)*dJdy
dJdx_group.append(element.backward(aux_dJdy, optimizer))
return dJdx_group
class ParallelGroup(WithElements, GenericLayer):
def forward(self, x, update = False):
y_group = []
for element in self.elements:
y_group.append(element.forward(x, update))
return y_group
def backward(self, dJdy_group, optimizer = None):
aux_dJdx = []
for (dJdy, element) in izip(dJdy_group, self.elements):
aux_dJdx.append(element.backward(dJdy, optimizer))
return np.sum(np.array(aux_dJdx),0)