the function i try to solve is Boundary Value Problem
enter image description here
import torch
import numpy as np
import matplotlib.pyplot as plt
torch.manual_seed(123)
class MLP(torch.nn.Module):
def __init__(self):
super(MLP,self).__init__()
self.net = torch.nn.Sequential(
torch.nn.Linear(1,128),
torch.nn.Tanh(),
torch.nn.Linear(128,128),
torch.nn.Tanh(),
torch.nn.Linear(128,128),
torch.nn.Tanh(),
torch.nn.Linear(128,128),
torch.nn.Tanh(),
torch.nn.Linear(128,1),
)
def forward(self,x):
return self.net(x)
def exact_solution(ep, t):
return (1-torch.exp(-(t+1)/2/ep))/(1-np.exp(-1/ep))
def gradients(u,x,order =1):
if order == 1:
return torch.autograd.grad(u,x,grad_outputs=torch.ones_like(u),
create_graph=True,
only_inputs=True,
retain_graph=True
)[0]
else:
return gradients(gradients(u,x),x,order = order-1)
pinn = MLP()
t_boundary1 = torch.tensor(0.).view(-1,1).requires_grad_(True)-1
t_boundary2 = torch.tensor(0.).view(-1,1).requires_grad_(True)+1# (1, 1)
points = torch.normal(-1, 0.1, size=(200,1))
t_physics = torch.linspace(-0.98,1,100).view(-1,1).requires_grad_(True)
t_physics0 = torch.distributions.Uniform(-1, -0.98).sample((60,1)).requires_grad_(True)
#t_physics0 = points[(points >= -1) & (points <= -0.95)].view(-1,1).requires_grad_(True)
tt=torch.cat([t_physics,t_physics0],dim=0)
ep=1e-3
t_test = torch.linspace(-1,1,600).view(-1,1)
u_exact = exact_solution(ep, t_test)
optimiser = torch.optim.Adam(pinn.parameters(),lr=1e-3)
w_boundary=1.0
w_physics=1.0
for i in range(50001):
optimiser.zero_grad()
u1 = pinn(t_boundary1)
loss1 = (torch.squeeze(u1) - 0)**2
u2 = pinn(t_boundary2)
loss2 = (torch.squeeze(u2) - 1)**2
u = pinn(t_physics)
dudt =gradients(u,t_physics,1)
d2udt2 =gradients(u,t_physics,2)
loss3 = torch.mean((ep*d2udt2 +1/2*dudt)**2)
loss = w_boundary*(loss1 + loss2) + w_physics*loss3
loss.backward()
optimiser.step()
if i % 5000 == 0:
if loss1.item() + loss2.item() > 10 * loss3.item():
w_boundary *= 0.5
if loss3.item() > 10 * (loss1.item() + loss2.item()):
w_physics *= 0.5
print(i,w_boundary, w_physics,loss1,loss2,loss3)
u = pinn(t_test).detach()
plt.plot(t_test[:,0], u_exact[:,0], label="Exact solution", color="tab:grey", alpha=0.6)
plt.plot(t_test[:,0], u[:,0], label="PINN solution", color="tab:green")
plt.title(f"Training step {i}")
plt.legend()
plt.savefig('pinnbvpsolution00001.png')
plt.show()
plt.plot(t_test[:,0], u_exact[:,0]-u[:,0], label="Error", color="tab:grey", alpha=0.6)
plt.title(f"Training step {i}")
plt.legend()
plt.savefig('pinnbvperror00001.png')
plt.show()
and the output is like:
enter image description here
the loss are very small but the pictures are not match
i think the problem is gradient explode but don’t know how to solve this problem
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
李业昕 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.