forked from miquelflorensa/miquelflorensa.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
200 lines (173 loc) · 6.81 KB
/
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
###############################################################################
# File: model.py
# Description: Diffrent example how to build a model in pytagi
# Authors: Luong-Ha Nguyen & James-A. Goulet
# Created: October 12, 2022
# Updated: November 26, 2022
# Contact: [email protected] & [email protected]
# Copyright (c) 2022 Luong-Ha Nguyen & James-A. Goulet. Some rights reserved.
###############################################################################
from pytagi import NetProp
from typing import Union
class MLP(NetProp):
"""Multi-layer perceptron"""
def __init__(self,
layers: list,
nodes: list,
activations: list,
batch_size: int,
sigma_v: Union[float , None] = None,
sigma_v_min: Union[float, None] = None,
noise_type: Union[str, None] = None,
noise_gain: Union[float, None] = None,
init_method: Union[str, None] = "He",
device: Union[str, None] = "cpu") -> None:
super().__init__()
self.layers = layers
self.nodes = nodes
self.activations = activations
self.batch_size = batch_size
if sigma_v is not None:
self.sigma_v = sigma_v
if sigma_v_min is not None:
self.sigma_v_min = sigma_v_min
if noise_type is not None:
self.noise_type = noise_type
if noise_gain is not None:
self.noise_gain = noise_gain
self.init_method: init_method
self.device = device
class RegressionMLP(NetProp):
"""Multi-layer perceptron for regression task"""
def __init__(self) -> None:
super().__init__()
self.layers = [1, 1, 1]
self.nodes = [1, 50, 1]
self.activations = [0, 4, 0]
self.batch_size = 4
self.sigma_v = 0.06
self.sigma_v_min: float = 0.06
self.device = "cpu"
class HeterosMLP(NetProp):
"""Multi-layer preceptron for regression task where the
output's noise varies overtime"""
def __init__(self) -> None:
super().__init__()
self.layers: list = [1, 1, 1, 1]
self.nodes: list = [1, 100, 100, 2] # output layer = [mean, std]
self.activations: list = [0, 4, 4, 0]
self.batch_size: int = 10
self.sigma_v: float = 0
self.sigma_v_min: float = 0
self.noise_type: str = "heteros"
self.noise_gain: float = 1.0
self.init_method: str = "He"
self.device: str = "cpu"
class DervMLP(NetProp):
"""Multi-layer perceptron for computing the derivative of a
regression task"""
def __init__(self) -> None:
super().__init__()
self.layers: list = [1, 1, 1, 1]
self.nodes: list = [1, 64, 64, 1]
self.activations: list = [0, 1, 4, 0]
self.batch_size: int = 10
self.sigma_v: float = 0.3
self.sigma_v_min: float = 0.1
self.decay_factor_sigma_v: float = 0.99
self.collect_derivative: bool = True
self.init_method: str = "He"
class FullCovMLP(NetProp):
"""Multi-layer perceptron for performing full-covariance prediction and
inference"""
def __init__(self) -> None:
super().__init__()
self.layers: list = [1, 1, 1, 1]
self.nodes: list = [1, 30, 30, 1]
self.activations: list = [0, 4, 4, 0]
self.batch_size: int = 10
self.sigma_v: float = 0.5
self.sigma_v_min: float = 0.065
self.decay_factor_sigma_v: float = 0.95
self.sigma_x: float = 0.3485
self.is_full_cov: bool = True
self.multithreading: bool = True
self.device: str = "cpu"
class MnistMLP(NetProp):
"""Multi-layer perceptron for mnist classificaiton.
NOTE: The number of hidden states for last layer is 11 because
TAGI use the hierarchical softmax for the classification task.
Further details can be found in
https://www.jmlr.org/papers/volume22/20-1009/20-1009.pdf
"""
def __init__(self) -> None:
super().__init__()
self.layers = [1, 1, 1, 1]
self.nodes = [784, 100, 100, 11]
self.activations = [0, 4, 4, 0]
self.batch_size = 10
self.sigma_v = 1
self.is_idx_ud = True
self.multithreading = True
self.device = "cpu"
class TimeSeriesLSTM(NetProp):
"""LSTM for time series forecasting"""
def __init__(self,
input_seq_len: int,
output_seq_len: int,
seq_stride: int = 1,
*args,
**kwargs) -> None:
super().__init__(*args, **kwargs)
self.layers: list = [1, 7, 7, 1]
self.nodes: list = [1, 5, 5, 1]
self.activations: list = [0, 0, 0, 0]
self.batch_size: int = 10
self.input_seq_len: int = input_seq_len
self.output_seq_len: int = output_seq_len
self.seq_stride: int = seq_stride
self.sigma_v: float = 2
self.sigma_v_min: float = 0.3
self.decay_factor_sigma_v: float = 0.95
self.multithreading: bool = False
self.device: str = "cpu"
class MnistEncoder(NetProp):
"""Encoder network for Mnist example"""
def __init__(self) -> None:
super().__init__()
self.layers: list = [2, 2, 6, 4, 2, 6, 4, 1, 1]
self.nodes: list = [784, 0, 0, 0, 0, 0, 0, 100, 10]
self.kernels: list = [3, 1, 3, 3, 1, 3, 1, 1, 1]
self.strides: list = [1, 0, 2, 1, 0, 2, 0, 0, 0]
self.widths: list = [28, 0, 0, 0, 0, 0, 0, 0, 0]
self.heights: list = [28, 0, 0, 0, 0, 0, 0, 0, 0]
self.filters: list = [1, 16, 16, 16, 32, 32, 32, 1, 1]
self.pads: list = [1, 0, 1, 1, 0, 1, 0, 0, 0]
self.pad_types: list = [1, 0, 2, 1, 0, 2, 0, 0, 0]
self.activations: list = [0, 4, 0, 0, 4, 0, 0, 4, 0]
self.batch_size: int = 10
self.is_output_ud: bool = False
self.init_method: str = "He"
self.device: str = "cuda"
class MnistDecoder(NetProp):
"""Decoder network for Mnist example"""
def __init__(self) -> None:
super().__init__()
self.layers: list = [1, 1, 21, 21, 21]
self.nodes: list = [10, 1568, 0, 0, 784]
self.kernels: list = [1, 3, 3, 3, 1]
self.strides: list = [0, 2, 2, 1, 0]
self.widths: list = [0, 7, 0, 0, 0]
self.heights: list = [0, 7, 0, 0, 0]
self.filters: list = [1, 32, 32, 16, 1]
self.pads: list = [0, 1, 1, 1, 0]
self.pad_types: list = [0, 2, 2, 1, 0]
self.activations: list = [0, 4, 4, 4, 0]
self.batch_size: int = 10
self.sigma_v: float = 8
self.sigma_v_min: float = 2
self.is_idx_ud: bool = False
self.last_backward_layer: int = 0
self.decay_factor_sigma_v: float = 0.95
self.init_method: str = "He"
self.device: str = "cuda"