Commit b54bb7b
Replace deprecation of
This also removes a spurious deprecation warning that appears when `JointDistribution`s are used in public colabs. (JDs have a multi-level hierarchy in which no class defines its own _parameter_properties, but since they all inherit the base class def that raises `NotImplementedError`, there's no inheritance problem).
Justification for this change: there are legitimate 'quick-and-dirty' uses of parameter_properties inheritance, even if we wouldn't do it in TFP.
For example, somewhere in the DeepMind silo is a Normal distribution that takes a log_scale rather than a scale:
class NormalWithLogScale(tfd.Normal):
def __init__(self, loc, log_scale):
super().__init__(loc=loc, scale=tf.exp(log_scale))
Ignoring whether this is the best way to accomplish any particular goal, from a general Pythonic standpoint one might expect that this class would at least be basically functional. It may not support batch slicing or AutoCompositeTensor, but one should at least be able to call sample and log_prob, which means it should at least define properties like batch_shape. But now that batch shape depends on parameter_properties (as of cl/373590501), breaking parameter_properties inheritance means breaking batch_shape inheritance.
To allow quick subclasses like this, I propose we simply warn when an inherited `parameter_properties` is called. In this example, the batch shape would be computed (correctly) using the base Normal parameters, just as if an explicit batch_shape method had been inherited.
For full functionality including batch slicing, CompositeTensor, etc., a subclass would need to both (a) set self.parameters = dict(locals()) in its own constructor, and (b) define its own _parameter_properties. I've tried to articulate these requirements in the warning message.
PiperOrigin-RevId: 374554087parameter_properties inheritance with a warning.1 parent c0e7515 commit b54bb7b
2 files changed
Lines changed: 61 additions & 39 deletions
Lines changed: 45 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
276 | 277 | | |
277 | 278 | | |
278 | 279 | | |
279 | | - | |
280 | | - | |
281 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
282 | 283 | | |
283 | 284 | | |
284 | 285 | | |
285 | | - | |
286 | | - | |
| 286 | + | |
287 | 287 | | |
288 | 288 | | |
289 | | - | |
290 | 289 | | |
291 | 290 | | |
292 | 291 | | |
293 | 292 | | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | 293 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
| 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 | + | |
308 | 332 | | |
309 | | - | |
310 | | - | |
| 333 | + | |
311 | 334 | | |
312 | | - | |
313 | 335 | | |
314 | 336 | | |
315 | 337 | | |
| |||
657 | 679 | | |
658 | 680 | | |
659 | 681 | | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
| 682 | + | |
664 | 683 | | |
665 | 684 | | |
666 | 685 | | |
| |||
Lines changed: 16 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
33 | | - | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
617 | 616 | | |
618 | 617 | | |
619 | 618 | | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
| 619 | + | |
624 | 620 | | |
625 | 621 | | |
626 | 622 | | |
| |||
640 | 636 | | |
641 | 637 | | |
642 | 638 | | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
647 | | - | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
648 | 650 | | |
649 | 651 | | |
650 | 652 | | |
651 | 653 | | |
652 | 654 | | |
653 | 655 | | |
654 | 656 | | |
655 | | - | |
656 | | - | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
657 | 660 | | |
658 | 661 | | |
659 | 662 | | |
| |||
0 commit comments