-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
577 lines (500 loc) · 21.7 KB
/
base.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
from crypt import methods
import torch
from einops import rearrange
from torch import nn
from torchdiffeq import odeint_adjoint
from basehelper import *
def bmul(vec, mat, axis=0):
mat = mat.transpose(axis, -1)
return (mat * vec.expand_as(mat)).transpose(axis, -1)
def bmul(vec, mat, axis=0):
mat = mat.transpose(axis, -1)
return (mat * vec.expand_as(mat)).transpose(axis, -1)
class mu_net(nn.Module):
def __init__(self, input_dim, hid):
super(mu_net, self).__init__()
self.input_dim = input_dim
self.fc1 = nn.Linear(input_dim, hid)
self.fc2 = nn.Linear(hid, 1)
self.actv = nn.ReLU()
def forward(self, x):
mu_out = self.fc1(x)
mu_out = self.actv(mu_out)
mu_out = self.fc2(mu_out)
mu_out = torch.pow(mu_out, 2)
return mu_out[0][0][0] # silverbox
# return mu_out[0][0][0][0] # human activity
class Tinvariant_NLayerNN(NLayerNN):
def forward(self, t, x):
return super(Tinvariant_NLayerNN, self).forward(x)
class dfwrapper(nn.Module):
def __init__(self, df, shape, recf=None):
super(dfwrapper, self).__init__()
self.df = df
self.shape = shape
self.recf = recf
def forward(self, t, x):
bsize = x.shape[0]
if self.recf:
x = x[:, :-self.recf.osize].reshape(bsize, *self.shape)
dx = self.df(t, x)
dr = self.recf(t, x, dx).reshape(bsize, -1)
dx = dx.reshape(bsize, -1)
dx = torch.cat([dx, dr], dim=1)
else:
x = x.reshape(bsize, *self.shape)
dx = self.df(t, x)
dx = dx.reshape(bsize, -1)
return dx
class NODEintegrate(nn.Module):
def __init__(self, df, shape=None, tol=1e-5, adjoint=True, evaluation_times=None, recf=None, algebraic_from_differential=None, differential_from_algebraic=None, activation_h=None, activation_output=None, abs_second=None, time_requires_grad=True, verbose=True):
"""
Create an OdeRnnBase model
x' = df(x)
x(t0) = x0
:param df: a function that computes derivative. input & output shape [batch, channel, feature]
:param x0: initial condition.
- if x0 is set to be nn.parameter then it can be trained.
- if x0 is set to be nn.Module then it can be computed through some network.
"""
super().__init__()
self.df = dfwrapper(df, shape, recf) if shape else df
self.tol = tol
if verbose:
print(f"Tolerance: {self.tol}")
print(f"Adjoint:", adjoint)
self.odeint = torchdiffeq.odeint_adjoint if adjoint else torchdiffeq.odeint
self.evaluation_times = evaluation_times if evaluation_times is not None else torch.Tensor([0.0, 1.0])
if time_requires_grad == False:
self.evaluation_times.requires_grad = False
if verbose:
print("Evaluation times:", self.evaluation_times)
print("Learnable eval times:", self.evaluation_times.requires_grad)
self.shape = shape
self.recf = recf
self.algebraic_from_differential = algebraic_from_differential
self.activation_h = nn.Identity() if activation_h is None else activation_h
self.activation_output = nn.Identity() if activation_output is None else activation_output
self.abs_second = abs_second
if verbose:
print("self.algebraic_from_differential:", self.algebraic_from_differential)
print("self.activation_h for NODEintegrate:", self.activation_h)
if recf:
assert shape is not None
self.differential_from_algebraic = differential_from_algebraic
def forward(self, x0):
"""
Evaluate odefunc at given evaluation time
:param x0: shape [batch, channel, feature]. Set to None while training.
:param evaluation_times: time stamps where method evaluates, shape [time]
:param x0stats: statistics to compute x0 when self.x0 is a nn.Module, shape required by self.x0
:return: prediction by ode at evaluation_times, shape [time, batch, channel, feature]
"""
if self.differential_from_algebraic:
x, m = self.calc_differential_from_algebraic(x0, self.evaluation_times[0])
x0 = torch.cat((x, m), dim=1)
bsize = x0.shape[0]
if self.shape:
assert x0.shape[1:] == torch.Size(self.shape), \
'Input shape {} does not match with model shape {}'.format(x0.shape[1:], self.shape)
x0 = x0.reshape(bsize, -1)
if self.abs_second:
theta, m = torch.split(x0, 1, dim=1)
m = torch.abs(m)
x0 = torch.cat((theta, m), dim=1)
if self.recf:
reczeros = torch.zeros_like(x0[:, :1])
reczeros = repeat(reczeros, 'b 1 -> b c', c=self.recf.osize)
x0 = torch.cat([x0, reczeros], dim=1)
out = odeint(self.df, x0, self.evaluation_times, rtol=self.tol, atol=self.tol)
if self.algebraic_from_differential:
out = self.calc_algebraic_from_differential(out)
if self.recf:
rec = out[-1, :, -self.recf.osize:]
out = out[:, :, :-self.recf.osize]
out = out.reshape(-1, bsize, *self.shape)
return out, rec
else:
return out
else:
if self.abs_second:
theta, m = torch.split(x0, 1, dim=1)
m = torch.abs(m)
x0 = torch.cat((theta, m), dim=1)
out = odeint(self.df, x0, self.evaluation_times, rtol=self.tol, atol=self.tol)
if self.algebraic_from_differential:
out = self.calc_algebraic_from_differential(out)
return out
@property
def nfe(self):
return self.df.nfe
def to(self, device, *args, **kwargs):
super().to(device, *args, **kwargs)
self.evaluation_times.to(device)
def calc_algebraic_from_differential(self, z):
# split the input into the starting time step and the other time steps
z_0 = z[:1]
z_T = z[1:]
# get the corresponding value of t for the other time steps
if len(self.evaluation_times.shape) == 2:
T = self.evaluation_times[:, 1:]
else:
T = self.evaluation_times[1:]
x, m = torch.split(z_T, 1, dim=2)
# T^(-3/2) * e^(T/2)
k = torch.pow(T, -3/2) * torch.exp(T / 2)
if z.is_cuda:
k = k.to(z.get_device())
T = T.to(z.get_device())
# h(T) = [x(T) m(T)] * Transpose([T^(-3/2)*e^(T/2) I])
# h = x * k
# dh = k * (m - (3/2 * torch.pow(T, 1/2) * torch.exp(-T/2) - 1/2 * 1/k) * h)
k = self.activation_h(k)
h = self.activation_output(bmul(k, x))
dh = self.activation_output(bmul(k, m - bmul(3/2 * torch.pow(T, 1/2) * torch.exp(-T/2) - 1/2 * 1/k, h)))
z_t = torch.cat((h, dh), dim=2)
out = torch.cat((z_0, z_t), dim=0)
return out
def calc_differential_from_algebraic(self, z, t):
h, dh = torch.split(z, 1, dim=1)
k_reciprocal = 1 / (torch.pow(t, -self.nesterov_factor/2) * torch.exp(t/2))
if z.is_cuda:
k_reciprocal = k_reciprocal.to(z.get_device())
m = (self.nesterov_factor/2 * (1/t) * k_reciprocal - 1/2 * k_reciprocal) * h \
+ k_reciprocal * dh
x = h * k_reciprocal
return x, m
def trajectory(self, x, timesteps):
"""Returns ODE trajectory.
Parameters
----------
x : torch.Tensor
Shape (batch_size, self.odefunc.data_dim)
timesteps : int
Number of timesteps in trajectory.
"""
integration_time = torch.linspace(0., 1., timesteps)
return self.forward(x, eval_times=integration_time)
class NODElayer(NODEintegrate):
def forward(self, x0):
out = super(NODElayer, self).forward(x0)
if isinstance(out, tuple):
out, rec = out
return out[-1], rec
else:
return out[-1]
'''
class ODERNN(nn.Module):
def __init__(self, node, rnn, evaluation_times, nhidden):
super(ODERNN, self).__init__()
self.t = torch.as_tensor(evaluation_times).float()
self.n_t = len(self.t)
self.node = node
self.rnn = rnn
self.nhidden = (nhidden,) if isinstance(nhidden, int) else nhidden
def forward(self, x):
assert len(x) == self.n_t
batchsize = x.shape[1]
out = torch.zeros([self.n_t, batchsize, *self.nhidden]).to(x.device)
for i in range(1, self.n_t):
odesol = odeint(self.node, out[i - 1], self.t[i - 1:i + 1])
h_ode = odesol[1]
out[i] = self.rnn(h_ode, x[i])
return out
'''
class NODE(nn.Module):
def __init__(self, df=None, **kwargs):
super(NODE, self).__init__()
self.__dict__.update(kwargs)
self.df = df
self.nfe = 0
self.elem_t = None
def forward(self, t, x):
self.nfe += 1
if self.elem_t is None:
return self.df(t, x)
else:
return self.elem_t * self.df(self.elem_t, x)
def update(self, elem_t):
self.elem_t = elem_t.view(*elem_t.shape, 1)
class SONODE(NODE):
def forward(self, t, x):
"""
Compute [y y']' = [y' y''] = [y' df(t, y, y')]
:param t: time, shape [1]
:param x: [y y'], shape [batch, 2, vec]
:return: [y y']', shape [batch, 2, vec]
"""
self.nfe += 1
v = x[:, 1:, :]
out = self.df(t, x)
return torch.cat((v, out), dim=1)
class HeavyBallNODE(NODE):
def __init__(self, df, actv_h=None, gamma_guess=-3.0, gamma_act='sigmoid', corr=-100, corrf=True, sign=1):
super().__init__(df)
# Momentum parameter gamma
self.gamma = Parameter([gamma_guess], frozen=False)
self.gammaact = nn.Sigmoid() if gamma_act == 'sigmoid' else gamma_act
self.corr = Parameter([corr], frozen=corrf)
self.sp = nn.Softplus()
self.sign = sign # Sign of df
self.actv_h = nn.Identity() if actv_h is None else actv_h # Activation for dh, GHBNODE only
def forward(self, t, x):
"""
Compute [theta' m' v'] with heavy ball parametrization in
$$ h' = -m $$
$$ m' = sign * df - gamma * m $$
based on paper https://www.jmlr.org/papers/volume21/18-808/18-808.pdf
:param t: time, shape [1]
:param x: [theta m], shape [batch, 2, dim]
:return: [theta' m'], shape [batch, 2, dim]
"""
self.nfe += 1
h, m = torch.split(x, 1, dim=1)
dh = self.actv_h(- m)
dm = self.df(t, h) * self.sign - self.gammaact(self.gamma()) * m
dm = dm + self.sp(self.corr()) * h
out = torch.cat((dh, dm), dim=1)
if self.elem_t is None:
return out
else:
return self.elem_t * out
def update(self, elem_t):
self.elem_t = elem_t.view(*elem_t.shape, 1, 1)
HBNODE = HeavyBallNODE # Alias
class NesterovNODE(NODE):
def __init__(self, df, actv_h=None, corr=-100, corrf=True, use_h=False, full_details=False, algebraic_from_differential=True, actv_m=None, actv_dm=None, actv_df=None, sign=1):
super().__init__(df)
self.corr = Parameter([corr], frozen=corrf)
self.sp = nn.Softplus()
self.sign = sign # Sign of df
self.actv_h = nn.Identity() if actv_h is None else actv_h # Activation for dh, GNNODE only
self.actv_m = nn.Identity() if actv_m is None else actv_m # Activation for dh, GNNODE only
self.actv_dm = nn.Identity() if actv_dm is None else actv_dm # Activation for dh, GNNODE only
self.actv_df = nn.Identity() if actv_df is None else actv_df # Activation for df, GNNODE only
self.use_h = use_h
self.full_details = full_details
self.algebraic_from_differential = algebraic_from_differential
def forward(self, t, z):
"""
Compute [x' m'] with diff-alg nesterov parametrization in
$$ h' = -m $$
$$ m' = sign * df(t, h) - m - xi * h $$
:param t: time, shape [1]
:param z: [h dh], shape [batch, 2, dim]
:return: [x' m'], shape [batch, 2, dim]
"""
self.nfe += 1
x, m = torch.split(z, 1, dim=1)
dx = self.actv_h(m)
k = torch.pow(t, -3/2) * torch.exp(t/2)
h = x * self.actv_h(k)
dm = self.actv_df(self.df(t, h)) * self.sign - m
dm = self.actv_dm(self.actv_m(dm) - self.sp(self.corr()) * h)
out = torch.cat((dx, dm), dim=1)
if self.elem_t is None:
return out
else:
return self.elem_t * out
def update(self, elem_t):
self.elem_t = elem_t.view(*elem_t.shape, 1, 1)
NNODE = NesterovNODE # Alias
class RMSpropNODE(NODE):
def __init__(self, df, alpha=1e-1, actv_dtheta=None, corr=-100, corrf=True):
super().__init__(df)
self.alpha = alpha
self.corr = Parameter([corr], frozen=corrf)
self.sp = nn.Softplus()
self.actv_dtheta = nn.Identity() if actv_dtheta is None else actv_dtheta
def forward(self, t, x):
self.nfe += 1
theta, m = torch.split(x, 1, dim=1)
m = nn.Tanh()(torch.abs(m))
gtheta = self.df(t, theta) * self.df(t, m)
dtheta = -self.actv_dtheta(gtheta)
dm = 1 / self.alpha * (self.actv_dtheta(gtheta ** 2) - 1) * m - self.sp(self.corr()) * theta
out = torch.cat((dtheta, dm), dim=1)
if self.elem_t is None:
return out
else:
return self.elem_t * out
def update(self, elem_t):
self.elem_t = elem_t.view(*elem_t.shape, 1, 1)
class TOAES_NODE(NODE):
def __init__(self, df, actv_df=None, thetaact=None, learnable_mu = False, mu=1.0, corr=-100):
super().__init__(df)
# Momentum parameter gamma
self.actv_df = nn.Identity() if actv_df is None else actv_df
self.thetaact = nn.Identity() if thetaact is None else thetaact
self.mu = mu
self.learnable_mu = learnable_mu
self.sp = nn.Softplus()
self.corr = Parameter([corr], frozen=True)
# self.mu_net = mu_net(28, 10) # human activity
self.mu_net = mu_net(1, 10) # silverbox
def forward(self, t, x):
self.nfe += 1
h, p, q = torch.split(x, 1, dim=1)
if self.learnable_mu:
self.mu = self.mu_net(x)
f = self.df(t, h + 1/torch.sqrt(self.mu) * p)
dh = self.thetaact(p)
dp = self.thetaact(q)
dq = -3 * torch.sqrt(self.mu) * q - 2 * self.mu * p - torch.sqrt(self.mu) * self.actv_df(f) - self.sp(self.corr()) * h
out = torch.cat((dh, dp, dq), dim=1)
else:
f = self.df(t, h + 1/np.sqrt(self.mu) * p)
dh = self.thetaact(p)
dp = self.thetaact(q)
dq = -3 * np.sqrt(self.mu) * q - 2 * self.mu * p - np.sqrt(self.mu) * self.actv_df(f) - self.sp(self.corr()) * h
out = torch.cat((dh, dp, dq), dim=1)
if self.elem_t is None:
return out
else:
return self.elem_t * out
class ODE_RNN(nn.Module):
def __init__(self, ode, rnn, nhid, ic, rnn_out=False, both=False, tol=1e-7):
super().__init__()
self.ode = ode
self.t = torch.Tensor([0, 1])
self.nhid = [nhid] if isinstance(nhid, int) else nhid
self.rnn = rnn
self.tol = tol
self.rnn_out = rnn_out
self.ic = ic
self.both = both
def forward(self, t, x, multiforecast=None):
"""
--
:param t: [time, batch]
:param x: [time, batch, ...]
:return: [time, batch, *nhid]
"""
n_t, n_b = t.shape
h_ode = torch.zeros(n_t + 1, n_b, *self.nhid, device=x.device)
h_rnn = torch.zeros(n_t + 1, n_b, *self.nhid, device=x.device)
if self.ic:
h_ode[0] = h_rnn[0] = self.ic(rearrange(x, 't b c -> b (t c)')).view(h_ode[0].shape)
if self.rnn_out:
for i in range(n_t):
self.ode.update(t[i])
h_ode[i] = odeint(self.ode, h_rnn[i], self.t, atol=self.tol, rtol=self.tol)[-1]
h_rnn[i + 1] = self.rnn(h_ode[i], x[i])
out = (h_rnn,)
else:
for i in range(n_t):
self.ode.update(t[i])
h_rnn[i] = self.rnn(h_ode[i], x[i])
h_ode[i + 1] = odeint(self.ode, h_rnn[i], self.t, atol=self.tol, rtol=self.tol)[-1]
out = (h_ode,)
if self.both:
out = (h_rnn, h_ode)
if multiforecast is not None:
self.ode.update(torch.ones_like((t[0])))
forecast = odeint(self.ode, out[-1][-1], multiforecast * 1.0, atol=self.tol, rtol=self.tol)
out = (*out, forecast)
return out
class ODE_RNN_with_Grad_Listener(nn.Module):
def __init__(self, ode, rnn, nhid, ic, rnn_out=False, both=False, tol=1e-7, method="dopri5", evaluation_times=None, algebraic_from_differential=None, differential_from_algebraic=None, activation_h=None, time_requires_grad=True):
super().__init__()
self.ode = ode
self.evaluation_times = evaluation_times if evaluation_times is not None else torch.Tensor([0.0, 1.0])
if time_requires_grad == False:
self.evaluation_times.requires_grad = False
self.algebraic_from_differential = algebraic_from_differential
self.differential_from_algebraic = differential_from_algebraic
self.nesterov_factor = 3
self.activation_h = nn.Identity() if activation_h is None else activation_h
self.nhid = [nhid] if isinstance(nhid, int) else nhid
self.rnn = rnn
self.tol = tol
self.rnn_out = rnn_out
self.ic = ic
self.both = both
self.method = method
def forward(self, t, x, multiforecast=None, retain_grad=False):
"""
--
:param t: [time, batch]
:param x: [time, batch, ...]
:return: [time, batch, *nhid]
"""
n_t, n_b = t.shape
h_ode = [None] * (n_t + 1)
h_rnn = [None] * (n_t + 1)
h_ode[-1] = h_rnn[-1] = torch.zeros(n_b, *self.nhid, device=x.device)
if self.ic:
h_ode[0] = h_rnn[0] = self.ic(rearrange(x, 't b c -> b (t c)')).view((n_b, *self.nhid))
else:
h_ode[0] = h_rnn[0] = torch.zeros(n_b, *self.nhid, device=x.device)
if self.rnn_out:
for i in range(n_t):
self.ode.update(t[i])
if self.differential_from_algebraic:
h_rnn[i] = self.calc_differential_from_algebraic(h_rnn[i], t[i])
h_ode[i] = odeint(self.ode, h_rnn[i], self.evaluation_times, atol=self.tol, rtol=self.tol, method=self.method)[-1]
if self.algebraic_from_differential:
h_ode[i] = self.calc_algebraic_from_differential(h_ode[i])
h_rnn[i + 1] = self.rnn(h_ode[i], x[i])
out = (h_rnn,)
else:
for i in range(n_t):
self.ode.update(t[i])
h_rnn[i] = self.rnn(h_ode[i], x[i])
if self.differential_from_algebraic:
h_rnn[i] = self.calc_differential_from_algebraic(h_rnn[i], t[i])
h_ode[i + 1] = odeint(self.ode, h_rnn[i], self.evaluation_times, atol=self.tol, rtol=self.tol, method=self.method)[-1]
if self.algebraic_from_differential:
h_ode[i + 1] = self.calc_algebraic_from_differential(h_ode[i + 1])
out = (h_ode,)
if self.both:
out = (h_rnn, h_ode)
out = [torch.stack([k.to(x.device) for k in h], dim=0) for h in out]
if multiforecast is not None:
self.ode.update(torch.ones_like((t[0]), device=x.device))
forecast = odeint(self.ode, out[-1][-1], multiforecast * 1.0, atol=self.tol, rtol=self.tol, method=self.method)
if self.algebraic_from_differential:
forecast = self.calc_algebraic_from_differential(forecast)
out = (*out, forecast)
if retain_grad:
self.h_ode = h_ode
self.h_rnn = h_rnn
for i in range(n_t + 1):
if self.h_ode[i].requires_grad:
self.h_ode[i].retain_grad()
if self.h_rnn[i].requires_grad:
self.h_rnn[i].retain_grad()
return out
def calc_algebraic_from_differential(self, z):
# split the input into the starting time step and the other time steps
z_0 = z[:1]
z_T = z[1:]
# get the corresponding value of t for the other time steps
if len(self.evaluation_times.shape) == 2:
T = self.evaluation_times[:, 1:]
else:
T = self.evaluation_times[1:]
x, m = torch.split(z_T, 1, dim=-2)
# T^(-3/2) * e^(T/2)
k = torch.pow(T, -self.nesterov_factor/2) * torch.exp(T / 2)
if z.is_cuda:
k = k.to(z.get_device())
T = T.to(z.get_device())
# h(T) = [x(T) m(T)] * Transpose([T^(-3/2)*e^(T/2) I])
# h = x * k
# dh = k * (m - (3/2 * torch.pow(T, 1/2) * torch.exp(-T/2) - 1/2 * 1/k) * h)
k = self.activation_h(k)
h = bmul(k, x)
dh = bmul(k, m - bmul(self.nesterov_factor/2 * torch.pow(T, 1/2) * torch.exp(-T/2) - 1/2 * 1/k, h))
z_t = torch.cat((h, dh), dim=-2)
out = torch.cat((z_0, z_t), dim=0)
return out
def calc_differential_from_algebraic(self, z, t):
h, dh = torch.split(z, 1, dim=1)
k_reciprocal = 1 / (torch.pow(t, -self.nesterov_factor/2) * torch.exp(t/2))
if z.is_cuda:
k_reciprocal = k_reciprocal.to(z.get_device())
m = bmul((self.nesterov_factor/2 * (1/t) * k_reciprocal - 1/2 * k_reciprocal), h) \
+ bmul(k_reciprocal, dh)
x = bmul(h, k_reciprocal)
return torch.cat([x, m], dim=1)