-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpytorch_modules.py
executable file
·670 lines (528 loc) · 19 KB
/
pytorch_modules.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import numpy as np
import torch
import torch.utils.data
from torch.distributions import Normal, kl_divergence
from networks.VAE_net import encoder_cmr, decoder_cmr, encoder_fundus, decoder_fundus
from mcvae import utilities, diagnostics
# TODO: write a better initialization for DEVICE, considering the available resources.
DEVICE_ID = 0
DEVICE = torch.device('cuda:' + str(DEVICE_ID) if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
print('Using cuda')
torch.cuda.set_device(DEVICE_ID)
dtype = torch.cuda.FloatTensor
else:
print('Using CPU')
dtype = torch.FloatTensor
# pi = torch.FloatTensor([np.pi]).to(DEVICE) # torch.Size([1])
pi = torch.FloatTensor([np.pi]).type(dtype)
log_2pi = torch.log(2 * pi)
BCELoss = torch.nn.BCELoss(reduction='none')
BCELoss = BCELoss.type(dtype)
def reconstruction_error(predicted, target):
# sum over data dimensions (n_feats); average over observations (N_obs)
if len(target.shape) < 4:
# This is for demographic data
recon_error = ((target - predicted) ** 2).sum(1).mean()
else:
# This is for image data
recon_error = ((target - predicted) ** 2).sum((1,2,3)).mean()
return recon_error
def mae(predicted, target):
"""
Mean Absolute Error
"""
# sum over data dimensions (n_feats); average over observations (N_obs)
if len(target.shape) < 4:
# This is for demographic data
mae_err = torch.abs(target - predicted).sum(1).mean()
else:
# This is for image data
mae_err = torch.abs(target - predicted).sum((1,2,3)).mean()
return mae_err
# def LL(predicted, target, logvar, index_dict=None):
# '''
# Computes Log-Likelihood.
#
# If X represents N observations of a scalar variable x, then
# ln p(X|mu,var) = -1/(2*var) sum_{n=1}^N( (x_n - mu)^2 -N/2 logvar - N/2 log(2*pi) )
#
# Remember: the maximum likelihood approach systematically underestimates the variance of the distribution.
#
# See Bishop PRML pag.27
#
# :param predicted: double tensor (Nob_s x N_features)
# :param target: double tensor (same size as 'predicted')
# :param logvar: single logvariance associated to each feature (size = N_features
# :return: scalar value containing total log-likelihood
# '''
# if index_dict is None:
# ll = -0.5 * ((predicted - target) ** 2 / logvar.exp() + logvar + log_2pi)
# else:
# ll = -0.5 * ((predicted[index_dict['left'], :] - target[index_dict['right'], :]) ** 2 / logvar.exp() + logvar + log_2pi)
# # sum over data dimensions (n_feats); average over observations (N_obs)
# return ll.sum(1).mean(0)
def KL(mu, logvar):
'''
Solution of KL(qφ(z)||pθ(z)), Gaussian case:
Sum over the dimensionality of z
KL = - 0.5 * sum(1 + log(σ^2) - µ^2 - σ^2)
When using a recognition model qφ(z|x) then µ and σ are simply functions of x and the variational parameters φ
See Appendix B from VAE paper:
Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
https://arxiv.org/abs/1312.6114
'''
kl = -0.5 * (1 + logvar - mu.pow(2) - logvar.exp())
return kl
def compute_log_alpha(mu, logvar):
# clamp because dropout rate p in 0-99%, where p = alpha/(alpha+1)
return (logvar - 2 * torch.log(torch.abs(mu) + 1e-8)).clamp(min=-8, max=8)
def compute_logvar(mu, log_alpha):
return log_alpha + 2 * torch.log(torch.abs(mu) + 1e-8)
def compute_clip_mask(mu, logvar, thresh=3):
# thresh < 3 means p < 0.95
# clamp dropout rate p in 0-99%, where p = alpha/(alpha+1)
log_alpha = compute_log_alpha(mu, logvar)
return (log_alpha < thresh).float()
def KL_log_uniform(p, *args, **kwargs):
"""
Arguments other than 'p' are ignored.
Formula from Paragraph 4.2 in:
Variational Dropout Sparsifies Deep Neural Networks
Molchanov, Dmitry; Ashukha, Arsenii; Vetrov, Dmitry
https://arxiv.org/abs/1701.05369
"""
log_alpha = compute_log_alpha(p.loc, p.scale.pow(2).log())
k1, k2, k3 = 0.63576, 1.8732, 1.48695
neg_KL = k1 * torch.sigmoid(k2 + k3 * log_alpha) - 0.5 * torch.log1p(torch.exp(-log_alpha)) - k1
return -neg_KL
def loss_has_converged(x, N=500, stop_slope=-0.01):
"""
True if loss function started to oscillate or small incrementation
True also if oscillations are big; that is why you need "loss_has_diverged()"
"""
if stop_slope is None:
return False
L = len(x)
if L < 1002:
return False
else:
# slope of the loss function, moving averaged on a window of N epochs
rate = [np.polyfit(np.arange(N), x[i:i + N], deg=1)[0] for i in range(L - N)]
oscillations = (np.array(rate) > 0).sum()
if oscillations > 1:
pass
print("{} oscillations (> 1)".format(str((np.array(rate) > 0).sum())))
if np.mean(rate[-100:-1]) > stop_slope:
pass
print("Sl. {}. Slope criteria reached (sl > {})".format(np.mean(rate[-100:-1]), stop_slope))
return oscillations > 1 or np.mean(rate[-100:-1]) > stop_slope
def loss_has_diverged(x):
return x[-1] > x[0]
def loss_is_nan(x):
return str(x[-1]) == 'nan'
def moving_average(x, n=1):
return [np.mean(x[i - n:i]) for i in range(n, len(x))]
class MultiChannelBase(torch.nn.Module):
def __init__(
self,
lat_dim=1,
n_channels=1,
n_feats=(1,),
noise_init_logvar=3,
model_name_dict=None,
opt={},
):
super().__init__()
assert n_channels == len(n_feats)
self.lat_dim = lat_dim
self.n_channels = n_channels
self.n_feats = n_feats
self.noise_init_logvar = noise_init_logvar
self.model_name_dict = model_name_dict
self.opt = opt
self.init_names()
self.init_encoder()
self.init_decoder()
self.init_KL()
def init_names(self):
self.model_name = self._get_name()
if not self.model_name_dict == None:
for key in sorted(self.model_name_dict):
val = self.model_name_dict[key]
if type(val) == list or type(val) == tuple:
# val = '_'.join([str(i) for i in val])
val = str(np.sum(val))
self.model_name += '__' + key + '_' + str(val)
self.ch_name = ['Ch.' + str(i) for i in range(self.n_channels)]
self.varname = []
for ch in self.n_feats:
self.varname.append(['feat.' + str(j) for j in range(self.n_feats[ch][0])])
# Method for weight initialization
def weights_init_normal(self, m):
classname = m.__class__.__name__
if classname.find("Conv2d") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find("ConvTranspose2d") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
elif classname.find("BatchNorm2d") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
def init_encoder(self):
# Encoders: random initialization of weights
W_mu = []
W_logvar = []
for ch in self.n_feats:
# The fully connected linear encoding must be replaced by the encoder you wish.
# Be careful, because we are in a for loop across channels.
# This means that you should define different encoders for diffferent channels
# bias = False
# W_mu.append(torch.nn.Linear(self.n_feats[ch], self.lat_dim, bias=bias))
# W_logvar.append(torch.nn.Linear(self.n_feats[ch], self.lat_dim, bias=bias))
# Fundus/CMR implementation
if ch == 'fundus':
# print('\n' + ch + ' encoder loaded')
W_mu.append(torch.nn.DataParallel(encoder_fundus(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
W_logvar.append(torch.nn.DataParallel(encoder_fundus(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
elif ch == 'cmr':
# print('\n' + ch + ' encoder loaded')
W_mu.append(torch.nn.DataParallel(encoder_cmr(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
W_logvar.append(torch.nn.DataParallel(encoder_cmr(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
# print(W_mu)
self.W_mu = torch.nn.ModuleList(W_mu).apply(self.weights_init_normal)
self.W_mu = self.W_mu.type(dtype)
self.W_logvar = torch.nn.ModuleList(W_logvar).apply(self.weights_init_normal)
self.W_logvar = self.W_logvar.type(dtype)
def init_decoder(self):
# Decoders: random initialization of weights
W_out = []
W_out_logvar = []
for ch in self.n_feats:
# The fully connected linear decoding must be replaced by the decoder you wish.
# Be careful, because we are in a for loop across channels.
# This means that you should define different decoders for diffferent channels
# bias = False
# W_out.append(torch.nn.Linear(self.lat_dim, self.n_feats[ch], bias=bias))
# W_out_logvar.append(torch.nn.Parameter(torch.FloatTensor(1, self.n_feats[ch]).fill_(self.noise_init_logvar)))
# Fundus/CMR implementation
if ch == 'fundus':
# print('\n' + ch + ' decoder loaded')
W_out.append(torch.nn.DataParallel(decoder_fundus(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
# This should be the size of the input channels torch.FloatTensor(self.n_feats[ch][0], self.n_feats[ch][2], self.n_feats[ch][2])
W_out_logvar.append(torch.nn.Parameter(torch.FloatTensor(self.n_feats[ch][0], self.n_feats[ch][2], self.n_feats[ch][2]).fill_(self.noise_init_logvar)))
elif ch == 'cmr':
# print('\n' + ch + ' decoder loaded')
W_out.append(torch.nn.DataParallel(decoder_cmr(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
# This should be the size of the input channels torch.FloatTensor(self.n_feats[ch][0], self.n_feats[ch][2], self.n_feats[ch][2])
W_out_logvar.append(torch.nn.Parameter(torch.FloatTensor(self.n_feats[ch][0], self.n_feats[ch][2], self.n_feats[ch][2]).fill_(self.noise_init_logvar)))
self.W_out = torch.nn.ModuleList(W_out).apply(self.weights_init_normal)
# self.Wij = Wij should be defined for each intersection. From fundus to demographic, from demographic to fundus, from cmr to demographic and from demographic to cmr
self.W_out = self.W_out.type(dtype)
self.W_out_logvar = torch.nn.ParameterList(W_out_logvar).apply(self.weights_init_normal)
self.W_out_logvar = self.W_out_logvar.type(dtype)
def init_KL(self):
self.KL_fn = kl_divergence
def encode(self, x):
qzx = []
for i, xi in enumerate(x):
q = Normal(
loc=self.W_mu[i](xi),
scale=self.W_logvar[i](xi).exp().pow(0.5)
)
qzx.append(q)
del q
return qzx
def sample_from(self, qzx):
'''
sampling by leveraging on the reparametrization trick
'''
zx = []
for ch in range(self.n_channels):
if self.training:
zx.append(
qzx[ch].rsample()
)
else:
zx.append(
qzx[ch].loc
)
return zx
def decode(self, zx):
pxz = []
for i in range(self.n_channels):
pxz.append([])
for j in range(self.n_channels):
p_out = Normal(
# inter_z = self.W_out[j](Wij(zx[i])) # latent transformation. Define Wij for each pair of cross decoding. Define this where Wout is defined.
loc= self.W_out[j](zx[i]),
scale=self.W_out_logvar[j].exp().pow(0.5)
)
pxz[i].append(p_out)
del p_out
return pxz
def forward(self, x):
qzx = self.encode(x)
zx = self.sample_from(qzx)
pxz = self.decode(zx)
return {
'x': x,
'qzx': qzx,
'zx': zx,
'pxz': pxz
}
def reconstruct(self, x, reconstruct_from=None):
available_channels = range(self.n_channels) if reconstruct_from is None else reconstruct_from
fwd_return = self.forward(x)
pxz = fwd_return['pxz']
Xhat = []
for c in range(self.n_channels):
# mean along the stacking direction
xhat = torch.stack([pxz[e][c].loc.detach() for e in available_channels]).mean(0)
Xhat.append(xhat)
del xhat
return Xhat
def optimize_batch(self, local_batch):
pred = self.forward(local_batch)
loss = self.loss_function(pred)
# loss = self.recon_loss(pred) ##### Added recon_loss! March 17, 2020, Andres Diaz-Pinto
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
return loss.detach().item()
def optimize(self, epochs, data, *args, **kwargs):
self.train() # Inherited method which sets self.training = True
try: # list of epochs to allow more training
so_far = len(self.loss['total'])
self.epochs[-1] = so_far
self.epochs.append(so_far + epochs)
except AttributeError:
self.epochs = [0, epochs]
print('\nOptimising model ...')
for epoch in range(self.epochs[-2], self.epochs[-1]):
if type(data) is torch.utils.data.DataLoader:
current_batch = 0
for gen_batch in data:
local_batch = []
for idx in range(len(gen_batch)-2): # All data except metadata and img_names
local_batch.append(gen_batch[idx].type(dtype))
# print("\nBatch # {} / {}".format(current_batch, len(data) - 1), end='\t')
# print(gen_batch[idx+1]) # print batch img names
loss = self.optimize_batch(local_batch)
current_batch += 1
else:
loss = self.optimize_batch(data)
if np.isnan(loss):
print('Loss is nan!')
break
if epoch % 1 == 0:
self.print_loss(epoch)
if loss_has_diverged(self.loss['total']):
print('Loss diverged!')
# break ################ Letting continue the training even if the oscillation is big. ####################
# Saving model
if epoch % self.opt.save_model == 0 and epoch > 0:
utilities.save_model(self, filename = self.opt.dir_results + 'model_' + str(epoch) + '_epochs_dict')
diagnostics.plot_loss(self, save_fig=True, path_plot = self.opt.dir_results)
diagnostics.save_img_sample(self, path_plot = self.opt.dir_results, opt = self.opt)
self.eval() # Inherited method which sets self.training = False
def loss_function(self, fwd_return):
x = fwd_return['x']
qzx = fwd_return['qzx']
pxz = fwd_return['pxz']
kl = 0
ll = 0
for i in range(self.n_channels):
# KL Divergence
kl += self.KL_fn(qzx[i], Normal(0, 1)).sum(1).mean()
for j in [1]: # This is to reconstruct only CMR # range(self.n_channels): # [2]: ####### Try only the index for the demographic data. ##########
# i = latent comp; j = decoder
# Direct (i=j) and Crossed (i!=j) Log-Likelihood
ll_temp = pxz[i][j].log_prob(x[j])
# This is for image data. Sum along multiple axes
ll_temp = ll_temp.sum((1,2,3)).mean()
ll += ll_temp
total = kl - ll
losses = {
'total': total,
'kl': kl,
'll': ll
}
if self.training:
self.save_loss(losses)
return total
else:
return losses
def recon_loss(self, fwd_return, target=None):
x = fwd_return['x'] if target is None else target
qzx = fwd_return['qzx']
pxz = fwd_return['pxz']
kl = 0
ll = 0
rec_loss = 0
mae_loss = 0
for i in range(self.n_channels):
# KL Divergence
kl += self.KL_fn(qzx[i], Normal(0, 1)).sum(1).mean()
for j in range(self.n_channels):
# i = latent comp; j = decoder
# Direct (i=j) and Crossed (i!=j) Log-Likelihood
ll_temp = pxz[i][j].log_prob(x[j])
# This is for image data. Sum along multiple axes
ll_temp = ll_temp.sum((1,2,3)).mean()
ll += ll_temp
rec_loss += reconstruction_error(target=x[j], predicted=pxz[i][j].loc)
mae_loss += mae(target=x[j], predicted=pxz[i][j].loc)
total = kl - ll
losses = {
'total': total,
'kl': kl,
'll': ll,
'rec_loss': rec_loss,
'mae': mae_loss
}
return losses
def init_loss(self):
empty_loss = {
'total': [],
'kl': [],
'll': []
}
self.loss = empty_loss
def print_loss(self, epoch):
print('====> Epoch: {:4d}/{} ({:.0f}%)\tLoss: {:.4f}\tLL: {:.4f}\tKL: {:.4f}\tLL/KL: {:.4f}'.format(
epoch,
self.epochs[-1],
100. * (epoch) / self.epochs[-1],
self.loss['total'][-1],
self.loss['ll'][-1],
self.loss['kl'][-1],
self.loss['ll'][-1] / (1e-8 + self.loss['kl'][-1])
), end='\n')
def save_loss(self, losses):
for key in self.loss.keys():
self.loss[key].append(float(losses[key].detach().item()))
class MultiChannelSparseVAE(MultiChannelBase):
def __init__(
self,
dropout_threshold=0.2,
*args, **kwargs,
):
super().__init__(*args, **kwargs)
self.dropout_threshold = dropout_threshold
def init_encoder(self):
# Encoders: random initialization of weights
W_mu = []
for ch in self.n_feats:
# The fully connected linear encoding must be replaced by the encoder you wish.
# Be careful, because we are in a for loop across channels.
# This means that you should define different encoders for diffferent channels
# W_mu.append(torch.nn.Linear(self.n_feats[ch], self.lat_dim, bias=bias))
# bias = False
# Fundus/CMR implementation
if ch == 'fundus':
# print('\n' + ch + ' encoder loaded')
W_mu.append(torch.nn.DataParallel(encoder_fundus(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
elif ch == 'cmr':
# print('\n' + ch + ' encoder loaded')
W_mu.append(torch.nn.DataParallel(encoder_cmr(self.n_feats[ch][0], self.n_feats[ch][1], self.lat_dim)))
self.W_mu = torch.nn.ModuleList(W_mu).apply(self.weights_init_normal)
self.W_mu = self.W_mu.type(dtype)
self.log_alpha = torch.nn.Parameter(torch.FloatTensor(1, self.lat_dim).normal_(0, 0.01).type(dtype))
def init_KL(self):
self.KL_fn = KL_log_uniform
def encode(self, x):
"""
z ~ N( z | mu, alpha * mu^2 )
"""
qzx = []
for i, xi in enumerate(x):
mu = self.W_mu[i](xi)
logvar = compute_logvar(mu, self.log_alpha)
q = Normal(
loc=mu,
scale=logvar.exp().pow(0.5)
)
qzx.append(q)
del mu, logvar, q
return qzx
def dropout_fn(self, lv):
alpha = torch.exp(self.log_alpha.detach())
do = alpha / (alpha + 1)
lv_out = []
for ch in range(self.n_channels):
lv_out.append(lv[ch] * (do < self.dropout_threshold).float())
return lv_out
def forward(self, x):
qzx = self.encode(x)
zx = self.sample_from(qzx)
if self.training:
pxz = self.decode(zx)
else:
pxz = self.decode(self.dropout_fn(zx))
return {
'x': x,
'qzx': qzx,
'zx': zx,
'pxz': pxz
}
@property
def dropout(self):
alpha = torch.exp(self.log_alpha.detach())
return alpha / (alpha + 1)
@property
def kept_components(self):
keep = (self.dropout.reshape(-1) < self.dropout_threshold).tolist()
components = [i for i, kept in enumerate(keep) if kept]
return components
def logvar(self, x):
qzx = self.encode(x)
logvar = []
for ch in range(self.n_channels):
mu = qzx[ch].loc
lv = compute_logvar(mu, self.log_alpha)
logvar.append(lv.detach())
return logvar
class ScenarioGenerator(torch.nn.Module):
def __init__(
self,
lat_dim=1,
n_channels=1,
n_feats=1,
seed=100
):
"""
Generate multiple sources (channels) of data through a linear generative model:
z ~ N(0,I)
for ch in N_channels:
x_ch = W_ch(z)
where:
"W_ch" is an arbitrary linear mapping z -> x_ch
:param lat_dim:
:param n_channels:
:param n_feats:
"""
super().__init__()
self.lat_dim = lat_dim
self.n_channels = n_channels
self.n_feats = n_feats
# Save random state (http://archive.is/9glXO)
np.random.seed(seed) # or None
self.random_state = np.random.get_state() # get the initial state of the RNG
W = []
for ch in range(n_channels):
w_ = np.random.uniform(-1, 1, (n_feats, lat_dim))
u, s, vt = np.linalg.svd(w_, full_matrices=False)
w = u if n_feats >= lat_dim else vt
W.append(torch.nn.Linear(lat_dim, n_feats, bias=False))
W[ch].weight.data = torch.FloatTensor(w)
self.W = torch.nn.ModuleList(W)
def forward(self, z):
if type(z) == np.ndarray:
z = torch.FloatTensor(z)
assert z.size(1) == self.lat_dim
obs = []
for ch in range(self.n_channels):
x = self.W[ch](z)
obs.append(x.detach())
return obs