forked from Networks-Learning/counterfactual-continuous-mdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·275 lines (227 loc) · 10.9 KB
/
models.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Various helper network modules
"""
import torch
import torch.nn as nn
import torch.distributions as distributions
class RealNVP(distributions.Transform):
def __init__(self, input_size, hidden_size, device):
super(RealNVP, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
# Initialize networks
self.scale = nn.ModuleList([
nn.Sequential(
nn.Linear(input_size//2, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, input_size//2),
nn.Tanh()
),
nn.Sequential(
nn.Linear(input_size//2, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, input_size//2),
nn.Tanh()
)
]).to(device)
self.translation = nn.ModuleList([
nn.Sequential(
nn.Linear(input_size//2, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, input_size//2)
),
nn.Sequential(
nn.Linear(input_size//2, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, input_size//2)
)
]).to(device)
def forward(self, x):
# Apply transformation to first half of input tensor
if self.input_size == 0:
x_even = x
else:
x_even = x[:, 1:]
x_a, x_b = x_even[:, :self.input_size//2], x_even[:, self.input_size//2:]
scale_1 = self.scale[0](x_a)
translation_1 = self.translation[0](x_a)
y_b = (x_b * torch.exp(scale_1)) + translation_1
y_a = x_a
# Apply transformation to second half of transformed input tensor
scale_2 = self.scale[1](y_b)
translation_2 = self.translation[1](y_b)
y_a = (y_a * torch.exp(scale_2)) + translation_2
# Combine transformed and untransformed half of input tensor
y = torch.zeros_like(x, device=x.device)
if self.input_size == 0:
y[:, :self.input_size//2] = y_a
y[:, self.input_size//2:] = y_b
else:
y[:, 0] = x[:, 0]
y[:, 1:self.input_size//2+1] = y_a
y[:, self.input_size//2+1:] = y_b
# Compute log determinant of Jacobian
log_det_J = torch.sum(scale_1, dim=1) + torch.sum(scale_2, dim=1)
return y, log_det_J
def backward(self, y):
# Apply inverse transformation to second half of input tensor
if self.input_size == 0:
y_even = y
else:
y_even = y[:, 1:]
y_a, y_b = y_even[:, :self.input_size//2], y_even[:, self.input_size//2:]
scale_2 = self.scale[1](y_b)
translation_2 = self.translation[1](y_b)
y_a = (y_a - translation_2) * torch.exp(-scale_2)
# Apply inverse transformation to first half of transformed input tensor
x_a = y_a
scale_1 = self.scale[0](x_a)
translation_1 = self.translation[0](x_a)
x_b = (y_b - translation_1) * torch.exp(-scale_1)
# Combine transformed and untransformed half of input tensor
x = torch.zeros_like(y, device=y.device)
if self.input_size == 0:
x[:, :self.input_size//2] = x_a
x[:, self.input_size//2:] = x_b
else:
x[:, 0] = y[:, 0]
x[:, 1:self.input_size//2+1] = x_a
x[:, self.input_size//2+1:] = x_b
# Compute log determinant of Jacobian
log_det_J = -torch.sum(scale_1, dim=1) - torch.sum(scale_2, dim=1)
return x, log_det_J
def log_abs_det_jacobian(self, x, y):
_, log_det_J = self.realnvp.forward(x)
return log_det_J
class TransformedDistribution(distributions.Distribution):
def __init__(self, input_size, hidden_size, device):
super(TransformedDistribution, self).__init__(validate_args=False)
self.realnvp_transform = RealNVP(input_size, hidden_size, device)
self.gaussian = distributions.Normal(torch.zeros(input_size).to(device), torch.ones(input_size).to(device))
def sample(self, sample_shape=(1,)):
x = self.gaussian.sample(sample_shape)
y, _ = self.realnvp_transform.forward(x)
return y
def log_prob(self, value):
x, log_det_J = self.realnvp_transform.backward(value)
log_prob = self.gaussian.log_prob(x).sum(dim=1) + log_det_J
return torch.reshape(log_prob, (-1, 1))
class ParamMultivariateNormal(nn.Module):
def __init__(self, dim, device):
super(ParamMultivariateNormal, self).__init__()
self.dim = dim
self.device = device
self.raw_L = nn.Parameter(torch.eye(dim, device=device))
rows, cols = torch.tril_indices(self.dim, self.dim, device=device)
self.L_mask = torch.zeros_like(self.raw_L, device=device)
self.L_mask[rows, cols] = 1
def sample(self, sample_shape=(1,)):
L = torch.mul(self.raw_L, self.L_mask)
covariance = torch.matmul(L, L.t())
gaussian = distributions.MultivariateNormal(torch.zeros(self.dim).to(self.device), covariance.to(self.device))
u = gaussian.sample(sample_shape)
return u
def log_prob(self, value):
L = torch.mul(self.raw_L, self.L_mask)
covariance = torch.matmul(L, L.t())
gaussian = distributions.MultivariateNormal(torch.zeros(self.dim).to(self.device), covariance.to(self.device))
log_prob = gaussian.log_prob(value)
return torch.reshape(log_prob, (-1, 1))
class CustomSpectralLinear(nn.Module):
def __init__(self, in_features, out_features, a_dim=2, bias=True, n_power_iterations=1):
super(CustomSpectralLinear, self).__init__()
self.s_linear = nn.utils.parametrizations.spectral_norm(nn.Linear(in_features-a_dim, out_features, bias), n_power_iterations=n_power_iterations)
self.a_linear = nn.Linear(a_dim, out_features, bias=False)
self.a_dim = a_dim
def forward(self, x):
# pass the action columns through the a_liner layer
a = self.a_linear(x[:, :self.a_dim])
s = self.s_linear(x[:, self.a_dim:])
return a + s
class MultiplyConstantLayer(nn.Module):
def __init__(self, constant):
super(MultiplyConstantLayer, self).__init__()
self.constant = constant
def forward(self, x):
return x * self.constant.to(x.device)
class NeuralRegressor(nn.Module):
"""
neural network with nin inputs, nout outputs, nh hidden units per layer, nl layers, and leaky relu activation functions
if lipschitz is not None, layers include spectral normalization
if positive is True, the last activation function is a softplus
"""
def __init__(self, nin, nout, nl, nh, lipschitz=None, positive=False, a_dim=2, n_power_iterations=1):
super().__init__()
if lipschitz is not None:
multiplier = torch.pow(torch.tensor([lipschitz]), 1/(nl+1))
if nl==0:
if lipschitz is None:
layers = [nn.Linear(nin, nout)]
else:
layers = [CustomSpectralLinear(nin, nout, a_dim=a_dim, n_power_iterations=n_power_iterations), MultiplyConstantLayer(multiplier)]
if positive:
layers.append(nn.Softplus())
elif nl>0:
if lipschitz is None:
layers = [nn.Linear(nin, nh), nn.Tanh()]
else:
layers = [CustomSpectralLinear(nin, nh, a_dim=a_dim, n_power_iterations=n_power_iterations), MultiplyConstantLayer(multiplier), nn.Tanh()]
for _ in range(nl-1):
if lipschitz is None:
layers += [nn.Linear(nh, nh), nn.Tanh()]
else:
layers += [nn.utils.parametrizations.spectral_norm(nn.Linear(nh, nh), n_power_iterations=n_power_iterations), MultiplyConstantLayer(multiplier), nn.Tanh()]
if lipschitz is None:
layers += [nn.Linear(nh, nout)]
else:
layers += [nn.utils.parametrizations.spectral_norm(nn.Linear(nh, nout), n_power_iterations=n_power_iterations), MultiplyConstantLayer(multiplier)]
if positive:
layers.append(nn.Softplus())
self.lipschitz = lipschitz
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
class SCM(nn.Module):
"""
A single affine transformation SCM with a normal prior
"""
def __init__(self, s_dim, nl, nh, a_dim, c_dim, lipschitz_loc=None, lipschitz_scale=None, prior_type='gaussian', device='cpu', n_power_iterations=1):
super().__init__()
# initialize the NN models
self.a_dim = a_dim
self.s_dim_actionable = s_dim - c_dim # the first three features are not actionable in MIMIC (gender, re_admission, and age)
self.location_model = NeuralRegressor(a_dim + s_dim, self.s_dim_actionable, nl, nh, lipschitz=lipschitz_loc, a_dim=a_dim, n_power_iterations=n_power_iterations)
self.scale_model = NeuralRegressor(a_dim + s_dim, self.s_dim_actionable, nl, nh, lipschitz=lipschitz_scale, positive=True, a_dim=a_dim, n_power_iterations=n_power_iterations)
# initialize the prior distribution to a zero mean isotropic Gaussian
if prior_type == 'gaussian':
self.prior = torch.distributions.Normal(torch.zeros(self.s_dim_actionable).to(device), torch.ones(self.s_dim_actionable).to(device))
elif prior_type == 'laplace':
self.prior = torch.distributions.Laplace(torch.zeros(self.s_dim_actionable).to(device), torch.ones(self.s_dim_actionable).to(device))
elif prior_type == 'multigaussian':
self.prior = ParamMultivariateNormal(self.s_dim_actionable, device)
def forward(self, s, a, u):
# compute the location and scale of the distribution
s_prime = torch.zeros_like(s, device=s.device)
s_prime[:, :-self.s_dim_actionable] = s[:, :-self.s_dim_actionable]
concat_input = torch.cat([a, s], dim=1)
location = self.location_model(concat_input)
scale = self.scale_model(concat_input)
s_prime[:,-self.s_dim_actionable:] = scale * u + location
return s_prime
def backward(self, s, a, s_prime):
# compute the location and scale of the distribution
u = torch.zeros(s.shape[0], self.s_dim_actionable, device=s.device)
concat_input = torch.cat([a, s], dim=1)
location = self.location_model(concat_input)
scale = self.scale_model(concat_input)
u = (s_prime[:, -self.s_dim_actionable:] - location) / scale
log_det = - torch.sum(torch.log(scale), dim=1)
return u, log_det
def sample(self, s, a):
u = self.prior.sample((s.shape[0], ))
s_prime = self.forward(s, a, u)
return s_prime
def log_likelihood(self, s, a, s_prime):
u, log_det = self.backward(s, a, s_prime)
prior_logprob = self.prior.log_prob(u).sum(dim=1)
return prior_logprob+log_det