-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathforecasters.py
144 lines (125 loc) · 5.83 KB
/
forecasters.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
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torch.nn import init
from torchdiffeq import odeint
from functools import partial
from torch import optim
import math
import numbers
import functools
from networks import *
class DerivativeEstimatorMultiEnv(nn.Module):
def __init__(self, left_model, right_model, n_env, decomp_type, ignore_right=False):
super().__init__()
assert isinstance( left_model, nn.ModuleList)
assert isinstance( right_model, nn.ModuleList)
self.left_model = left_model
self.right_model = right_model
self.decomp_type = decomp_type
self.env = None
self.enable_right = None
self.ignore_right = ignore_right
self.n_env = n_env
def set_env(self, env):
self.env = env
def set_enable_right(self, right: bool):
self.enable_right = right
def forward(self, t, u):
left_res, right_res = None, None
if self.decomp_type == 'leads_decomp':
# 1 model on the left, n_env models on the right
assert len(self.left_model) == 1
assert self.env is not None
left_res = self.left_model[0](u)
right_res = self.right_model[self.env](u)
elif self.decomp_type == 'one_for_all':
# 1 model on the left, 1 model on the right
# This case is adapted to experments with changing environments
assert len(self.left_model) == len(self.right_model)
change_every = self.n_env // len(self.left_model)
left_res = self.left_model[self.env // change_every](u)
right_res = self.right_model[self.env // change_every](u)
elif self.decomp_type == 'one_per_env':
# n_env models on the left, n_env models on the right
assert len(self.left_model) == len(self.right_model)
assert self.env is not None
left_res = self.left_model[self.env](u)
right_res = self.right_model[self.env](u)
else:
change_every = len(self.right_model) // len(self.left_model)
left_res = self.left_model[self.env // change_every](u)
right_res = self.right_model[self.env](u)
if right_res is not None and (self.enable_right and not self.ignore_right):
return left_res + right_res
else:
return left_res
class Forecaster(nn.Module):
def __init__(self, in_c, out_c, n_env, hidden, net_type, n_left=None, n_right=None, options=None, factor=1., method=None, decomp_type=None, ignore_right=False):
super().__init__()
code_c = 2
if decomp_type == 'leads_decomp':
n_left = 1
n_right = n_env
elif decomp_type == 'one_for_all':
if n_left is None and n_right is None:
n_left = n_right = 1
else:
n_left = n_right
elif decomp_type == 'one_per_env':
n_left = n_right = n_env
else:
n_left = n_left
n_right = n_right
if net_type == 'mlp':
self.left_model = nn.ModuleList([MLPEstimator(in_c=in_c, out_c=out_c, hidden=hidden, factor=factor) for _ in range(n_left)])
self.right_model = nn.ModuleList([MLPEstimator(in_c=in_c, out_c=out_c, hidden=hidden, factor=factor) for _ in range(n_right)])
elif net_type == 'linear':
self.left_model = nn.ModuleList([Linear(in_c=in_c, out_c=out_c, factor=factor) for _ in range(n_left)])
self.right_model = nn.ModuleList([Linear(in_c=in_c, out_c=out_c, factor=factor) for _ in range(n_right)])
elif net_type in ['conv', 'fno']:
self.left_model = nn.ModuleList([ConvNetEstimator(in_c=in_c, out_c=out_c, hidden=hidden, factor=factor, net_type=net_type) for _ in range(n_left)])
self.right_model = nn.ModuleList([ConvNetEstimator(in_c=in_c, out_c=out_c, hidden=hidden, factor=factor, net_type=net_type) for _ in range(n_right)])
else:
raise NotImplementedError
self.derivative_estimator = DerivativeEstimatorMultiEnv(self.left_model, self.right_model, n_env=n_env, decomp_type=decomp_type, ignore_right=ignore_right)
self.method = method
self.options = options
self.int_ = odeint
self.net_type = net_type
def forward(self, y, t, env, enable_right=True, epsilon=None):
self.derivative_estimator.set_enable_right(enable_right)
self.derivative_estimator.set_env(env)
if epsilon is None:
y0 = y[:,:,0]
ret = self.int_(self.derivative_estimator, y0=y0, t=t, method=self.method, options=self.options)
else:
eval_points = np.random.random(len(t)) < epsilon
eval_points[-1] = False
start_i, end_i = 0, None
res = []
eval_points = eval_points[1:]
for i, eval_point in enumerate(eval_points):
if eval_point == True:
end_i = i+1
y0 = y[:,:,start_i]
t_seg = t[start_i:end_i+1]
res_seg = self.int_(self.derivative_estimator, y0=y0, t=t_seg, method=self.method, options=self.options)
if len(res) == 0:
res.append(res_seg)
else:
res.append(res_seg[1:])
start_i = end_i
end_i = None
y0 = y[:,:,start_i]
t_seg = t[start_i:]
res_seg = self.int_(self.derivative_estimator, y0=y0, t=t_seg, method=self.method, options=self.options)
if len(res) == 0:
res.append(res_seg)
else:
res.append(res_seg[1:])
ret = torch.cat(res, dim=0)
dim = y.dim()
dims = [1, 2, 0] + list(range(dim))[3:]
return ret.permute(*dims)