-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmap_playground.py
36 lines (24 loc) · 904 Bytes
/
vmap_playground.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import haiku as hk
import distrax
import jax.numpy as jnp
import jax
class TestModule(hk.Module):
def __call__(self, x, *args, **kwargs):
mean = hk.nets.MLP((5, 5))(x)
return distrax.Normal(loc=mean, scale=jnp.ones_like(mean))
if __name__ == '__main__':
def f(x):
return TestModule()(x)
fun = hk.without_apply_rng(hk.transform(f))
x = jnp.ones(3)
params = fun.init(x=x, rng=jax.random.PRNGKey(0))
dist = fun.apply(params, x)
def z(x, func):
return func(x)
jax.vmap(z, in_axes=(0, None))(jnp.ones((3, 4)), lambda x: x + 1)
# can we return vmapped distribution object?
dist2 = jax.vmap(fun.apply, in_axes=(None, 0))(params, jnp.ones((4, 3)))
def g(dist):
return dist.sample(seed=jax.random.PRNGKey(0), sample_shape=(3,))
# but we cant vmap over distribution objects.
batch_samples = jax.vmap(g)(dist2)