8
8
from .distribution import draw_values
9
9
import numpy as np
10
10
from scipy .special import logit as nplogit
11
- from scipy .special import expit
12
11
13
12
14
13
__all__ = [
@@ -193,9 +192,6 @@ class Log(ElemwiseTransform):
193
192
def backward (self , x ):
194
193
return tt .exp (x )
195
194
196
- def backward_val (self , x ):
197
- return np .exp (x )
198
-
199
195
def forward (self , x ):
200
196
return tt .log (x )
201
197
@@ -215,9 +211,6 @@ class LogExpM1(ElemwiseTransform):
215
211
def backward (self , x ):
216
212
return tt .nnet .softplus (x )
217
213
218
- def backward_val (self , x ):
219
- return np .log (1 + np .exp (- np .abs (x ))) + np .max ([x , 0 ])
220
-
221
214
def forward (self , x ):
222
215
"""Inverse operation of softplus.
223
216
@@ -236,16 +229,12 @@ def jacobian_det(self, x):
236
229
log_exp_m1 = LogExpM1 ()
237
230
238
231
239
-
240
232
class LogOdds (ElemwiseTransform ):
241
233
name = "logodds"
242
234
243
235
def backward (self , x ):
244
236
return invlogit (x , 0.0 )
245
237
246
- def backward_val (self , x ):
247
- return invlogit (x , 0.0 )
248
-
249
238
def forward (self , x ):
250
239
return logit (x )
251
240
@@ -256,7 +245,6 @@ def forward_val(self, x, point=None):
256
245
logodds = LogOdds ()
257
246
258
247
259
-
260
248
class Interval (ElemwiseTransform ):
261
249
"""Transform from real line interval [a,b] to whole real line."""
262
250
@@ -265,19 +253,12 @@ class Interval(ElemwiseTransform):
265
253
def __init__ (self , a , b ):
266
254
self .a = tt .as_tensor_variable (a )
267
255
self .b = tt .as_tensor_variable (b )
268
- self .a_ = a
269
- self .b_ = b
270
256
271
257
def backward (self , x ):
272
258
a , b = self .a , self .b
273
259
r = (b - a ) * tt .nnet .sigmoid (x ) + a
274
260
return r
275
261
276
- def backward_val (self , x ):
277
- a , b = self .a_ , self .b_
278
- r = (b - a ) * 1 / (1 + np .exp (- x )) + a
279
- return r
280
-
281
262
def forward (self , x ):
282
263
a , b = self .a , self .b
283
264
return tt .log (x - a ) - tt .log (b - x )
@@ -304,16 +285,10 @@ class LowerBound(ElemwiseTransform):
304
285
305
286
def __init__ (self , a ):
306
287
self .a = tt .as_tensor_variable (a )
307
- self .a_ = a
308
288
309
289
def backward (self , x ):
310
- a = self .a_
311
- r = tt .exp (x ) + a
312
- return r
313
-
314
- def backward_val (self , x ):
315
290
a = self .a
316
- r = np .exp (x ) + a
291
+ r = tt .exp (x ) + a
317
292
return r
318
293
319
294
def forward (self , x ):
@@ -332,10 +307,10 @@ def jacobian_det(self, x):
332
307
333
308
334
309
lowerbound = LowerBound
335
- '''
310
+ """
336
311
Alias for ``LowerBound`` (:class: LowerBound) Transform (:class: Transform) class
337
312
for use in the ``transform`` argument of a random variable.
338
- '''
313
+ """
339
314
340
315
341
316
class UpperBound (ElemwiseTransform ):
@@ -345,18 +320,12 @@ class UpperBound(ElemwiseTransform):
345
320
346
321
def __init__ (self , b ):
347
322
self .b = tt .as_tensor_variable (b )
348
- self .b_ = b
349
323
350
324
def backward (self , x ):
351
325
b = self .b
352
326
r = b - tt .exp (x )
353
327
return r
354
328
355
- def backward_val (self , x ):
356
- b = self .b_
357
- r = b - np .exp (x )
358
- return r
359
-
360
329
def forward (self , x ):
361
330
b = self .b
362
331
return tt .log (b - x )
@@ -373,11 +342,10 @@ def jacobian_det(self, x):
373
342
374
343
375
344
upperbound = UpperBound
376
- '''
345
+ """
377
346
Alias for ``UpperBound`` (:class: UpperBound) Transform (:class: Transform) class
378
347
for use in the ``transform`` argument of a random variable.
379
- '''
380
-
348
+ """
381
349
382
350
383
351
class Ordered (Transform ):
@@ -389,12 +357,6 @@ def backward(self, y):
389
357
x = tt .inc_subtensor (x [..., 1 :], tt .exp (y [..., 1 :]))
390
358
return tt .cumsum (x , axis = - 1 )
391
359
392
- def backward_val (self , y ):
393
- x = np .zeros (y .shape )
394
- x [..., 0 ] += y [..., 0 ]
395
- x [..., 1 :] += np .exp (y [..., 1 :])
396
- return np .cumsum (x , axis = - 1 )
397
-
398
360
def forward (self , x ):
399
361
y = tt .zeros (x .shape )
400
362
y = tt .inc_subtensor (y [..., 0 ], x [..., 0 ])
@@ -412,10 +374,10 @@ def jacobian_det(self, y):
412
374
413
375
414
376
ordered = Ordered ()
415
- '''
377
+ """
416
378
Instantiation of ``Ordered`` (:class: Ordered) Transform (:class: Transform) class
417
379
for use in the ``transform`` argument of a random variable.
418
- '''
380
+ """
419
381
420
382
421
383
class SumTo1 (Transform ):
@@ -430,10 +392,6 @@ def backward(self, y):
430
392
remaining = 1 - tt .sum (y [..., :], axis = - 1 , keepdims = True )
431
393
return tt .concatenate ([y [..., :], remaining ], axis = - 1 )
432
394
433
- def backward_val (self , y ):
434
- remaining = 1 - np .sum (y [..., :], axis = - 1 , keepdims = True )
435
- return np .concatenate ([y [..., :], remaining ], axis = - 1 )
436
-
437
395
def forward (self , x ):
438
396
return x [..., :- 1 ]
439
397
@@ -500,18 +458,6 @@ def backward(self, y_):
500
458
x = S * yl
501
459
return floatX (x .T )
502
460
503
- def backward_val (self , y_ ):
504
- y = y_ .T
505
- Km1 = y .shape [0 ]
506
- k = np .arange (Km1 )[(slice (None ),) + (None ,) * (y .ndim - 1 )]
507
- eq_share = nplogit (1.0 / (Km1 + 1 - k ).astype (str (y_ .dtype )))
508
- z = expit (y + eq_share )
509
- yl = np .concatenate ([z , np .ones (y [:1 ].shape )])
510
- yu = np .concatenate ([np .ones (y [:1 ].shape ), 1 - z ])
511
- S = np .cumprod (yu , 0 )
512
- x = S * yl
513
- return floatX (x .T )
514
-
515
461
def jacobian_det (self , y_ ):
516
462
y = y_ .T
517
463
Km1 = y .shape [0 ]
@@ -526,10 +472,9 @@ def jacobian_det(self, y_):
526
472
stick_breaking = StickBreaking ()
527
473
528
474
529
-
530
475
def t_stick_breaking (eps : float ) -> StickBreaking :
531
- ''' Return a new :class:`StickBreaking` transform with specified eps(ilon),
532
- instead of the default.'''
476
+ """ Return a new :class:`StickBreaking` transform with specified eps(ilon),
477
+ instead of the default."""
533
478
return StickBreaking (eps )
534
479
535
480
@@ -542,9 +487,6 @@ class Circular(ElemwiseTransform):
542
487
def backward (self , y ):
543
488
return tt .arctan2 (tt .sin (y ), tt .cos (y ))
544
489
545
- def backward_val (self , y ):
546
- return y
547
-
548
490
def forward (self , x ):
549
491
return tt .as_tensor_variable (x )
550
492
@@ -557,6 +499,7 @@ def jacobian_det(self, x):
557
499
558
500
circular = Circular ()
559
501
502
+
560
503
class CholeskyCovPacked (Transform ):
561
504
name = "cholesky-cov-packed"
562
505
@@ -566,10 +509,6 @@ def __init__(self, n):
566
509
def backward (self , x ):
567
510
return tt .advanced_set_subtensor1 (x , tt .exp (x [self .diag_idxs ]), self .diag_idxs )
568
511
569
- def backward_val (self , x ):
570
- x [..., self .diag_idxs ] = np .exp (x [..., self .diag_idxs ])
571
- return x
572
-
573
512
def forward (self , y ):
574
513
return tt .advanced_set_subtensor1 (y , tt .log (y [self .diag_idxs ]), self .diag_idxs )
575
514
@@ -580,6 +519,7 @@ def forward_val(self, y, point=None):
580
519
def jacobian_det (self , y ):
581
520
return tt .sum (y [self .diag_idxs ])
582
521
522
+
583
523
class Chain (Transform ):
584
524
def __init__ (self , transform_list ):
585
525
self .transform_list = transform_list
@@ -603,12 +543,6 @@ def backward(self, y):
603
543
x = transf .backward (x )
604
544
return x
605
545
606
- def backward_val (self , y ):
607
- x = y
608
- for transf in reversed (self .transform_list ):
609
- x = transf .backward_val (x )
610
- return x
611
-
612
546
def jacobian_det (self , y ):
613
547
y = tt .as_tensor_variable (y )
614
548
det_list = []
0 commit comments