in this given code set for the particular pinn of the second order differential wave equation,with every training i am getting a different solutions instead of a particular solution and my model is not following the ic annd bc,moreover i took the codes from github and tried to incorporate it with mine,
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Check device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net = Net().to(device)
mse_cost_function = nn.MSELoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
Parameters for the wave equation
Nx = 100 # Number of spatial points
Nt = 100 # Number of time points
L = 1.0 # Length of spatial domain
T = 1.0 # Total time
c = 1.0 # Speed of wave propagation
Calculate spatial and time steps
hx = L / (Nx - 1)
ht = T / (Nt - 1)
Generate training data
x = np.linspace(0, L, Nx)
t = np.linspace(0, T, Nt)
x, t = np.meshgrid(x, t)
x = x.reshape(-1, 1)
t = t.reshape(-1, 1)
torch.save(net.state_dict(), "model_wave.pt")
I am unable to find the error.Someone pplease suggest some help as this is very new to me and i am a noob in it.
in this given code set for the particular pinn of the second order differential wave equation,with every training i am getting a different solutions instead of a particular solution and my model is not following the ic annd bc,moreover i took the codes from github and tried to incorporate it with mine, import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
Check device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
Define the neural network
class Net(nn.Module): def init(self): super(Net, self).init() self.hidden_layer1 = nn.Linear(2, 50) # Increased number of neurons self.hidden_layer2 = nn.Linear(50, 50) self.hidden_layer3 = nn.Linear(50, 50) self.hidden_layer4 = nn.Linear(50, 50) self.hidden_layer5 = nn.Linear(50, 50) self.output_layer = nn.Linear(50, 1)
Initialize the neural network
net = Net().to(device) mse_cost_function = nn.MSELoss() optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
Parameters for the wave equation
Nx = 100 # Number of spatial points Nt = 100 # Number of time points L = 1.0 # Length of spatial domain T = 1.0 # Total time c = 1.0 # Speed of wave propagation
Calculate spatial and time steps
hx = L / (Nx - 1) ht = T / (Nt - 1)
Generate training data
x = np.linspace(0, L, Nx) t = np.linspace(0, T, Nt) x, t = np.meshgrid(x, t) x = x.reshape(-1, 1) t = t.reshape(-1, 1)
Initial Conditions
u_init = np.zeros((Nx, Nt)) u_init[10:30, 0] = 0.5 u_init[10:30, 1] = 0.5
Boundary Conditions
def boundary_conditions(t): return np.zeros_like(t)
Collocation Points for PDE Residual
x_collocation = np.random.uniform(low=0.0, high=L, size=(10000, 1)) t_collocation = np.random.uniform(low=0.0, high=T, size=(10000, 1)) all_zeros = np.zeros((10000, 1))
PDE loss function
def f(x, t, net): x.requiresgrad(True) t.requiresgrad(True)
Training parameters
iterations = 5000
Training the PINN
for epoch in range(iterations): optimizer.zero_grad()
Visualization
with torch.no_grad(): x_vis = np.linspace(0, L, Nx) t_vis = np.linspace(0, T, Nt) ms_x, ms_t = np.meshgrid(x_vis, t_vis) x_flat = ms_x.ravel().reshape(-1, 1) t_flat = ms_t.ravel().reshape(-1, 1)
Save Model
torch.save(net.state_dict(), "model_wave.pt") I am unable to find the error.Someone pplease suggest some help as this is very new to me and i am a noob in it.