forked from SymmeJ/deeponet_solve_ode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaces.py
162 lines (130 loc) · 5.03 KB
/
spaces.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
from pathos.pools import ProcessPool
from scipy import linalg, interpolate
from sklearn import gaussian_process as gp
import config
from utils import eig
class FinitePowerSeries:
def __init__(self, N=100, M=1):
self.N = N
self.M = M
def random(self, n):
return 2 * self.M * np.random.rand(n, self.N) - self.M
def eval_u_one(self, a, x):
return np.dot(a, x ** np.arange(self.N))
def eval_u(self, a, sensors):
mat = np.ones((self.N, len(sensors)))
for i in range(1, self.N):
mat[i] = np.ravel(sensors ** i)
return np.dot(a, mat)
class FiniteChebyshev:
def __init__(self, N=100, M=1):
self.N = N
self.M = M
def random(self, n):
return 2 * self.M * np.random.rand(n, self.N) - self.M
def eval_u_one(self, a, x):
return np.polynomial.chebyshev.chebval(2 * x - 1, a)
def eval_u(self, a, sensors):
return np.polynomial.chebyshev.chebval(2 * np.ravel(sensors) - 1, a.T)
class GRF(object):
def __init__(self, T, kernel="RBF", length_scale=1, N=1000, interp="cubic"):
self.N = N
self.interp = interp
self.x = np.linspace(0, T, num=N)[:, None]
if kernel == "RBF":
K = gp.kernels.RBF(length_scale=length_scale)
elif kernel == "AE":
K = gp.kernels.Matern(length_scale=length_scale, nu=0.5)
self.K = K(self.x)
self.L = np.linalg.cholesky(self.K + 1e-13 * np.eye(self.N))
def random(self, n):
"""Generate `n` random feature vectors.
"""
u = np.random.randn(self.N, n)
return np.dot(self.L, u).T
def eval_u_one(self, y, x):
"""Compute the function value at `x` for the feature `y`.
"""
if self.interp == "linear":
return np.interp(x, np.ravel(self.x), y)
f = interpolate.interp1d(
np.ravel(self.x), y, kind=self.interp, copy=False, assume_sorted=True
)
return f(x)
def eval_u(self, ys, sensors):
"""For a list of functions represented by `ys`,
compute a list of a list of function values at a list `sensors`.
"""
if self.interp == "linear":
return np.vstack([np.interp(sensors, np.ravel(self.x), y).T for y in ys])
p = ProcessPool(nodes=config.processes)
res = p.map(
lambda y: interpolate.interp1d(
np.ravel(self.x), y, kind=self.interp, copy=False, assume_sorted=True
)(sensors).T,
ys,
)
return np.vstack(list(res))
class GRF_KL(object):
def __init__(
self, T, kernel="RBF", length_scale=1, num_eig=10, N=100, interp="cubic"
):
if not np.isclose(T, 1):
raise ValueError("Only support T = 1.")
self.num_eig = num_eig
if kernel == "RBF":
kernel = gp.kernels.RBF(length_scale=length_scale)
elif kernel == "AE":
kernel = gp.kernels.Matern(length_scale=length_scale, nu=0.5)
eigval, eigvec = eig(kernel, num_eig, N, eigenfunction=True)
eigvec *= eigval ** 0.5
x = np.linspace(0, T, num=N)
self.eigfun = [
interpolate.interp1d(x, y, kind=interp, copy=False, assume_sorted=True)
for y in eigvec.T
]
def bases(self, sensors):
return np.array([np.ravel(f(sensors)) for f in self.eigfun])
def random(self, n):
"""Generate `n` random feature vectors.
"""
return np.random.randn(n, self.num_eig)
def eval_u_one(self, y, x):
"""Compute the function value at `x` for the feature `y`.
"""
eigfun = [f(x) for f in self.eigfun]
return np.sum(eigfun * y)
def eval_u(self, ys, sensors):
"""For a list of functions represented by `ys`,
compute a list of a list of function values at a list `sensors`.
"""
eigfun = np.array([np.ravel(f(sensors)) for f in self.eigfun])
return np.dot(ys, eigfun)
def space_samples(space, T):
features = space.random(100000)
sensors = np.linspace(0, T, num=1000)
u = space.eval_u(features, sensors[:, None])
plt.plot(sensors, np.mean(u, axis=0), "k")
plt.plot(sensors, np.std(u, axis=0), "k--")
plt.plot(sensors, np.cov(u.T)[0], "k--")
plt.plot(sensors, np.exp(-0.5 * sensors ** 2 / 0.2 ** 2))
for ui in u[:3]:
plt.plot(sensors, ui)
plt.show()
def main():
# space = FinitePowerSeries(N=100, M=1)
# space = FiniteChebyshev(N=20, M=1)
# space = GRF(1, length_scale=0.2, N=1000, interp="cubic")
# space = GRF_KL(1, length_scale=0.2, num_eig=10, N=100, interp="cubic")
# space_samples(space, 1)
space1 = GRF(1, length_scale=0.1, N=100, interp="cubic")
space2 = GRF(1, length_scale=1, N=100, interp="cubic")
W2 = np.trace(space1.K + space2.K - 2 * linalg.sqrtm(space1.K @ space2.K)) ** 0.5 / 100 ** 0.5
print(W2)
if __name__ == "__main__":
main()