-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoise.py
More file actions
46 lines (33 loc) · 1.4 KB
/
noise.py
File metadata and controls
46 lines (33 loc) · 1.4 KB
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
36
37
38
39
40
41
42
import jax
import jax.numpy as jnp
import numpy as np
def gaussiannoise(param, key, std):
treedef = jax.tree_util.tree_structure(param)
num_vars = len(jax.tree_util.tree_leaves(param))
all_keys = jax.random.split(key, num=(num_vars + 1))
noise = jax.tree_map(lambda p, k: std * jax.random.normal(k, shape=p.shape), param,
jax.tree_util.tree_unflatten(treedef, all_keys[1:]))
return noise
def rayleighnoise(param, key, _, mc):
noise = jax.tree_map(
lambda p: jnp.array(np.random.rayleigh(1.0, size=(mc, *p.shape))),
param)
return noise
def uniformnoise(param, key, noisestrength):
treedef = jax.tree_util.tree_structure(param)
num_vars = len(jax.tree_util.tree_leaves(param))
all_keys = jax.random.split(key, num=(num_vars + 1))
noise = jax.tree_map(
lambda p, k: noisestrength * (jax.random.uniform(key, shape=p.shape) - 0.5),
param,
jax.tree_util.tree_unflatten(treedef, all_keys[1:]))
return noise
def laplacenoise(param, key, noisestrength):
treedef = jax.tree_util.tree_structure(param)
num_vars = len(jax.tree_util.tree_leaves(param))
all_keys = jax.random.split(key, num=(num_vars + 1))
noise = jax.tree_map(
lambda p, k: noisestrength * jax.random.laplace(k, shape=p.shape),
param,
jax.tree_util.tree_unflatten(treedef, all_keys[1:]))
return noise