-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUncertainty.py
314 lines (272 loc) · 10.6 KB
/
Uncertainty.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
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
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import jax
import jax.numpy as jnp
from jax import grad, jit, lax
from jax.experimental.ode import odeint
from jax.example_libraries.optimizers import adam
plt.rcParams['font.size'] = 20
plt.rcParams['lines.linewidth'] = 2
plt.rcParams['axes.linewidth'] = 2
def prbs(cf):
cf = cf + np.random.normal(0,5)
return cf
def Reactor(t,x,*args):
Qf, V, cf, k1, k2, k_2 = args
ca, cb, cc = x
dcadt = Qf*(cf - ca)/V - k1*ca
dcbdt = -Qf*cb/V + k1*ca - 3*(k2*cb**2 - k_2*cc)
dccdt = -Qf*cc/V + k2*cb**2 - k_2*cc
return dcadt, dcbdt, dccdt
t_start = 0
t_end = 200
num_step_ups = 5
t_jumps = t_end/num_step_ups
A,B,C, ta, cfa = [],[],[],[],[]
initial_conditions = [0.3, 0.2, 0.1]
np.random.seed(100)
parameters = [[0.6, 15, 0.3, 0.2, 0.5, 0.1]]
parameters = [list(inner_list) for inner_list in parameters for _ in range(5)]
for i in range(len(parameters)-1):
parameters[i+1][2] = np.random.uniform(0,1.5)
for i, params in enumerate(parameters):
t_start = t_jumps * i
t_end = t_jumps * (i + 1)
t = np.linspace(t_start, t_end, 100)
sol = solve_ivp(Reactor, [t_start, t_end], initial_conditions, method="RK45", t_eval=t, args=params)
initial_conditions = sol.y[:, -1]
A.append(sol.y[0])
B.append(sol.y[1])
C.append(sol.y[2])
ta.append(t)
A = np.concatenate(A)
B = np.concatenate(B)
C = np.concatenate(C)
A = A + np.random.normal(0,0.01,len(A))
B = B + np.random.normal(0,0.01,len(B))
C = C + np.random.normal(0,0.01,len(C))
t = np.concatenate(ta)
for i in range(len(parameters)):
cfa.append([parameters[i][2]]*100)
cfa = np.concatenate(cfa)
def neural_net(params, x, kp):
xz = x
Qf, V, cf = kp
for W, b in params:
xz = jnp.tanh(jnp.dot(W, xz) + b)
dcadt = Qf*(cf - x[0])/V - xz[0]
dcbdt = -Qf*(x[1])/V + xz[0] - xz[1]
return jnp.array([dcadt, dcbdt])
def ode_nn(params, x, cf, kp, z):
xnew = jnp.concatenate([x, jnp.array([cf]), z])
return neural_net(params, xnew, kp)
def update_z(z, new_x):
return jnp.roll(z, -2, axis=-1).at[-2:].set(new_x)
@jit
def RK4_ode(y0, z0, t, params, kp):
dt = t[1] - t[0]
def body_fun(carry, ts_i):
y_prev, z, i = carry
Qf, V, cf = kp
cfi = cf[i]
current_kp = [Qf, V, cfi]
new_x_half = y_prev + (dt / 2) * ode_nn(params, y_prev, cfi, current_kp, z)
z_updated_half = update_z(z, new_x_half)
k1 = ode_nn(params, y_prev,cfi, current_kp, z)
k2 = ode_nn(params, y_prev + 0.5 * dt * k1,cfi, current_kp, z_updated_half)
k3 = ode_nn(params, y_prev + 0.5 * dt * k2,cfi, current_kp, z_updated_half)
new_x_full = y_prev + dt * k3
z_updated_full = update_z(z, new_x_full)
k4 = ode_nn(params, y_prev + dt * k3,cfi, current_kp, z_updated_full)
y_next = y_prev + dt * (k1 + 2. * k2 + 2. * k3 + k4) / 6.
z_next = update_z(z, y_next)
return (y_next, z_next, i + 1), y_next
_, y = lax.scan(body_fun, (y0, z0, 0), jnp.zeros(len(t) - 1))
return jnp.vstack([y0[None, :], y])
def quantile_loss(q, y_true, y_pred):
e = y_true - y_pred
return jnp.maximum(q * e, (q - 1) * e)
@jit
def loss_fn(params, x0, z0, t_span, true_solution, kp, q):
learned_solution = RK4_ode(x0,z0, t_span, params, kp)
return jnp.sum(quantile_loss(q, true_solution, learned_solution))
num_steps = 100*5
t_span_true = jnp.linspace(0., 200, num_steps)
x0_true = jnp.array([0.3, 0.2])
z0_true = jnp.array([0.,0.])
true_solution = [A,B]
true_solution_j = jnp.array(true_solution)
true_solution_jax = true_solution_j.T
kp = [0.6, 15, cfa]
layer_sizes = [3 + z0_true.size, 8, 8, 2]
def init_network_params(layer_sizes, rng_key):
params = []
for n_in, n_out in zip(layer_sizes[:-1], layer_sizes[1:]):
W_key, b_key = jax.random.split(rng_key)
W = 1e-2 * jax.random.normal(W_key, (n_out, n_in))
b = 1e-2 * jax.random.normal(b_key, (n_out,))
params.append((W, b))
return params
rng = jax.random.PRNGKey(0)
params = init_network_params(layer_sizes, rng)
grad_loss = grad(loss_fn)
opt_init, opt_update, get_params = adam(1e-3)
opt_state = opt_init(params)
loss = []
for i in range(2000):
grads = grad_loss(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp, 0.025)
opt_state = opt_update(i, grads, opt_state)
if i % 100 == 0:
loss_value = loss_fn(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp, 0.025)
print(f"Iteration {i}, Loss: {loss_value:.4f}")
loss.append(loss_value)
params_opt1 = get_params(opt_state)
print("Optimized parameters:", params_opt1)
grad_loss = grad(loss_fn)
opt_init, opt_update, get_params = adam(1e-3)
opt_state = opt_init(params)
loss = []
for i in range(2000):
grads = grad_loss(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp, 0.975)
opt_state = opt_update(i, grads, opt_state)
if i % 100 == 0:
loss_value = loss_fn(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp, 0.975)
print(f"Iteration {i}, Loss: {loss_value:.4f}")
loss.append(loss_value)
params_opt2 = get_params(opt_state)
print("Optimized parameters:", params_opt2)
def neural_net(params, x, kp):
xz = x
Qf, V, cf = kp
for W, b in params:
xz = jnp.tanh(jnp.dot(W, xz) + b)
dcadt = Qf*(cf - x[0])/V - xz[0]
dcbdt = -Qf*(x[1])/V + xz[0] - xz[1]
return jnp.array([dcadt, dcbdt])
def ode_nn(params, x, cf, kp, z):
xnew = jnp.concatenate([x, jnp.array([cf]), z])
return neural_net(params, xnew, kp)
def update_z(z, new_x):
return jnp.roll(z, -2, axis=-1).at[-2:].set(new_x)
@jit
def RK4_ode(y0, z0, t, params, kp):
dt = t[1] - t[0]
def body_fun(carry, ts_i):
y_prev, z, i = carry
Qf, V, cf = kp
cfi = cf[i]
current_kp = [Qf, V, cfi]
new_x_half = y_prev + (dt / 2) * ode_nn(params, y_prev, cfi, current_kp, z)
z_updated_half = update_z(z, new_x_half)
k1 = ode_nn(params, y_prev,cfi, current_kp, z)
k2 = ode_nn(params, y_prev + 0.5 * dt * k1,cfi, current_kp, z_updated_half)
k3 = ode_nn(params, y_prev + 0.5 * dt * k2,cfi, current_kp, z_updated_half)
new_x_full = y_prev + dt * k3
z_updated_full = update_z(z, new_x_full)
k4 = ode_nn(params, y_prev + dt * k3,cfi, current_kp, z_updated_full)
y_next = y_prev + dt * (k1 + 2. * k2 + 2. * k3 + k4) / 6.
z_next = update_z(z, y_next)
return (y_next, z_next, i + 1), y_next
_, y = lax.scan(body_fun, (y0, z0, 0), jnp.zeros(len(t) - 1))
return jnp.vstack([y0[None, :], y])
@jit
def loss_fn(params, x0,z0, t_span, true_solution, kp):
learned_solution = RK4_ode(x0,z0, t_span, params, kp)
return jnp.sum((learned_solution - true_solution) ** 2)
num_steps = 100*5
t_span_true = jnp.linspace(0., 200, num_steps)
x0_true = jnp.array([0.3, 0.2])
z0_true = jnp.array([0.,0.])
true_solution = [A,B]
true_solution_j = jnp.array(true_solution)
true_solution_jax = true_solution_j.T
kp = [0.6, 15, cfa]
layer_sizes = [3 + z0_true.size, 8, 8, 2]
def init_network_params(layer_sizes, rng_key):
params = []
for n_in, n_out in zip(layer_sizes[:-1], layer_sizes[1:]):
W_key, b_key = jax.random.split(rng_key)
W = 1e-2 * jax.random.normal(W_key, (n_out, n_in))
b = 1e-2 * jax.random.normal(b_key, (n_out,))
params.append((W, b))
return params
rng = jax.random.PRNGKey(0)
params = init_network_params(layer_sizes, rng)
grad_loss = grad(loss_fn)
opt_init, opt_update, get_params = adam(1e-3)
opt_state = opt_init(params)
loss = []
for i in range(2000):
grads = grad_loss(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp)
opt_state = opt_update(i, grads, opt_state)
if i % 100 == 0:
loss_value = loss_fn(get_params(opt_state), x0_true, z0_true, t_span_true, true_solution_jax, kp)
print(f"Iteration {i}, Loss: {loss_value:.4f}")
loss.append(loss_value)
params_opt = get_params(opt_state)
print("Optimized parameters:", params_opt)
t_start = 0
t_end = 200
num_step_ups = 5
t_jumps = t_end/num_step_ups
A,B,C, ta, cfa = [],[],[],[],[]
initial_conditions = [0.3, 0.2, 0.1]
np.random.seed(500)
parameters = [[0.6, 15, 0.3, 0.2, 0.5, 0.1]]
parameters = [list(inner_list) for inner_list in parameters for _ in range(5)]
for i in range(len(parameters)-1):
parameters[i+1][2] = np.random.uniform(0,1.5)
for i, params in enumerate(parameters):
t_start = t_jumps * i
t_end = t_jumps * (i + 1)
t = np.linspace(t_start, t_end, 100)
sol = solve_ivp(Reactor, [t_start, t_end], initial_conditions, method="RK45", t_eval=t, args=params)
initial_conditions = sol.y[:, -1]
A.append(sol.y[0])
B.append(sol.y[1])
C.append(sol.y[2])
ta.append(t)
A = np.concatenate(A)
B = np.concatenate(B)
C = np.concatenate(C)
A = A + np.random.normal(0,0.01,len(A))
B = B + np.random.normal(0,0.01,len(B))
C = C + np.random.normal(0,0.01,len(C))
t = np.concatenate(ta)
for i in range(len(parameters)):
cfa.append([parameters[i][2]]*100)
cfa = np.concatenate(cfa)
pdf = PdfPages('Uncertainty.pdf')
true_solution = [A,B]
true_solution_j = jnp.array(true_solution)
true_solution_jax = true_solution_j.T
kp = [0.6, 15, cfa]
x0_true = jnp.array([0.3,0.2])
fig, axs = plt.subplots(3, 1, figsize=(9, 10)) # 2 rows, 1 column
fitted_solution = RK4_ode(x0_true, z0_true, t_span_true, params_opt, kp)
f1 = RK4_ode(x0_true, z0_true, t_span_true, params_opt1, kp)
f2 = RK4_ode(x0_true, z0_true, t_span_true, params_opt2, kp)
# Plot A_true and A on the first subplot
axs[0].plot(np.linspace(0,200,len(true_solution_j[0])),true_solution_j[0], 'r', label="Plant")
axs[0].plot(np.linspace(0,200,len(fitted_solution.T[0])),fitted_solution.T[0],'k',label="Struc")
axs[0].fill_between(np.linspace(0,200,len(f1.T[0])), f1.T[0], f2.T[0], color='gray', alpha=0.5)
axs[0].set_ylabel(r"$c_A$")
axs[0].legend()
# Plot B_true (assuming this is the same as the true B) and B on the second subplot
axs[1].plot(np.linspace(0,200,len(true_solution_j[1])), true_solution_j[1], 'g', label="Plant")
axs[1].plot(np.linspace(0,200,len(fitted_solution.T[1])), fitted_solution.T[1],'k',label="Struc")
axs[1].fill_between(np.linspace(0,200,len(f1.T[1])), f1.T[1], f2.T[1], color='gray', alpha=0.5)
axs[1].set_ylabel(r"$c_B$")
axs[1].legend()
axs[2].plot(np.linspace(0,200,len(cfa)), cfa, 'b', label = r"$c_{Af}$")
axs[2].set_xlabel("Time (min)")
axs[2].set_ylabel(r"$c_{Af}$")
#axs[2].plot(C, 'k', label = r"$c_{Af}$")
#axs[2].set_xlabel("Time (min)")
#axs[2].set_ylabel(r"$c_{C}$")
plt.tight_layout()
pdf.savefig(fig)
plt.close(fig)
pdf.close()