Skip to content

Commit a89d125

Browse files
committed
commit transfer-learn
1 parent 83836f5 commit a89d125

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

examples/transfer-learn.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import scipy.io
2+
import math
3+
import tensordiffeq as tdq
4+
from tensordiffeq.models import CollocationSolverND
5+
from tensordiffeq.boundaries import *
6+
7+
Domain = DomainND(["x", "t"], time_var='t')
8+
9+
Domain.add("x", [-1.0, 1.0], 512)
10+
Domain.add("t", [0.0, 1.0], 201)
11+
12+
N_f = 50000
13+
Domain.generate_collocation_points(N_f)
14+
15+
16+
def func_ic(x):
17+
return x ** 2 * np.cos(math.pi * x)
18+
19+
20+
# Conditions to be considered at the boundaries for the periodic BC
21+
def deriv_model(u_model, x, t):
22+
u = u_model(tf.concat([x, t], 1))
23+
u_x = tf.gradients(u, x)[0]
24+
# u_xx = tf.gradients(u_x, x)[0]
25+
# u_xxx = tf.gradients(u_xx, x)[0]
26+
# u_xxxx = tf.gradients(u_xxx, x)[0]
27+
return u, u_x
28+
29+
30+
init = IC(Domain, [func_ic], var=[['x']])
31+
x_periodic = periodicBC(Domain, ['x'], [deriv_model])
32+
33+
BCs = [init, x_periodic]
34+
35+
36+
def f_model(u_model, x, t):
37+
u = u_model(tf.concat([x, t], 1))
38+
u_x = tf.gradients(u, x)
39+
u_xx = tf.gradients(u_x, x)
40+
u_t = tf.gradients(u, t)
41+
c1 = tdq.utils.constant(.0001)
42+
c2 = tdq.utils.constant(5.0)
43+
f_u = u_t - c1 * u_xx + c2 * u * u * u - c2 * u
44+
return f_u
45+
46+
47+
col_weights = tf.Variable(tf.random.uniform([N_f, 1]), trainable=True, dtype=tf.float32)
48+
u_weights = tf.Variable(100 * tf.random.uniform([512, 1]), trainable=True, dtype=tf.float32)
49+
50+
layer_sizes = [2, 128, 128, 128, 128, 1]
51+
52+
model = CollocationSolverND()
53+
model.compile(layer_sizes, f_model, Domain, BCs, isAdaptive=True, col_weights=col_weights, u_weights=u_weights)
54+
model.fit(tf_iter=5000)
55+
model.save("test_model")
56+
57+
# Must re-initialize the model class in order to effectively transfer learn or resume training
58+
model = CollocationSolverND()
59+
model.compile(layer_sizes, f_model, Domain, BCs, isAdaptive=True, col_weights=col_weights, u_weights=u_weights)
60+
model.tf_optimizer = tf.keras.optimizers.Adam(.0001)
61+
model.tf_optimizer_weights= tf.keras.optimizers.Adam(.0001)
62+
model.load_model("test_model")
63+
model.fit(tf_iter=5000)
64+
65+
# Must re-initialize the model class in order to effectively transfer learn or resume training
66+
model = CollocationSolverND()
67+
model.compile(layer_sizes, f_model, Domain, BCs, isAdaptive=True, col_weights=col_weights, u_weights=u_weights)
68+
model.tf_optimizer = tf.keras.optimizers.Adam(.00001)
69+
model.tf_optimizer_weights= tf.keras.optimizers.Adam(.00001)
70+
model.load_model("test_model")
71+
model.fit(tf_iter=5000)
72+
73+
# Load high-fidelity data for error calculation
74+
data = scipy.io.loadmat('AC.mat')
75+
76+
Exact = data['uu']
77+
Exact_u = np.real(Exact)
78+
79+
80+
81+
x = Domain.domaindict[0]['xlinspace']
82+
t = Domain.domaindict[1]["tlinspace"]
83+
84+
# create mesh for plotting
85+
86+
X, T = np.meshgrid(x, t)
87+
88+
X_star = np.hstack((X.flatten()[:, None], T.flatten()[:, None]))
89+
u_star = Exact_u.T.flatten()[:, None]
90+
91+
# forward pass through model
92+
u_pred, f_u_pred = model.predict(X_star)
93+
94+
error_u = tdq.helpers.find_L2_error(u_pred, u_star)
95+
print('Error u: %e' % (error_u))
96+
97+
U_pred = tdq.plotting.get_griddata(X_star, u_pred.flatten(), (X, T))
98+
FU_pred = tdq.plotting.get_griddata(X_star, f_u_pred.flatten(), (X, T))
99+
100+
lb = np.array([-1.0, 0.0])
101+
ub = np.array([1.0, 1])
102+
103+
tdq.plotting.plot_solution_domain1D(model, [x, t], ub=ub, lb=lb, Exact_u=Exact_u)

0 commit comments

Comments
 (0)