-
Notifications
You must be signed in to change notification settings - Fork 9
/
base_model.py
51 lines (43 loc) · 1.59 KB
/
base_model.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
from abc import ABCMeta
from layers.linear import *
from layers.conv2d import *
import torch.nn as nn
# Add custom layers here.
_STN_LAYERS = [StnLinear, StnConv2d]
class StnModel(nn.Module, metaclass=ABCMeta):
# Initialize an attribute self.layers (a list containing all layers).
def get_layers(self):
raise NotImplementedError
def get_response_parameters(self):
""" Return the response parameters.
:return: List[Tensors]
"""
params = []
for idx, layer in enumerate(self.get_layers()):
for stn_layer in _STN_LAYERS:
if isinstance(layer, stn_layer):
params = params + layer.response_parameters
return params
def get_general_parameters(self):
""" Return the general parameters.
:return: List[Tensors]
"""
params = []
for idx, layer in enumerate(self.get_layers()):
is_stn_layer = False
for stn_layer in _STN_LAYERS:
if isinstance(layer, stn_layer):
is_stn_layer = True
params = params + layer.general_parameters
break
if not is_stn_layer:
params = params + [p for p in layer.parameters()]
return params
def forward(self, x, h_net, h_param):
""" A forward pass for StnModel.
:param x: Input Tensor
:param h_net: Tensor of size 'batch_size x num_hyper'
:param h_param: Tensor of size 'batch_size x num_hyper'
:return: Output Tensor
"""
raise NotImplementedError()