Skip to content

Commit f458987

Browse files
authored
Merge pull request #8 from levimcclenny/main
Add transfer learning example to examples/
2 parents 4e63ddf + fccab2e commit f458987

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

examples/transfer-learn.py

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

0 commit comments

Comments
 (0)