-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmodels.py
More file actions
137 lines (105 loc) · 4.44 KB
/
Copy pathmodels.py
File metadata and controls
137 lines (105 loc) · 4.44 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
# EpyNN/epynn/convolution/models.py
# Local application/library specific imports
from epynn.commons.models import Layer
from epynn.commons.maths import (
relu,
xavier,
activation_tune,
)
from epynn.convolution.forward import convolution_forward
from epynn.convolution.backward import convolution_backward
from epynn.convolution.parameters import (
convolution_compute_shapes,
convolution_initialize_parameters,
convolution_compute_gradients,
convolution_update_parameters,
)
class Convolution(Layer):
"""
Definition of a convolution layer prototype.
:param unit_filters: Number of unit filters in convolution layer, defaults to 1.
:type unit_filters: int, optional
:param filter_size: Height and width for convolution window, defaults to `(3, 3)`.
:type filter_size: int or tuple[int], optional
:param strides: Height and width to shift the convolution window by, defaults to `None` which equals `filter_size`.
:type strides: int or tuple[int], optional
:param padding: Number of zeros to pad each features plane with, defaults to 0.
:type padding: int, optional
:param activate: Non-linear activation of unit filters, defaults to `relu`.
:type activate: function, optional
:param initialization: Weight initialization function for convolution layer, defaults to `xavier`.
:type initialization: function, optional
:param use_bias: Whether the layer uses bias, defaults to `True`.
:type use_bias: bool, optional
:param se_hPars: Layer hyper-parameters, defaults to `None` and inherits from model.
:type se_hPars: dict[str, str or float] or NoneType, optional
"""
def __init__(self,
unit_filters=1,
filter_size=(3, 3),
strides=None,
padding=0,
activate=relu,
initialization=xavier,
use_bias=True,
se_hPars=None):
"""Initialize instance variable attributes.
"""
super().__init__()
filter_size = filter_size if isinstance(filter_size, tuple) else (filter_size, filter_size)
strides = strides if isinstance(strides, tuple) else filter_size
self.d['u'] = unit_filters
self.d['fh'], self.d['fw'] = filter_size
self.d['sh'], self.d['sw'] = strides
self.d['p'] = padding
self.activate = activate
self.initialization = initialization
self.use_bias = use_bias
self.activation = { 'activate': activate.__name__ }
self.trainable = True
return None
def compute_shapes(self, A):
"""Wrapper for :func:`epynn.convolution.parameters.convolution_compute_shapes()`.
:param A: Output of forward propagation from previous layer.
:type A: :class:`numpy.ndarray`
"""
convolution_compute_shapes(self, A)
return None
def initialize_parameters(self):
"""Wrapper for :func:`epynn.convolution.parameters.convolution_initialize_parameters()`.
"""
convolution_initialize_parameters(self)
return None
def forward(self, A):
"""Wrapper for :func:`epynn.convolution.forward.convolution_forward()`.
:param A: Output of forward propagation from *previous* layer.
:type A: :class:`numpy.ndarray`
:return: Output of forward propagation for **current** layer.
:rtype: :class:`numpy.ndarray`
"""
activation_tune(self.se_hPars)
A = convolution_forward(self, A)
self.update_shapes(self.fc, self.fs)
return A
def backward(self, dX):
"""Wrapper for :func:`epynn.convolution.backward.convolution_backward()`.
:param dX: Output of backward propagation from next layer.
:type dX: :class:`numpy.ndarray`
:return: Output of backward propagation for current layer.
:rtype: :class:`numpy.ndarray`
"""
activation_tune(self.se_hPars)
dX = convolution_backward(self, dX)
self.update_shapes(self.bc, self.bs)
return dX
def compute_gradients(self):
"""Wrapper for :func:`epynn.convolution.parameters.convolution_compute_gradients()`.
"""
convolution_compute_gradients(self)
return None
def update_parameters(self):
"""Wrapper for :func:`epynn.convolution.parameters.convolution_update_parameters()`.
"""
if self.trainable:
convolution_update_parameters(self)
return None