API
Import modules and set version.
bijectors
Define the bijectors used in the normalizing flows.
Bijector
Wrapper class for bijector functions
Source code in pzflow/bijectors.py
110 111 112 113 114 115 116 117 118 |
|
ForwardFunction
Return the output and log_det of the forward bijection on the inputs.
ForwardFunction of a Bijector, originally returned by the InitFunction of the Bijector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
A pytree of bijector parameters. This usually looks like a nested tuple or list of parameters. |
required |
inputs |
jnp.ndarray
|
The data to be transformed by the bijection. |
required |
Returns:
Name | Type | Description |
---|---|---|
outputs |
jnp.ndarray
|
Result of the forward bijection applied to the inputs. |
log_det |
jnp.ndarray
|
The log determinant of the Jacobian evaluated at the inputs. |
Source code in pzflow/bijectors.py
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 |
|
InitFunction
Initialize the corresponding Bijector.
InitFunction returned by the initialization of a Bijector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rng |
jnp.ndarray
|
A Random Number Key from jax.random.PRNGKey. |
required |
input_dim |
int
|
The input dimension of the bijection. |
required |
Returns:
Name | Type | Description |
---|---|---|
params |
a Jax pytree
|
A pytree of bijector parameters. This usually looks like a nested tuple or list of parameters. |
forward_fun |
ForwardFunction
|
The forward function of the Bijector. |
inverse_fun |
InverseFunction
|
The inverse function of the Bijector. |
Source code in pzflow/bijectors.py
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 |
|
InverseFunction
Return the output and log_det of the inverse bijection on the inputs.
InverseFunction of a Bijector, originally returned by the InitFunction of the Bijector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
A pytree of bijector parameters. This usually looks like a nested tuple or list of parameters. |
required |
inputs |
jnp.ndarray
|
The data to be transformed by the bijection. |
required |
Returns:
Name | Type | Description |
---|---|---|
outputs |
jnp.ndarray
|
Result of the inverse bijection applied to the inputs. |
log_det |
jnp.ndarray
|
The log determinant of the Jacobian evaluated at the inputs. |
Source code in pzflow/bijectors.py
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 |
|
Chain(inputs)
Bijector that chains multiple InitFunctions into a single InitFunction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Bijector1(), Bijector2(), ...
|
A container of Bijector calls to be chained together. |
()
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the total chained Bijector. |
Bijector_Info
|
Tuple('Chain', Tuple(Bijector_Info for each bijection in the chain)) This allows the chain to be recreated later. |
Source code in pzflow/bijectors.py
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 |
|
ColorTransform(ref_idx, mag_idx)
Bijector that calculates photometric colors from magnitudes.
Using ColorTransform restricts and impacts the order of columns in the corresponding normalizing flow. See the notes below for an example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref_idx |
int
|
The index corresponding to the column of the reference band, which serves as a proxy for overall luminosity. |
required |
mag_idx |
arraylike of int
|
The indices of the magnitude columns from which colors will be calculated. |
required |
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the ColorTransform Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Notes
ColorTransform requires careful management of column order in the bijector. This is best explained with an example:
Assume we have data [redshift, u, g, ellipticity, r, i, z, y, mass] Then ColorTransform(ref_idx=4, mag_idx=[1, 2, 4, 5, 6, 7]) will output [redshift, ellipticity, mass, r, u-g, g-r, r-i, i-z, z-y]
Notice how the non-magnitude columns are aggregated at the front of the array, maintaining their relative order from the original array. These values are then followed by the reference magnitude, and the new colors.
Also notice that the magnitudes indices in mag_idx are assumed to be adjacent colors. E.g. mag_idx=[1, 2, 5, 4, 6, 7] would have produced the colors [u-g, g-i, i-r, r-z, z-y]. You can chain multiple ColorTransforms back-to-back to create colors in a non-adjacent manner.
Source code in pzflow/bijectors.py
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 |
|
InvSoftplus(column_idx, sharpness=1)
Bijector that applies inverse softplus to the specified column(s).
Applying the inverse softplus ensures that samples from that column will always be non-negative. This is because samples are the output of the inverse bijection -- so samples will have a softplus applied to them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_idx |
int
|
An index or iterable of indices corresponding to the column(s) you wish to be transformed. |
required |
sharpness |
float
|
The sharpness(es) of the softplus transformation. If more than one is provided, the list of sharpnesses must be of the same length as column_idx. |
1
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the Softplus Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
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 |
|
NeuralSplineCoupling(K=16, B=5, hidden_layers=2, hidden_dim=128, transformed_dim=None, n_conditions=0, periodic=False)
A coupling layer bijection with rational quadratic splines.
This Bijector is a Coupling Layer [1,2], and as such only transforms the second half of input dimensions (or the last N dimensions, where N = transformed_dim). In order to transform all of the dimensions, you need multiple Couplings interspersed with Bijectors that change the order of inputs dimensions, e.g., Reverse, Shuffle, Roll, etc.
NeuralSplineCoupling uses piecewise rational quadratic splines, as developed in [3].
If periodic=True, then this is a Circular Spline as described in [4].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
K |
int
|
Number of bins in the spline (the number of knots is K+1). |
16
|
B |
float
|
Range of the splines. If periodic=False, outside of (-B,B), the transformation is just the identity. If periodic=True, the input is mapped into the appropriate location in the range (-B,B). |
5
|
hidden_layers |
int
|
The number of hidden layers in the neural network used to calculate the positions and derivatives of the spline knots. |
2
|
hidden_dim |
int
|
The width of the hidden layers in the neural network used to calculate the positions and derivatives of the spline knots. |
128
|
transformed_dim |
int
|
The number of dimensions transformed by the splines. Default is ceiling(input_dim /2). |
None
|
n_conditions |
int
|
The number of variables to condition the bijection on. |
0
|
periodic |
bool
|
Whether to make this a periodic, Circular Spline [4]. |
False
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the NeuralSplineCoupling Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
References
[1] Laurent Dinh, David Krueger, Yoshua Bengio. NICE: Non-linear Independent Components Estimation. arXiv: 1605.08803, 2015. http://arxiv.org/abs/1605.08803 [2] Laurent Dinh, Jascha Sohl-Dickstein, Samy Bengio. Density Estimation Using Real NVP. arXiv: 1605.08803, 2017. http://arxiv.org/abs/1605.08803 [3] Conor Durkan, Artur Bekasov, Iain Murray, George Papamakarios. Neural Spline Flows. arXiv:1906.04032, 2019. https://arxiv.org/abs/1906.04032 [4] Rezende, Danilo Jimenez et al. Normalizing Flows on Tori and Spheres. arxiv:2002.02428, 2020 http://arxiv.org/abs/2002.02428
Source code in pzflow/bijectors.py
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 |
|
Reverse()
Bijector that reverses the order of inputs.
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the the Reverse Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
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 |
|
Roll(shift=1)
Bijector that rolls inputs along their last column using jnp.roll.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shift |
int
|
The number of places to roll. |
1
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the the Roll Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
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 |
|
RollingSplineCoupling(nlayers, shift=1, K=16, B=5, hidden_layers=2, hidden_dim=128, transformed_dim=None, n_conditions=0, periodic=False)
Bijector that alternates NeuralSplineCouplings and Roll bijections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nlayers |
int
|
The number of (NeuralSplineCoupling(), Roll()) couplets in the chain. |
required |
shift |
int
|
How far the inputs are shifted on each Roll(). |
1
|
K |
int
|
Number of bins in the RollingSplineCoupling. |
16
|
B |
float
|
Range of the splines in the RollingSplineCoupling. If periodic=False, outside of (-B,B), the transformation is just the identity. If periodic=True, the input is mapped into the appropriate location in the range (-B,B). |
5
|
hidden_layers |
int
|
The number of hidden layers in the neural network used to calculate the bins and derivatives in the RollingSplineCoupling. |
2
|
hidden_dim |
int
|
The width of the hidden layers in the neural network used to calculate the bins and derivatives in the RollingSplineCoupling. |
128
|
transformed_dim |
int
|
The number of dimensions transformed by the splines. Default is ceiling(input_dim /2). |
None
|
n_conditions |
int
|
The number of variables to condition the bijection on. |
0
|
periodic |
bool
|
Whether to make this a periodic, Circular Spline |
False
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the RollingSplineCoupling Bijector. |
Bijector_Info
|
Nested tuple of the Bijector name and input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
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 |
|
Scale(scale)
Bijector that multiplies inputs by a scalar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale |
float
|
Factor by which to scale inputs. |
required |
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the the Scale Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
|
ShiftBounds(min, max, B=5)
Bijector shifts the bounds of inputs so the lie in the range (-B, B).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min |
float
|
The minimum of the input range. |
required |
min |
float
|
The maximum of the input range. |
required |
B |
float
|
The extent of the output bounds, which will be (-B, B). |
5
|
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the ShiftBounds Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
Shuffle()
Bijector that randomly permutes inputs.
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the Shuffle Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 |
|
StandardScaler(means, stds)
Bijector that applies standard scaling to each input.
Each input dimension i has an associated mean u_i and standard dev s_i. Each input is rescaled as (input[i] - u_i)/s_i, so that each input dimension has mean zero and unit variance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
means |
jnp.ndarray
|
The mean of each column. |
required |
stds |
jnp.ndarray
|
The standard deviation of each column. |
required |
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the StandardScaler Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 |
|
UniformDequantizer(column_idx)
Bijector that dequantizes discrete variables with uniform noise.
Dequantizers are necessary for modeling discrete values with a flow. Note that this isn't technically a bijector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_idx |
int
|
An index or iterable of indices corresponding to the column(s) with discrete values. |
required |
Returns:
Type | Description |
---|---|
InitFunction
|
The InitFunction of the UniformDequantizer Bijector. |
Bijector_Info
|
Tuple of the Bijector name and the input parameters. This allows it to be recreated later. |
Source code in pzflow/bijectors.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
distributions
Define the latent distributions used in the normalizing flows.
CentBeta
Bases: LatentDist
A centered Beta distribution.
This distribution is just a regular Beta distribution, scaled and shifted to have support on the domain [-B, B] in each dimension.
Alpha and beta parameters for each dimension are learned during training.
Source code in pzflow/distributions.py
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 |
|
__init__(input_dim, B=5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The dimension of the distribution. |
required |
B |
float
|
The distribution has support (-B, B) along each dimension. |
5
|
Source code in pzflow/distributions.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Tuple of ((a1, b1), (a2, b2), ...) where aN,bN are log(alpha),log(beta) for the Nth dimension. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
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 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Tuple of ((a1, b1), (a2, b2), ...) where aN,bN are log(alpha),log(beta) for the Nth dimension. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
CentBeta13
Bases: LatentDist
A centered Beta distribution with alpha, beta = 13.
This distribution is just a regular Beta distribution, scaled and shifted to have support on the domain [-B, B] in each dimension.
Alpha, beta = 13 means that the distribution looks like a Gaussian distribution, but with hard cutoffs at +/- B.
Source code in pzflow/distributions.py
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 |
|
__init__(input_dim, B=5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The dimension of the distribution. |
required |
B |
float
|
The distribution has support (-B, B) along each dimension. |
5
|
Source code in pzflow/distributions.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
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 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
Joint
Bases: LatentDist
A joint distribution built from other distributions.
Note that each of the other distributions already have support for multiple dimensions. This is only useful if you want to combine different distributions for different dimensions, e.g. if your first dimension has a Uniform latent space and the second dimension has a CentBeta latent space.
Source code in pzflow/distributions.py
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 |
|
__init__(inputs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Union[LatentDist, tuple]
|
The latent distributions to join together. |
()
|
Source code in pzflow/distributions.py
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 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Jax Pytree
|
Parameters for the distributions. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
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 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Parameters for the distributions. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
LatentDist
Bases: ABC
Base class for latent distributions.
Source code in pzflow/distributions.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
log_prob(params, inputs)
abstractmethod
Calculate log-probability of the inputs.
Source code in pzflow/distributions.py
22 23 24 |
|
sample(params, nsamples, seed=None)
abstractmethod
Sample from the distribution.
Source code in pzflow/distributions.py
26 27 28 29 30 |
|
Normal
Bases: LatentDist
A multivariate Gaussian distribution with mean zero and unit variance.
Note this distribution has infinite support, so it is not recommended that you use it with the spline coupling layers, which have compact support. If you do use the two together, you should set the support of the spline layers (using the spline parameter B) to be large enough that you rarely draw Gaussian samples outside the support of the splines.
Source code in pzflow/distributions.py
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 |
|
__init__(input_dim)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The dimension of the distribution. |
required |
Source code in pzflow/distributions.py
242 243 244 245 246 247 248 249 250 251 252 253 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
Tdist
Bases: LatentDist
A multivariate T distribution with mean zero and unit scale matrix.
The number of degrees of freedom (i.e. the weight of the tails) is learned during training.
Note this distribution has infinite support and potentially large tails, so it is not recommended to use this distribution with the spline coupling layers, which have compact support.
Source code in pzflow/distributions.py
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 |
|
__init__(input_dim)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The dimension of the distribution. |
required |
Source code in pzflow/distributions.py
317 318 319 320 321 322 323 324 325 326 327 328 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Uses method explained here: http://gregorygundersen.com/blog/2020/01/20/multivariate-t/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
float
|
The degrees of freedom (nu) of the t-distribution. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
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 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
float
|
The degrees of freedom (nu) of the t-distribution. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
Uniform
Bases: LatentDist
A multivariate uniform distribution with support [-B, B].
Source code in pzflow/distributions.py
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 |
|
__init__(input_dim, B=5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The dimension of the distribution. |
required |
B |
float
|
The distribution has support (-B, B) along each dimension. |
5
|
Source code in pzflow/distributions.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
log_prob(params, inputs)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Jax Pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
inputs |
jnp.ndarray
|
Input data for which log probability density is calculated. |
required |
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/distributions.py
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 |
|
sample(params, nsamples, seed=None)
Returns samples from the distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
a Jax pytree
|
Empty pytree -- this distribution doesn't have learnable parameters. This parameter is present to ensure a consistent interface. |
required |
nsamples |
int
|
The number of samples to be returned. |
required |
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (nsamples, self.input_dim). |
Source code in pzflow/distributions.py
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 |
|
examples
Functions that return example data and a example flow trained on galaxy data. To see these examples in action, see the tutorial notebooks.
get_checkerboard_data()
Return DataFrame with discrete checkerboard data.
Source code in pzflow/examples.py
45 46 47 |
|
get_city_data()
Return DataFrame with example city data.
The countries, names, population, and coordinates of 47,966 cities.
Subset of the Kaggle world cities database. https://www.kaggle.com/max-mind/world-cities-database This database was downloaded from MaxMind. The license follows:
OPEN DATA LICENSE for MaxMind WorldCities and Postal Code Databases
Copyright (c) 2008 MaxMind Inc. All Rights Reserved.
The database uses toponymic information, based on the Geographic Names
Data Base, containing official standard names approved by the United States
Board on Geographic Names and maintained by the National
Geospatial-Intelligence Agency. More information is available at the Maps
and Geodata link at www.nga.mil. The National Geospatial-Intelligence Agency
name, initials, and seal are protected by 10 United States Code Section 445.
It also uses free population data from Stefan Helders www.world-gazetteer.com.
Visit his website to download the free population data. Our database
combines Stefan's population data with the list of all cities in the world.
All advertising materials and documentation mentioning features or use of
this database must display the following acknowledgment:
"This product includes data created by MaxMind, available from
http://www.maxmind.com/"
Redistribution and use with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions must retain the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
2. All advertising materials and documentation mentioning features or use of
this database must display the following acknowledgement:
"This product includes data created by MaxMind, available from
http://www.maxmind.com/"
3. "MaxMind" may not be used to endorse or promote products derived from this
database without specific prior written permission.
THIS DATABASE IS PROVIDED BY MAXMIND.COM ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MAXMIND.COM BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
DATABASE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Source code in pzflow/examples.py
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 |
|
get_example_flow()
Return a normalizing flow that was trained on galaxy data.
This flow was trained in the redshift_example.ipynb
Jupyter notebook,
on the example data available in pzflow.examples.galaxy_data
.
For more info: print(example_flow().info)
.
Source code in pzflow/examples.py
105 106 107 108 109 110 111 112 113 114 115 |
|
get_galaxy_data()
Return DataFrame with example galaxy data.
100,000 galaxies from the Buzzard simulation [1], with redshifts in the range (0,2.3) and photometry in the LSST ugrizy bands.
References
[1] Joseph DeRose et al. The Buzzard Flock: Dark Energy Survey Synthetic Sky Catalogs. arXiv:1901.02401, 2019. https://arxiv.org/abs/1901.02401
Source code in pzflow/examples.py
30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
get_twomoons_data()
Return DataFrame with two moons example data.
Two moons data originally from scikit-learn,
i.e., sklearn.datasets.make_moons
.
Source code in pzflow/examples.py
21 22 23 24 25 26 27 |
|
flow
Define the Flow object that defines the normalizing flow.
Flow
A normalizing flow that models tabular data.
Attributes:
Name | Type | Description |
---|---|---|
data_columns |
tuple
|
List of DataFrame columns that the flow expects/produces. |
conditional_columns |
tuple
|
List of DataFrame columns on which the flow is conditioned. |
latent |
distributions.LatentDist
|
The latent distribution of the normalizing flow. Has it's own sample and log_prob methods. |
data_error_model |
Callable
|
The error model for the data variables. See the docstring of init for more details. |
condition_error_model |
Callable
|
The error model for the conditional variables. See the docstring of init for more details. |
info |
Any
|
Object containing any kind of info included with the flow. Often describes the data the flow is trained on. |
Source code in pzflow/flow.py
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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
|
__init__(data_columns=None, bijector=None, latent=None, conditional_columns=None, data_error_model=None, condition_error_model=None, autoscale_conditions=True, seed=0, info=None, file=None, _dictionary=None)
Instantiate a normalizing flow.
Note that while all of the init parameters are technically optional, you must provide either data_columns OR file. In addition, if a file is provided, all other parameters must be None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_columns |
Sequence[str]
|
Tuple, list, or other container of column names. These are the columns the flow expects/produces in DataFrames. |
None
|
bijector |
Bijector Call
|
A Bijector call that consists of the bijector InitFunction that initializes the bijector and the tuple of Bijector Info. Can be the output of any Bijector, e.g. Reverse(), Chain(...), etc. If not provided, the bijector can be set later using flow.set_bijector, or by calling flow.train, in which case the default bijector will be used. The default bijector is ShiftBounds -> RollingSplineCoupling, where the range of shift bounds is learned from the training data, and the dimensions of RollingSplineCoupling is inferred. The default bijector assumes that the latent has support [-5, 5] for every dimension. |
None
|
latent |
distributions.LatentDist
|
The latent distribution for the normalizing flow. Can be any of the distributions from pzflow.distributions. If not provided, a uniform distribution is used with input_dim = len(data_columns), and B=5. |
None
|
conditional_columns |
Sequence[str]
|
Names of columns on which to condition the normalizing flow. |
None
|
data_error_model |
Callable
|
A callable that defines the error model for data variables. data_error_model must take key, X, Xerr, nsamples as arguments: - key is a jax rng key, e.g. jax.random.PRNGKey(0) - X is 2D array of data variables, where the order of variables matches the order of the columns in data_columns - Xerr is the corresponding 2D array of errors - nsamples is number of samples to draw from error distribution data_error_model must return an array of samples with the shape (X.shape[0], nsamples, X.shape[1]). If data_error_model is not provided, Gaussian error model assumed. |
None
|
condition_error_model |
Callable
|
A callable that defines the error model for conditional variables. condition_error_model must take key, X, Xerr, nsamples, where: - key is a jax rng key, e.g. jax.random.PRNGKey(0) - X is 2D array of conditional variables, where the order of variables matches order of columns in conditional_columns - Xerr is the corresponding 2D array of errors - nsamples is number of samples to draw from error distribution condition_error_model must return array of samples with shape (X.shape[0], nsamples, X.shape[1]). If condition_error_model is not provided, Gaussian error model assumed. |
None
|
autoscale_conditions |
bool
|
Sets whether or not conditions are automatically standard scaled when passed to a conditional flow. I recommend you leave as True. |
True
|
seed |
int
|
The random seed for initial parameters |
0
|
info |
Any
|
An object to attach to the info attribute. |
None
|
file |
str
|
Path to file from which to load a pretrained flow. If a file is provided, all other parameters must be None. |
None
|
Source code in pzflow/flow.py
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 |
|
log_prob(inputs, err_samples=None, seed=None)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Input data for which log probability density is calculated. Every column in self.data_columns must be present. If self.conditional_columns is not None, those must be present as well. If other columns are present, they are ignored. |
required |
err_samples |
int
|
Number of samples from the error distribution to average over for
the log_prob calculation. If provided, Gaussian errors are assumed,
and method will look for error columns in |
None
|
seed |
int
|
Random seed for drawing the samples with Gaussian errors. |
None
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0],). |
Source code in pzflow/flow.py
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 |
|
posterior(inputs, column, grid, marg_rules=None, normalize=True, err_samples=None, seed=None, batch_size=None, nan_to_zero=True)
Calculates posterior distributions for the provided column.
Calculates the conditional posterior distribution, assuming the data values in the other columns of the DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Data on which the posterior distributions are conditioned. Must have columns matching self.data_columns, except for the column specified for the posterior (see below). |
required |
column |
str
|
Name of the column for which the posterior distribution
is calculated. Must be one of the columns in self.data_columns.
However, whether or not this column is one of the columns in
|
required |
grid |
jnp.ndarray
|
Grid on which to calculate the posterior. |
required |
marg_rules |
dict
|
Dictionary with rules for marginalizing over missing variables. The dictionary must contain the key "flag", which gives the flag that indicates a missing value. E.g. if missing values are given the value 99, the dictionary should contain {"flag": 99}. The dictionary must also contain {"name": callable} for any variables that will need to be marginalized over, where name is the name of the variable, and callable is a callable that takes the row of variables nad returns a grid over which to marginalize the variable. E.g. {"y": lambda row: jnp.linspace(0, row["x"], 10)}. Note: the callable for a given name must always return an array of the same length, regardless of the input row. |
None
|
err_samples |
int
|
Number of samples from the error distribution to average over for
the posterior calculation. If provided, Gaussian errors are assumed,
and method will look for error columns in |
None
|
seed |
int
|
Random seed for drawing the samples with Gaussian errors. |
None
|
batch_size |
int
|
Size of batches in which to calculate posteriors. If None, all posteriors are calculated simultaneously. Simultaneous calculation is faster, but memory intensive for large data sets. |
None
|
normalize |
boolean
|
Whether to normalize the posterior so that it integrates to 1. |
True
|
nan_to_zero |
bool
|
Whether to convert NaN's to zero probability in the final pdfs. |
True
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
Device array of shape (inputs.shape[0], grid.size). |
Source code in pzflow/flow.py
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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
|
sample(nsamples=1, conditions=None, save_conditions=True, seed=None)
Returns samples from the normalizing flow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nsamples |
int
|
The number of samples to be returned. |
1
|
conditions |
pd.DataFrame
|
If this is a conditional flow, you must pass conditions for each sample. nsamples will be drawn for each row in conditions. |
None
|
save_conditions |
bool
|
If true, conditions will be saved in the DataFrame of samples that is returned. |
True
|
seed |
int
|
Sets the random seed for the samples. |
None
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
Pandas DataFrame of samples. |
Source code in pzflow/flow.py
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
|
save(file)
Saves the flow to a file.
Pickles the flow and saves it to a file that can be passed as
the file
argument during flow instantiation.
WARNING: Currently, this method only works for bijectors that are
implemented in the bijectors
module. If you want to save a flow
with a custom bijector, you either need to add the bijector to that
module, or handle the saving and loading on your end.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
Path to where the flow will be saved.
Extension |
required |
Source code in pzflow/flow.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 |
|
set_bijector(bijector, params=None, seed=0)
Set the bijector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bijector |
Bijector Call
|
A Bijector call that consists of the bijector InitFunction that initializes the bijector and the tuple of Bijector Info. Can be the output of any Bijector, e.g. Reverse(), Chain(...), etc. |
required |
params |
Pytree
|
A Pytree of bijector parameters. If not provided, the bijector will be initialized with random parameters. |
None
|
seed |
int
|
A random seed for initializing the bijector with random parameters. |
0
|
Source code in pzflow/flow.py
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 |
|
train(inputs, val_set=None, epochs=100, batch_size=1024, optimizer=None, loss_fn=None, convolve_errs=False, patience=None, best_params=True, seed=0, verbose=False, progress_bar=False)
Trains the normalizing flow on the provided inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Data on which to train the normalizing flow.
Must have columns matching |
required |
val_set |
pd.DataFrame
|
Validation set, of same format as inputs. If provided, validation loss will be calculated at the end of each epoch. |
None
|
epochs |
int
|
Number of epochs to train. |
100
|
batch_size |
int
|
Batch size for training. |
1024
|
optimizer |
optax optimizer
|
An optimizer from Optax. default = optax.adam(learning_rate=1e-3) see https://optax.readthedocs.io/en/latest/index.html for more. |
None
|
loss_fn |
Callable
|
A function to calculate the loss: |
None
|
convolve_errs |
bool
|
Whether to draw new data from the error distributions during
each epoch of training. Method will look for error columns in
|
False
|
patience |
int
|
Factor that controls early stopping. Training will stop if the loss doesn't decrease for this number of epochs. Note if a validation set is provided, the validation loss is used. |
None
|
best_params |
bool
|
Whether to use the params from the epoch with the lowest loss. Note if a validation set is provided, the epoch with the lowest validation loss is chosen. If False, the params from the final epoch are saved. |
True
|
seed |
int
|
A random seed to control the batching and the (optional) error sampling and creating the default bijector (the latter only happens if you didn't set up the bijector during Flow instantiation). |
0
|
verbose |
bool
|
If true, print the training loss every 5% of epochs. |
False
|
progress_bar |
bool
|
If true, display a tqdm progress bar during training. |
False
|
Returns:
Type | Description |
---|---|
list
|
List of training losses from every epoch. If no val_set provided, these are just training losses. If val_set is provided, then the first element is the list of training losses, while the second is the list of validation losses. |
Source code in pzflow/flow.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
|
flowEnsemble
Define FlowEnsemble object that holds an ensemble of normalizing flows.
FlowEnsemble
An ensemble of normalizing flows.
Attributes:
Name | Type | Description |
---|---|---|
data_columns |
tuple
|
List of DataFrame columns that the flows expect/produce. |
conditional_columns |
tuple
|
List of DataFrame columns on which the flows are conditioned. |
latent |
distributions.LatentDist
|
The latent distribution of the normalizing flows. Has it's own sample and log_prob methods. |
data_error_model |
Callable
|
The error model for the data variables. See the docstring of init for more details. |
condition_error_model |
Callable
|
The error model for the conditional variables. See the docstring of init for more details. |
info |
Any
|
Object containing any kind of info included with the ensemble. Often Reverse the data the flows are trained on. |
Source code in pzflow/flowEnsemble.py
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 |
|
__init__(data_columns=None, bijector=None, latent=None, conditional_columns=None, data_error_model=None, condition_error_model=None, autoscale_conditions=True, N=1, info=None, file=None)
Instantiate an ensemble of normalizing flows.
Note that while all of the init parameters are technically optional, you must provide either data_columns and bijector OR file. In addition, if a file is provided, all other parameters must be None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_columns |
Sequence[str]
|
Tuple, list, or other container of column names. These are the columns the flows expect/produce in DataFrames. |
None
|
bijector |
Bijector Call
|
A Bijector call that consists of the bijector InitFunction that initializes the bijector and the tuple of Bijector Info. Can be the output of any Bijector, e.g. Reverse(), Chain(...), etc. If not provided, the bijector can be set later using flow.set_bijector, or by calling flow.train, in which case the default bijector will be used. The default bijector is ShiftBounds -> RollingSplineCoupling, where the range of shift bounds is learned from the training data, and the dimensions of RollingSplineCoupling is inferred. The default bijector assumes that the latent has support [-5, 5] for every dimension. |
None
|
latent |
distributions.LatentDist
|
The latent distribution for the normalizing flow. Can be any of the distributions from pzflow.distributions. If not provided, a uniform distribution is used with input_dim = len(data_columns), and B=5. |
None
|
conditional_columns |
Sequence[str]
|
Names of columns on which to condition the normalizing flows. |
None
|
data_error_model |
Callable
|
A callable that defines the error model for data variables. data_error_model must take key, X, Xerr, nsamples as arguments: - key is a jax rng key, e.g. jax.random.PRNGKey(0) - X is 2D array of data variables, where the order of variables matches the order of the columns in data_columns - Xerr is the corresponding 2D array of errors - nsamples is number of samples to draw from error distribution data_error_model must return an array of samples with the shape (X.shape[0], nsamples, X.shape[1]). If data_error_model is not provided, Gaussian error model assumed. |
None
|
condition_error_model |
Callable
|
A callable that defines the error model for conditional variables. condition_error_model must take key, X, Xerr, nsamples, where: - key is a jax rng key, e.g. jax.random.PRNGKey(0) - X is 2D array of conditional variables, where the order of variables matches order of columns in conditional_columns - Xerr is the corresponding 2D array of errors - nsamples is number of samples to draw from error distribution condition_error_model must return array of samples with shape (X.shape[0], nsamples, X.shape[1]). If condition_error_model is not provided, Gaussian error model assumed. |
None
|
autoscale_conditions |
bool
|
Sets whether or not conditions are automatically standard scaled when passed to a conditional flow. I recommend you leave as True. |
True
|
N |
int
|
The number of flows in the ensemble. |
1
|
info |
Any
|
An object to attach to the info attribute. |
None
|
file |
str
|
Path to file from which to load a pretrained flow ensemble. If a file is provided, all other parameters must be None. |
None
|
Source code in pzflow/flowEnsemble.py
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 |
|
log_prob(inputs, err_samples=None, seed=None, returnEnsemble=False)
Calculates log probability density of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Input data for which log probability density is calculated. Every column in self.data_columns must be present. If self.conditional_columns is not None, those must be present as well. If other columns are present, they are ignored. |
required |
err_samples |
int
|
Number of samples from the error distribution to average over for
the log_prob calculation. If provided, Gaussian errors are assumed,
and method will look for error columns in |
None
|
seed |
int
|
Random seed for drawing the samples with Gaussian errors. |
None
|
returnEnsemble |
bool
|
If True, returns log_prob for each flow in the ensemble as an array of shape (inputs.shape[0], N flows in ensemble). If False, the prob is averaged over the flows in the ensemble, and the log of this average is returned as an array of shape (inputs.shape[0],) |
False
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
For shape, see returnEnsemble description above. |
Source code in pzflow/flowEnsemble.py
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 |
|
posterior(inputs, column, grid, marg_rules=None, normalize=True, err_samples=None, seed=None, batch_size=None, returnEnsemble=False, nan_to_zero=True)
Calculates posterior distributions for the provided column.
Calculates the conditional posterior distribution, assuming the data values in the other columns of the DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Data on which the posterior distributions are conditioned. Must have columns matching self.data_columns, except for the column specified for the posterior (see below). |
required |
column |
str
|
Name of the column for which the posterior distribution
is calculated. Must be one of the columns in self.data_columns.
However, whether or not this column is one of the columns in
|
required |
grid |
jnp.ndarray
|
Grid on which to calculate the posterior. |
required |
marg_rules |
dict
|
Dictionary with rules for marginalizing over missing variables. The dictionary must contain the key "flag", which gives the flag that indicates a missing value. E.g. if missing values are given the value 99, the dictionary should contain {"flag": 99}. The dictionary must also contain {"name": callable} for any variables that will need to be marginalized over, where name is the name of the variable, and callable is a callable that takes the row of variables nad returns a grid over which to marginalize the variable. E.g. {"y": lambda row: jnp.linspace(0, row["x"], 10)}. Note: the callable for a given name must always return an array of the same length, regardless of the input row. |
None
|
normalize |
boolean
|
Whether to normalize the posterior so that it integrates to 1. |
True
|
err_samples |
int
|
Number of samples from the error distribution to average over for
the posterior calculation. If provided, Gaussian errors are assumed,
and method will look for error columns in |
None
|
seed |
int
|
Random seed for drawing the samples with Gaussian errors. |
None
|
batch_size |
int
|
Size of batches in which to calculate posteriors. If None, all posteriors are calculated simultaneously. Simultaneous calculation is faster, but memory intensive for large data sets. |
None
|
returnEnsemble |
bool
|
If True, returns posterior for each flow in the ensemble as an array of shape (inputs.shape[0], N flows in ensemble, grid.size). If False, the posterior is averaged over the flows in the ensemble, and returned as an array of shape (inputs.shape[0], grid.size) |
False
|
nan_to_zero |
bool
|
Whether to convert NaN's to zero probability in the final pdfs. |
True
|
Returns:
Type | Description |
---|---|
jnp.ndarray
|
For shape, see returnEnsemble description above. |
Source code in pzflow/flowEnsemble.py
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 |
|
sample(nsamples=1, conditions=None, save_conditions=True, seed=None, returnEnsemble=False)
Returns samples from the ensemble.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nsamples |
int
|
The number of samples to be returned, either overall or per flow in the ensemble (see returnEnsemble below). |
1
|
conditions |
pd.DataFrame
|
If this is a conditional flow, you must pass conditions for each sample. nsamples will be drawn for each row in conditions. |
None
|
save_conditions |
bool
|
If true, conditions will be saved in the DataFrame of samples that is returned. |
True
|
seed |
int
|
Sets the random seed for the samples. |
None
|
returnEnsemble |
bool
|
If True, nsamples is drawn from each flow in the ensemble. If False, nsamples are drawn uniformly from the flows in the ensemble. |
False
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
Pandas DataFrame of samples. |
Source code in pzflow/flowEnsemble.py
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 |
|
save(file)
Saves the ensemble to a file.
Pickles the ensemble and saves it to a file that can be passed as
the file
argument during flow instantiation.
WARNING: Currently, this method only works for bijectors that are
implemented in the bijectors
module. If you want to save a flow
with a custom bijector, you either need to add the bijector to that
module, or handle the saving and loading on your end.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
Path to where the ensemble will be saved.
Extension |
required |
Source code in pzflow/flowEnsemble.py
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 |
|
train(inputs, val_set=None, epochs=50, batch_size=1024, optimizer=None, loss_fn=None, convolve_errs=False, patience=None, best_params=True, seed=0, verbose=False, progress_bar=False)
Trains the normalizing flows on the provided inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
pd.DataFrame
|
Data on which to train the normalizing flows. Must have columns matching self.data_columns. |
required |
val_set |
pd.DataFrame
|
Validation set, of same format as inputs. If provided, validation loss will be calculated at the end of each epoch. |
None
|
epochs |
int
|
Number of epochs to train. |
50
|
batch_size |
int
|
Batch size for training. |
1024
|
optimizer |
optax optimizer
|
An optimizer from Optax. default = optax.adam(learning_rate=1e-3) see https://optax.readthedocs.io/en/latest/index.html for more. |
None
|
loss_fn |
Callable
|
A function to calculate the loss: loss = loss_fn(params, x). If not provided, will be -mean(log_prob). |
None
|
convolve_errs |
bool
|
Whether to draw new data from the error distributions during
each epoch of training. Method will look for error columns in
|
False
|
patience |
int
|
Factor that controls early stopping. Training will stop if the loss doesn't decrease for this number of epochs. |
None
|
best_params |
bool
|
Whether to use the params from the epoch with the lowest loss. Note if a validation set is provided, the epoch with the lowest validation loss is chosen. If False, the params from the final epoch are saved. |
True
|
seed |
int
|
A random seed to control the batching and the (optional) error sampling. |
0
|
verbose |
bool
|
If true, print the training loss every 5% of epochs. |
False
|
progress_bar |
bool
|
If true, display a tqdm progress bar during training. |
False
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary of training losses from every epoch for each flow in the ensemble. |
Source code in pzflow/flowEnsemble.py
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 |
|
utils
Define utility functions for use in other modules.
DenseReluNetwork(out_dim, hidden_layers, hidden_dim)
Create a dense neural network with Relu after hidden layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_dim |
int
|
The output dimension. |
required |
hidden_layers |
int
|
The number of hidden layers |
required |
hidden_dim |
int
|
The dimension of the hidden layers |
required |
Returns:
Name | Type | Description |
---|---|---|
init_fun |
function
|
The function that initializes the network. Note that this is the init_function defined in the Jax stax module, which is different from the functions of my InitFunction class. |
forward_fun |
function
|
The function that passes the inputs through the neural network. |
Source code in pzflow/utils.py
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 |
|
RationalQuadraticSpline(inputs, W, H, D, B, periodic=False, inverse=False)
Apply rational quadratic spline to inputs and return outputs with log_det.
Applies the piecewise rational quadratic spline developed in [1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
jnp.ndarray
|
The inputs to be transformed. |
required |
W |
jnp.ndarray
|
The widths of the spline bins. |
required |
H |
jnp.ndarray
|
The heights of the spline bins. |
required |
D |
jnp.ndarray
|
The derivatives of the inner spline knots. |
required |
B |
float
|
Range of the splines. Outside of (-B,B), the transformation is just the identity. |
required |
inverse |
bool
|
If True, perform the inverse transformation. Otherwise perform the forward transformation. |
False
|
periodic |
bool
|
Whether to make this a periodic, Circular Spline [2]. |
False
|
Returns:
Name | Type | Description |
---|---|---|
outputs |
jnp.ndarray
|
The result of applying the splines to the inputs. |
log_det |
jnp.ndarray
|
The log determinant of the Jacobian at the inputs. |
References
[1] Conor Durkan, Artur Bekasov, Iain Murray, George Papamakarios. Neural Spline Flows. arXiv:1906.04032, 2019. https://arxiv.org/abs/1906.04032 [2] Rezende, Danilo Jimenez et al. Normalizing Flows on Tori and Spheres. arxiv:2002.02428, 2020 http://arxiv.org/abs/2002.02428
Source code in pzflow/utils.py
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 |
|
build_bijector_from_info(info)
Build a Bijector from a Bijector_Info object
Source code in pzflow/utils.py
11 12 13 14 15 16 17 18 19 |
|
gaussian_error_model(key, X, Xerr, nsamples)
Default Gaussian error model were X are the means and Xerr are the stds.
Source code in pzflow/utils.py
52 53 54 55 56 57 58 59 60 61 |
|
sub_diag_indices(inputs)
Return indices for diagonal of 2D blocks in 3D array
Source code in pzflow/utils.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|