-
Notifications
You must be signed in to change notification settings - Fork 23
/
lightning.py
224 lines (177 loc) · 7.26 KB
/
lightning.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
from argparse import ArgumentParser
from test_tube import Experiment, HyperOptArgumentParser
from .model import NeuralProcess
from neural_processes.lightning import PL_Seq2Seq
from neural_processes.utils import ObjectDict
class PL_NeuralProcess(PL_Seq2Seq):
"""Base class with everything off."""
def __init__(self, hparams,
MODEL_CLS=NeuralProcess.FROM_HPARAMS, **kwargs):
super().__init__(hparams,
MODEL_CLS=MODEL_CLS, **kwargs)
DEFAULT_ARGS = {
'dropout': 0.1,
'learning_rate': 0.003,
'attention_dropout': 0.5,
'batchnorm': False,
'attention_layers': 2,
'det_enc_cross_attn_type': 'uniform',
'det_enc_self_attn_type': 'uniform',
'latent_enc_self_attn_type': 'uniform',
'num_heads_power': 2,
'hidden_dim_power': 6,
'latent_dim_power': 5,
'n_latent_encoder_layers': 3,
'n_det_encoder_layers': 3,
'n_decoder_layers': 4,
'use_deterministic_path': True,
'use_lvar': False,
'use_self_attn': False,
'use_rnn': False,
'bnorm_inputs': True
}
USR_ATTRS_DEFAULT = {
'batch_size': 16,
'grad_clip': 40,
'max_nb_epochs': 200,
'num_workers': 4,
'num_context': 24* 4,
'vis_i': '670',
'num_extra_target': 24*4,
'x_dim': 18,
'context_in_target': False,
'y_dim': 1,
'patience': 3,
'min_std': 0.005,
}
@staticmethod
def add_suggest(trial, user_attrs={}):
trial.suggest_loguniform("learning_rate", 1e-6, 1e-2)
trial.suggest_int("attention_layers", 1, 4)
trial.suggest_discrete_uniform("num_heads_power", 2, 4, 1)
trial.suggest_discrete_uniform(
"hidden_dim_power", 4, 11, 1
)
trial.suggest_discrete_uniform(
"latent_dim_power", 4, 11, 1
)
trial.suggest_int("n_latent_encoder_layers", 1, 12)
trial.suggest_int("n_det_encoder_layers", 1, 12)
trial.suggest_int("n_decoder_layers", 1, 12)
trial.suggest_uniform("dropout", 0, 0.9)
trial.suggest_uniform("attention_dropout", 0, 0.9)
trial.suggest_categorical(
"latent_enc_self_attn_type", ['uniform', 'multihead', 'ptmultihead']
)
trial.suggest_categorical("det_enc_self_attn_type", ['uniform', 'multihead', 'ptmultihead'])
trial.suggest_categorical("det_enc_cross_attn_type", ['uniform', 'multihead', 'ptmultihead'])
trial.suggest_categorical("batchnorm", [False, True])
trial.suggest_categorical("use_self_attn", [False, True])
trial.suggest_categorical("use_lvar", [False, True])
trial.suggest_categorical("use_deterministic_path", [False, True])
trial.suggest_categorical("use_rnn", [True, False])
[trial.set_user_attr(k, v) for k, v in PL_NeuralProcess.USR_ATTRS_DEFAULT.items()]
[trial.set_user_attr(k, v) for k, v in user_attrs.items()]
return trial
class PL_NP(PL_NeuralProcess):
"""Vanilla NP with no attention or RNN."""
def __init__(self, hparams,
MODEL_CLS=NeuralProcess.FROM_HPARAMS, **kwargs):
super().__init__(hparams,
MODEL_CLS=MODEL_CLS, **kwargs)
DEFAULT_ARGS = {
**PL_NeuralProcess.DEFAULT_ARGS,
'det_enc_cross_attn_type': 'uniform',
'det_enc_self_attn_type': 'uniform',
'latent_enc_self_attn_type': 'uniform',
'use_deterministic_path': False,
}
@staticmethod
def add_suggest(trial, user_attrs={}):
trial.suggest_loguniform("learning_rate", 1e-6, 1e-2)
trial.suggest_discrete_uniform(
"hidden_dim_power", 3, 11, 1
)
trial.suggest_discrete_uniform(
"latent_dim_power", 3, 11, 1
)
trial.suggest_int("n_latent_encoder_layers", 1, 12)
trial.suggest_int("n_decoder_layers", 1, 12)
trial.suggest_uniform("dropout", 0, 0.9)
trial.suggest_categorical("batchnorm", [False, True])
[trial.set_user_attr(k, v) for k, v in PL_NeuralProcess.USR_ATTRS_DEFAULT.items()]
[trial.set_user_attr(k, v) for k, v in user_attrs.items()]
return trial
class PL_ANP(PL_NeuralProcess):
def __init__(self, hparams,
MODEL_CLS=NeuralProcess.FROM_HPARAMS, **kwargs):
super().__init__(hparams,
MODEL_CLS=MODEL_CLS, **kwargs)
DEFAULT_ARGS = {
**PL_NeuralProcess.DEFAULT_ARGS,
'det_enc_cross_attn_type': 'multihead',
'det_enc_self_attn_type': 'multihead',
'latent_enc_self_attn_type': 'multihead',
'use_self_attn': True,
'use_deterministic_path': True,
}
@staticmethod
def add_suggest(trial, user_attrs={}):
trial.suggest_loguniform("learning_rate", 1e-6, 1e-2)
trial.suggest_discrete_uniform("num_heads_power", 2, 4, 1)
trial.suggest_discrete_uniform(
"hidden_dim_power", 4, 11, 1
)
trial.suggest_discrete_uniform(
"latent_dim_power", 4, 11, 1
)
trial.suggest_int("n_latent_encoder_layers", 1, 12)
trial.suggest_int("n_det_encoder_layers", 1, 12)
trial.suggest_int("n_decoder_layers", 1, 12)
trial.suggest_uniform("dropout", 0, 0.9)
trial.suggest_uniform("attention_dropout", 0, 0.9)
trial.suggest_categorical("batchnorm", [False, True])
trial.suggest_categorical("use_deterministic_path", [False, True])
[trial.set_user_attr(k, v) for k, v in PL_NeuralProcess.USR_ATTRS_DEFAULT.items()]
[trial.set_user_attr(k, v) for k, v in user_attrs.items()]
return trial
class PL_ANPRNN(PL_NeuralProcess):
"""
Recurrent Attentive Neural Process for Sequential Data.
https://arxiv.org/abs/1910.09323
"""
def __init__(self, hparams,
MODEL_CLS=NeuralProcess.FROM_HPARAMS, **kwargs):
super().__init__(hparams,
MODEL_CLS=MODEL_CLS, **kwargs)
DEFAULT_ARGS = {
**PL_NeuralProcess.DEFAULT_ARGS,
'det_enc_cross_attn_type': 'multihead',
'det_enc_self_attn_type': 'multihead',
'latent_enc_self_attn_type': 'multihead',
'use_self_attn': True,
'use_rnn': True,
}
@staticmethod
def add_suggest(trial, user_attrs={}):
trial.suggest_loguniform("learning_rate", 1e-6, 1e-2)
trial.suggest_discrete_uniform("num_heads_power", 2, 4, 1)
trial.suggest_discrete_uniform(
"hidden_dim_power", 4, 11, 1
)
trial.suggest_discrete_uniform(
"latent_dim_power", 4, 11, 1
)
trial.suggest_int("n_latent_encoder_layers", 1, 12)
trial.suggest_int("n_det_encoder_layers", 1, 12)
trial.suggest_int("n_decoder_layers", 1, 12)
trial.suggest_uniform("dropout", 0, 0.9)
trial.suggest_uniform("attention_dropout", 0, 0.9)
trial.suggest_categorical("batchnorm", [False, True])
trial.suggest_categorical("use_deterministic_path", [False, True])
[trial.set_user_attr(k, v) for k, v in PL_NeuralProcess.USR_ATTRS_DEFAULT.items()]
[trial.set_user_attr(k, v) for k, v in user_attrs.items()]
return trial