The menagerie of GenerativeFunction
¶
Generative functions are probabilistic building blocks. They allow you to express complex probability distributions, and automate several operations on them. GenJAX exports a standard library of generative functions, and this page catalogues them and their usage.
The venerable & reliable Distribution
¶
To start, distributions are generative functions.
genjax.Distribution
¶
Bases: Generic[R]
, GenerativeFunction[R]
Methods:
Name | Description |
---|---|
random_weighted |
|
estimate_logpdf |
|
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
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 |
|
Distributions intentionally expose a permissive interface (random_weighted
and estimate_logpdf
which doesn't assume exact density evaluation. genjax.ExactDensity
is a more restrictive interface, which assumes exact density evaluation.
genjax.ExactDensity
¶
Bases: Generic[R]
, Distribution[R]
Methods:
Name | Description |
---|---|
random_weighted |
Given arguments to the distribution, sample from the distribution, and return the exact log density of the sample, and the sample. |
estimate_logpdf |
Given a sample and arguments to the distribution, return the exact log density of the sample. |
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
random_weighted
¶
random_weighted(key: PRNGKey, *args) -> tuple[Score, R]
Given arguments to the distribution, sample from the distribution, and return the exact log density of the sample, and the sample.
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
GenJAX exports a long list of exact density distributions, which uses the functionality of tfp.distributions
. A list of these is shown below.
genjax.generative_functions.distributions
¶
Attributes:
Name | Type | Description |
---|---|---|
bernoulli |
A |
|
beta |
A |
|
beta_binomial |
A |
|
beta_quotient |
A |
|
binomial |
ExactDensity[Array]
|
A |
categorical |
A |
|
cauchy |
A |
|
chi |
A |
|
chi2 |
A |
|
dirichlet |
A |
|
dirichlet_multinomial |
A |
|
double_sided_maxwell |
A |
|
exp_gamma |
A |
|
exp_inverse_gamma |
A |
|
exponential |
A |
|
flip |
A |
|
gamma |
A |
|
geometric |
A |
|
gumbel |
A |
|
half_cauchy |
A |
|
half_normal |
A |
|
half_student_t |
A |
|
inverse_gamma |
A |
|
kumaraswamy |
A |
|
lambert_w_normal |
A |
|
laplace |
A |
|
log_normal |
A |
|
logit_normal |
A |
|
moyal |
A |
|
multinomial |
A |
|
mv_normal |
A |
|
mv_normal_diag |
A |
|
negative_binomial |
A |
|
non_central_chi2 |
A |
|
normal |
A |
|
poisson |
A |
|
power_spherical |
A |
|
skellam |
A |
|
student_t |
A |
|
truncated_cauchy |
A |
|
truncated_normal |
A |
|
uniform |
A |
|
von_mises |
A |
|
von_mises_fisher |
A |
|
weibull |
A |
|
zipf |
A |
bernoulli
module-attribute
¶
bernoulli = tfp_distribution(
implicit_logit_warning(Bernoulli), name="bernoulli"
)
A tfp_distribution
generative function which wraps the tfd.Bernoulli
distribution from TensorFlow Probability distributions.
Takes an N-D Tensor representing the log-odds of a 1 event. Each entry in the Tensor parameterizes an independent Bernoulli distribution where the probability of an event is sigmoid(logits).
(Note that this is the logits
argument to the tfd.Bernoulli
constructor.)
beta
module-attribute
¶
beta = tfp_distribution(Beta)
A tfp_distribution
generative function which wraps the tfd.Beta
distribution from TensorFlow Probability distributions.
beta_binomial
module-attribute
¶
beta_binomial = tfp_distribution(BetaBinomial)
A tfp_distribution
generative function which wraps the tfd.BetaBinomial
distribution from TensorFlow Probability distributions.
beta_quotient
module-attribute
¶
beta_quotient = tfp_distribution(BetaQuotient)
A tfp_distribution
generative function which wraps the tfd.BetaQuotient
distribution from TensorFlow Probability distributions.
binomial
module-attribute
¶
binomial: ExactDensity[Array] = tfp_distribution(Binomial)
A tfp_distribution
generative function which wraps the tfd.Binomial
distribution from TensorFlow Probability distributions.
categorical
module-attribute
¶
categorical = tfp_distribution(
implicit_logit_warning(Categorical), name="categorical"
)
A tfp_distribution
generative function which wraps the tfd.Categorical
distribution from TensorFlow Probability distributions.
cauchy
module-attribute
¶
cauchy = tfp_distribution(Cauchy)
A tfp_distribution
generative function which wraps the tfd.Cauchy
distribution from TensorFlow Probability distributions.
chi
module-attribute
¶
chi = tfp_distribution(Chi)
A tfp_distribution
generative function which wraps the tfd.Chi
distribution from TensorFlow Probability distributions.
chi2
module-attribute
¶
chi2 = tfp_distribution(Chi2)
A tfp_distribution
generative function which wraps the tfd.Chi2
distribution from TensorFlow Probability distributions.
dirichlet
module-attribute
¶
dirichlet = tfp_distribution(Dirichlet)
A tfp_distribution
generative function which wraps the tfd.Dirichlet
distribution from TensorFlow Probability distributions.
dirichlet_multinomial
module-attribute
¶
dirichlet_multinomial = tfp_distribution(
DirichletMultinomial
)
A tfp_distribution
generative function which wraps the tfd.DirichletMultinomial
distribution from TensorFlow Probability distributions.
double_sided_maxwell
module-attribute
¶
double_sided_maxwell = tfp_distribution(DoublesidedMaxwell)
A tfp_distribution
generative function which wraps the tfd.DoublesidedMaxwell
distribution from TensorFlow Probability distributions.
exp_gamma
module-attribute
¶
exp_gamma = tfp_distribution(ExpGamma)
A tfp_distribution
generative function which wraps the tfd.ExpGamma
distribution from TensorFlow Probability distributions.
exp_inverse_gamma
module-attribute
¶
exp_inverse_gamma = tfp_distribution(ExpInverseGamma)
A tfp_distribution
generative function which wraps the tfd.ExpInverseGamma
distribution from TensorFlow Probability distributions.
exponential
module-attribute
¶
exponential = tfp_distribution(Exponential)
A tfp_distribution
generative function which wraps the tfd.Exponential
distribution from TensorFlow Probability distributions.
flip
module-attribute
¶
flip = tfp_distribution(
lambda p: Bernoulli(probs=p, dtype=bool_), name="flip"
)
A tfp_distribution
generative function which wraps the tfd.Bernoulli
distribution from TensorFlow Probability distributions, but is constructed using a probability value and not a logit.
Takes an N-D Tensor representing the probability of a 1 event. Each entry in the Tensor parameterizes an independent Bernoulli distribution.
(Note that this is the probs
argument to the tfd.Bernoulli
constructor.)
gamma
module-attribute
¶
gamma = tfp_distribution(Gamma)
A tfp_distribution
generative function which wraps the tfd.Gamma
distribution from TensorFlow Probability distributions.
geometric
module-attribute
¶
geometric = tfp_distribution(Geometric)
A tfp_distribution
generative function which wraps the tfd.Geometric
distribution from TensorFlow Probability distributions.
gumbel
module-attribute
¶
gumbel = tfp_distribution(Gumbel)
A tfp_distribution
generative function which wraps the tfd.Gumbel
distribution from TensorFlow Probability distributions.
half_cauchy
module-attribute
¶
half_cauchy = tfp_distribution(HalfCauchy)
A tfp_distribution
generative function which wraps the tfd.HalfCauchy
distribution from TensorFlow Probability distributions.
half_normal
module-attribute
¶
half_normal = tfp_distribution(HalfNormal)
A tfp_distribution
generative function which wraps the tfd.HalfNormal
distribution from TensorFlow Probability distributions.
half_student_t
module-attribute
¶
half_student_t = tfp_distribution(HalfStudentT)
A tfp_distribution
generative function which wraps the tfd.HalfStudentT
distribution from TensorFlow Probability distributions.
inverse_gamma
module-attribute
¶
inverse_gamma = tfp_distribution(InverseGamma)
A tfp_distribution
generative function which wraps the tfd.InverseGamma
distribution from TensorFlow Probability distributions.
kumaraswamy
module-attribute
¶
kumaraswamy = tfp_distribution(Kumaraswamy)
A tfp_distribution
generative function which wraps the tfd.Kumaraswamy
distribution from TensorFlow Probability distributions.
lambert_w_normal
module-attribute
¶
lambert_w_normal = tfp_distribution(LambertWNormal)
A tfp_distribution
generative function which wraps the tfd.LambertWNormal
distribution from TensorFlow Probability distributions.
laplace
module-attribute
¶
laplace = tfp_distribution(Laplace)
A tfp_distribution
generative function which wraps the tfd.Laplace
distribution from TensorFlow Probability distributions.
log_normal
module-attribute
¶
log_normal = tfp_distribution(LogNormal)
A tfp_distribution
generative function which wraps the tfd.LogNormal
distribution from TensorFlow Probability distributions.
logit_normal
module-attribute
¶
logit_normal = tfp_distribution(LogitNormal)
A tfp_distribution
generative function which wraps the tfd.LogitNormal
distribution from TensorFlow Probability distributions.
moyal
module-attribute
¶
moyal = tfp_distribution(Moyal)
A tfp_distribution
generative function which wraps the tfd.Moyal
distribution from TensorFlow Probability distributions.
multinomial
module-attribute
¶
multinomial = tfp_distribution(Multinomial)
A tfp_distribution
generative function which wraps the tfd.Multinomial
distribution from TensorFlow Probability distributions.
mv_normal
module-attribute
¶
mv_normal = tfp_distribution(
MultivariateNormalFullCovariance
)
A tfp_distribution
generative function which wraps the tfd.MultivariateNormalFullCovariance
distribution from TensorFlow Probability distributions.
mv_normal_diag
module-attribute
¶
mv_normal_diag = tfp_distribution(MultivariateNormalDiag)
A tfp_distribution
generative function which wraps the tfd.MultivariateNormalDiag
distribution from TensorFlow Probability distributions.
negative_binomial
module-attribute
¶
negative_binomial = tfp_distribution(NegativeBinomial)
A tfp_distribution
generative function which wraps the tfd.NegativeBinomial
distribution from TensorFlow Probability distributions.
non_central_chi2
module-attribute
¶
non_central_chi2 = tfp_distribution(NoncentralChi2)
A tfp_distribution
generative function which wraps the tfd.NoncentralChi2
distribution from TensorFlow Probability distributions.
normal
module-attribute
¶
normal = tfp_distribution(Normal)
A tfp_distribution
generative function which wraps the tfd.Normal
distribution from TensorFlow Probability distributions.
poisson
module-attribute
¶
poisson = tfp_distribution(Poisson)
A tfp_distribution
generative function which wraps the tfd.Poisson
distribution from TensorFlow Probability distributions.
power_spherical
module-attribute
¶
power_spherical = tfp_distribution(PowerSpherical)
A tfp_distribution
generative function which wraps the tfd.PowerSpherical
distribution from TensorFlow Probability distributions.
skellam
module-attribute
¶
skellam = tfp_distribution(Skellam)
A tfp_distribution
generative function which wraps the tfd.Skellam
distribution from TensorFlow Probability distributions.
student_t
module-attribute
¶
student_t = tfp_distribution(StudentT)
A tfp_distribution
generative function which wraps the tfd.StudentT
distribution from TensorFlow Probability distributions.
truncated_cauchy
module-attribute
¶
truncated_cauchy = tfp_distribution(TruncatedCauchy)
A tfp_distribution
generative function which wraps the tfd.TruncatedCauchy
distribution from TensorFlow Probability distributions.
truncated_normal
module-attribute
¶
truncated_normal = tfp_distribution(TruncatedNormal)
A tfp_distribution
generative function which wraps the tfd.TruncatedNormal
distribution from TensorFlow Probability distributions.
uniform
module-attribute
¶
uniform = tfp_distribution(Uniform)
A tfp_distribution
generative function which wraps the tfd.Uniform
distribution from TensorFlow Probability distributions.
von_mises
module-attribute
¶
von_mises = tfp_distribution(VonMises)
A tfp_distribution
generative function which wraps the tfd.VonMises
distribution from TensorFlow Probability distributions.
von_mises_fisher
module-attribute
¶
von_mises_fisher = tfp_distribution(VonMisesFisher)
A tfp_distribution
generative function which wraps the tfd.VonMisesFisher
distribution from TensorFlow Probability distributions.
weibull
module-attribute
¶
weibull = tfp_distribution(Weibull)
A tfp_distribution
generative function which wraps the tfd.Weibull
distribution from TensorFlow Probability distributions.
zipf
module-attribute
¶
zipf = tfp_distribution(Zipf)
A tfp_distribution
generative function which wraps the tfd.Zipf
distribution from TensorFlow Probability distributions.
ExactDensity
¶
Bases: Generic[R]
, Distribution[R]
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
estimate_logpdf
¶
estimate_logpdf(key: PRNGKey, v: R, *args) -> Weight
Given a sample and arguments to the distribution, return the exact log density of the sample.
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
exact_density
¶
exact_density(
sample: Callable[..., R],
logpdf: Callable[..., Score],
name: str | None = None,
) -> ExactDensity[R]
Construct a new type, a subclass of ExactDensity, with the given name,
(with genjax.
prepended, to avoid confusion with the underlying object,
which may not share the same interface) and attach the supplied functions
as the sample
and logpdf
methods. The return value is an instance of
this new type, and should be treated as a singleton.
Source code in src/genjax/_src/generative_functions/distributions/distribution.py
tfp_distribution
¶
tfp_distribution(
dist: Callable[..., Distribution],
name: str | None = None,
) -> ExactDensity[Array]
Creates a generative function from a TensorFlow Probability distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Callable[..., Distribution]
|
A callable that returns a TensorFlow Probability distribution. |
required |
Returns:
Type | Description |
---|---|
ExactDensity[Array]
|
A generative function wrapping the TensorFlow Probability distribution. |
This function creates a generative function that encapsulates the sampling and log probability
computation of a TensorFlow Probability distribution. It uses the distribution's sample
and
log_prob
methods to define the generative function's behavior.
Source code in src/genjax/_src/generative_functions/distributions/tensorflow_probability/__init__.py
StaticGenerativeFunction
: a programmatic language¶
For any serious work, you'll want a way to combine generative functions together, mixing deterministic functions with sampling. StaticGenerativeFunction
is a way to do that: it supports the use of a JAX compatible subset of Python to author generative functions. It also supports the ability to invoke other generative functions: instances of this type (and any other type of generative function) can then be used in larger generative programs.
genjax.StaticGenerativeFunction
¶
Bases: Generic[R]
, GenerativeFunction[R]
A StaticGenerativeFunction
is a generative function which relies on program
transformations applied to JAX-compatible Python programs to implement the generative
function interface.
By virtue of the implementation, any source program which is provided to this generative function must be JAX traceable, meaning all the footguns for programs that JAX exposes apply to the source program.
Language restrictions
In addition to JAX footguns, there are a few more which are specific to the generative function interface semantics. Here is the full list of language restrictions (and capabilities):
-
One is allowed to use
jax.lax
control flow primitives so long as the functions provided to the primitives do not containtrace
invocations. In other words, utilizing control flow primitives within the source of aStaticGenerativeFunction
's source program requires that the control flow primitives get deterministic computation. -
The above restriction also applies to
jax.vmap
. -
Source programs are allowed to utilize untraced randomness, although there are restrictions (which we discuss below). It is required to use
jax.random
and JAX's PRNG capabilities. To utilize untraced randomness, you'll need to pass in an extra key as an argument to your model.
Methods:
Name | Description |
---|---|
simulate |
|
assess |
|
Attributes:
Name | Type | Description |
---|---|---|
source |
Closure[R]
|
The source program of the generative function. This is a JAX-compatible Python program. |
Source code in src/genjax/_src/generative_functions/static.py
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 |
|