-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAllenCahn100D.py
118 lines (89 loc) · 3.89 KB
/
AllenCahn100D.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
import sys
sys.path.insert(0, '../')
import numpy as np
import torch
import matplotlib.pyplot as plt
import time
# from FBSNNs import FBSNN
from MTL_FBSNNs_Allen100D_uncert import FBSNN
print('Uncert training')
class AllenCahn(FBSNN):
def __init__(self, Xi, T, M, N, D, layers, mode, activation):
super().__init__(Xi, T, M, N, D, layers, mode, activation)
def phi_tf(self, t, X, Y, Z): # M x 1, M x D, M x 1, M x D
return - Y + Y ** 3 # M x 1
def g_tf(self, X):
return 1.0 / (2.0 + 0.4 * torch.sum(X ** 2, 1, keepdim=True))
def aux_g_tf(self, X):
return 1.0 / (2.0 + 0.3 * torch.sum(X ** 2, 1, keepdim=True))
def mu_tf(self, t, X, Y, Z): # M x 1, M x D, M x 1, M x D
return super().mu_tf(t, X, Y, Z) # M x D
def sigma_tf(self, t, X, Y): # M x 1, M x D, M x 1
return super().sigma_tf(t, X, Y) # M x D x D
###########################################################################
def u_exact(t, X): # (N+1) x 1, (N+1) x D
r = 0.05
sigma_max = 0.4
return np.exp((r + sigma_max ** 2) * (T - t)) * np.sum(X ** 2, 1, keepdims=True) # (N+1) x 1
def run_model(model, N_Iter, learning_rate):
training_mode = 'Uncert'
tot = time.time()
samples = 5
print(model.device)
graph = model.train(N_Iter, learning_rate)
print("total time:", time.time() - tot, "s")
model.model.load_state_dict(torch.load('allen_uncert.pth'))
np.random.seed(42)
t_test, W_test = model.fetch_minibatch()
X_pred, Y_pred = model.predict(Xi, t_test, W_test)
if type(t_test).__module__ != 'numpy':
t_test = t_test.cpu().numpy()
if type(X_pred).__module__ != 'numpy':
X_pred = X_pred.cpu().detach().numpy()
if type(Y_pred).__module__ != 'numpy':
Y_pred = Y_pred.cpu().detach().numpy()
Y_test = np.reshape(u_exact(np.reshape(t_test[0:M, :, :], [-1, 1]), np.reshape(X_pred[0:M, :, :], [-1, D])),
[M, -1, 1])
plt.figure()
plt.plot(graph[0], graph[1])
plt.xlabel('Iterations')
plt.ylabel('Value')
plt.yscale("log")
plt.title('Evolution of the training loss')
plt.figure()
plt.plot(t_test[0:1, :, 0].T, Y_pred[0:1, :, 0].T, 'b', label='Learned $u(t,X_t)$')
plt.plot(t_test[0:1, :, 0].T, Y_test[0:1, :, 0].T, 'r--', label='Exact $u(t,X_t)$')
plt.plot(t_test[0:1, -1, 0], Y_test[0:1, -1, 0], 'ko', label='$Y_T = u(T,X_T)$')
plt.plot(t_test[1:samples, :, 0].T, Y_pred[1:samples, :, 0].T, 'b')
plt.plot(t_test[1:samples, :, 0].T, Y_test[1:samples, :, 0].T, 'r--')
plt.plot(t_test[1:samples, -1, 0], Y_test[1:samples, -1, 0], 'ko')
plt.plot([0], Y_test[0, 0, 0], 'ks', label='$Y_0 = u(0,X_0)$')
plt.xlabel('$t$')
plt.ylabel('$Y_t = u(t,X_t)$')
plt.title(str(D) + '-dimensional Allen-Cahn, ' + training_mode)
plt.legend()
errors = np.sqrt((Y_test - Y_pred) ** 2 / Y_test ** 2)
mean_errors = np.mean(errors, 0)
std_errors = np.std(errors, 0)
print('mean errors across time', np.mean(mean_errors))
plt.figure()
plt.plot(t_test[0, :, 0], mean_errors, 'b', label='mean')
plt.plot(t_test[0, :, 0], mean_errors + 2 * std_errors, 'r--', label='mean + two standard deviations')
plt.xlabel('$t$')
plt.ylabel('relative error')
plt.title(str(D) + '-dimensional Allen-Cahn, ' + training_mode)
plt.legend()
plt.savefig(str(D) + '-dimensional Allen-Cahn, ' + training_mode)
if __name__ == "__main__":
tot = time.time()
M = 100 # number of trajectories (batch size)
N = 50 # number of time snapshots
D = 100 # number of dimensions
layers = [D + 1] + 4 * [256] + [1]
Xi = np.array([1.0, 0.5] * int(D / 2))[None, :]
T = 1.0
"Available architectures"
mode = "FC" # FC, Resnet and NAIS-Net are available
activation = "Sine" # sine and ReLU are available
model = AllenCahn(Xi, T, M, N, D, layers, mode, activation)
run_model(model, int(2e4), 1e-3)