-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
162 lines (129 loc) · 4.05 KB
/
utils.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
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
r"""Manifold experiment helpers"""
import jax.numpy as jnp
import math
import matplotlib.pyplot as plt
import os
import ot
import pandas as pd
import seaborn as sb
from functools import partial
from jax import Array
from typing import *
# isort: split
from priors.common import *
from priors.diffusion import *
from priors.image import *
from priors.nn import *
from priors.optim import *
if 'SCRATCH' in os.environ:
SCRATCH = os.environ['SCRATCH']
PATH = Path(SCRATCH) / 'priors/manifold'
else:
PATH = Path('.')
PATH.mkdir(parents=True, exist_ok=True)
def measure(A: Array, x: Array) -> Array:
return jnp.einsum('...ij,...j', A, x)
def show_pair(y: Array, cmap: str = 'Blues', **kwargs) -> plt.Figure:
cmap = plt.get_cmap(cmap)
colors = [cmap(i) for i in range(16, cmap.N)]
colors = [(1.0, 1.0, 1.0), *colors]
cmap = plt.cm.colors.ListedColormap(colors)
return sb.histplot(
data=pd.DataFrame({'$y_0$': y[:, 0], '$y_1$': y[:, 1]}),
x='$y_0$',
y='$y_1$',
bins=64,
binrange=(-3, 3),
thresh=None,
cmap=cmap,
**kwargs,
)
def show_corner(x: Array, cmap: str = 'Blues', **kwargs) -> plt.Figure:
cmap = plt.get_cmap(cmap)
colors = [cmap(i) for i in range(16, cmap.N)]
colors = [(1.0, 1.0, 1.0), *colors]
cmap = plt.cm.colors.ListedColormap(colors)
return sb.pairplot(
data=pd.DataFrame({f'$x_{i}$': xi for i, xi in enumerate(np.asarray(x).T)}),
corner=True,
kind='hist',
plot_kws={'bins': 64, 'binrange': (-3, 3), 'thresh': None, 'cmap': cmap},
diag_kws={'bins': 64, 'binrange': (-3, 3), 'element': 'step', 'color': cmap(cmap.N // 2)},
**kwargs,
)
def sinkhorn_divergence(
u1: Array,
u2: Array,
v: Array,
lmbda: float = 1e-3,
maxiter: int = 1024,
epsilon: float = 1e-3,
) -> Array:
r"""Computes the Sinkhorn divergence between two samples.
References:
| Faster Wasserstein Distance Estimation with the Sinkhorn Divergence (Chizat et al., 2020)
| https://arxiv.org/abs/2006.08172
"""
def transport(u, v):
return ot.sinkhorn2(
a=jnp.asarray(()),
b=jnp.asarray(()),
M=ot.dist(u, v),
reg=lmbda,
numItermax=maxiter,
stopThr=epsilon,
method='sinkhorn_log',
)
return jnp.maximum(transport(u1, v) - transport(u1, u2), 1e-6)
def smooth_manifold(
key: Array,
shape: Sequence[int] = (),
m: int = 1,
n: int = 3,
alpha: float = 3.0,
epsilon: float = 1e-3,
) -> Array:
r"""Samples points from a smooth random manifold.
References:
https://github.com/fzenke/randman
Arguments:
m: The manifold dimension.
n: The space dimension.
alpha: The smoothness coefficient.
"""
key_params, key_z = jax.random.split(key, 2)
cutoff = math.ceil(epsilon ** (-1 / alpha))
k = jnp.arange(cutoff) + 1
a, b, c = jax.random.uniform(key_params, (3, n, m, cutoff))
z = jax.random.uniform(key_z, (*shape, 1, m, 1))
x = a / k**alpha * jnp.sin(2 * jnp.pi * (k * b * z + c))
x = jnp.sum(x, axis=-1)
x = jnp.prod(x, axis=-1)
return x
def make_model(
key: Array,
features: int,
hid_features: Sequence[int] = (256, 256, 256),
emb_features: int = 64,
normalize: bool = True,
**absorb,
) -> Denoiser:
return Denoiser(
network=TimeMLP(
features=features,
hid_features=hid_features,
emb_features=emb_features,
normalize=normalize,
key=key,
),
emb_features=emb_features,
)
class TimeMLP(MLP):
def __init__(self, features: int, emb_features: int = 64, **kwargs):
super().__init__(features + emb_features, features, **kwargs)
@staticmethod
@partial(jnp.vectorize, signature='(m),(n)->(p)')
def cat(x: Array, y: Array) -> Array:
return jnp.concatenate((x, y))
def __call__(self, x: Array, t: Array, key: Array = None) -> Array:
return super().__call__(self.cat(x, t))