Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
def func(x):
"""
x: array_like, N x D_in
y: array_like, N x D_out
"""
return x * np.sin(5 * x)
geom = dde.geometry.Interval(-1, 1)
num_train = 16
num_test = 100
data = dde.data.Func(geom, func, num_train, num_test)
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN([1] + [20] * 3 + [1], activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=10000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
return -dy_xx - np.pi ** 2 * tf.sin(np.pi * x)
def boundary(x, on_boundary):
return on_boundary
def func(x):
return np.sin(np.pi * x)
geom = dde.geometry.Interval(-1, 1)
bc = dde.DirichletBC(geom, func, boundary)
data = dde.data.PDE(geom, 1, pde, bc, 16, 2, func=func, num_test=100)
layer_size = [1] + [50] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
checkpointer = dde.callbacks.ModelCheckpoint(
"./model/model.ckpt", verbose=1, save_better_only=True
)
movie = dde.callbacks.MovieDumper(
"model/movie", [-1], [1], period=100, save_spectrum=True, y_reference=func
)
losshistory, train_state = model.train(
epochs=10000, callbacks=[checkpointer, movie]
)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def boundary_r(x, on_boundary):
return on_boundary and np.isclose(x[0], 1)
def func(x):
return np.sin(np.pi * x)
geom = dde.geometry.Interval(-1, 1)
bc1 = dde.DirichletBC(geom, func, boundary_l)
bc2 = dde.PeriodicBC(geom, 0, boundary_r)
data = dde.data.PDE(geom, 1, pde, [bc1, bc2], 16, 2, func=func, num_test=100)
layer_size = [1] + [50] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=10000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
data = dde.data.TimePDE(
geomtime,
1,
pde,
[bc, ic],
num_domain=40,
num_boundary=20,
num_initial=10,
func=func,
num_test=10000,
)
layer_size = [2] + [32] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=10000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:])
geom = dde.geometry.Interval(-1, 1)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary)
ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial)
data = dde.data.TimePDE(
geomtime, 1, pde, [bc, ic], num_domain=40, func=func, num_test=10000
)
layer_size = [2] + [32] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
net.outputs_modify(
lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + tf.sin(np.pi * x[:, 0:1])
)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=10000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
quad_deg = 20
data = dde.data.IDE(
geom,
ide,
bc,
quad_deg,
kernel=kernel,
num_domain=10,
num_boundary=2,
train_distribution="uniform",
)
layer_size = [1] + [20] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("L-BFGS-B")
model.train()
X = geom.uniform_points(100)
y_true = func(X)
y_pred = model.predict(X)
print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
plt.figure()
plt.plot(X, y_true, "-")
plt.plot(X, y_pred, "o")
plt.show()
np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
fname_lo_train = "dataset/mf_lo_train.dat"
fname_hi_train = "dataset/mf_hi_train.dat"
fname_hi_test = "dataset/mf_hi_test.dat"
data = dde.data.MfDataSet(
fname_lo_train=fname_lo_train,
fname_hi_train=fname_hi_train,
fname_hi_test=fname_hi_test,
col_x=(0,),
col_y=(1,),
)
activation = "tanh"
initializer = "Glorot uniform"
regularization = ["l2", 0.01]
net = dde.maps.MfNN(
[1] + [20] * 4 + [1],
[10] * 2 + [1],
activation,
initializer,
regularization=regularization,
)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=80000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main():
def func_lo(x):
A, B, C = 0.5, 10, -5
return A * (6 * x - 2) ** 2 * np.sin(12 * x - 4) + B * (x - 0.5) + C
def func_hi(x):
return (6 * x - 2) ** 2 * np.sin(12 * x - 4)
geom = dde.geometry.Interval(0, 1)
num_test = 1000
data = dde.data.MfFunc(geom, func_lo, func_hi, 100, 6, num_test)
activation = "tanh"
initializer = "Glorot uniform"
regularization = ["l2", 0.01]
net = dde.maps.MfNN(
[1] + [20] * 4 + [1],
[10] * 2 + [1],
activation,
initializer,
regularization=regularization,
)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=80000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
dy_yy = tf.gradients(dy_y, x)[0][:, 1:]
return -dy_xx - dy_yy - 1
def boundary(x, on_boundary):
return on_boundary
def func(x):
return np.zeros([len(x), 1])
geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1], [0, 1]])
bc = dde.DirichletBC(geom, func, boundary)
data = dde.data.PDE(
geom, 1, pde, bc, num_domain=1200, num_boundary=120, num_test=1500
)
net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform")
model = dde.Model(data, net)
model.compile("adam", lr=0.001)
model.train(epochs=50000)
model.compile("L-BFGS-B")
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
"""
x: array_like, N x D_in
y: array_like, N x D_out
"""
return np.sin(2 * np.pi * x)
geom = dde.geometry.Interval(0, 1)
bc = dde.DirichletBC(geom, func, boundary)
quad_deg = 16
data = dde.data.IDE(geom, ide, bc, quad_deg, num_domain=16, num_boundary=2)
layer_size = [1] + [20] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001)
model.train(epochs=10000)
X = geom.uniform_points(100, True)
y_true = func(X)
y_pred = model.predict(X)
print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
plt.figure()
plt.plot(X, y_true, "-")
plt.plot(X, y_pred, "o")
plt.show()
np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))