How can i sovle pinn-pde in this situation

import torch.optim
from torch.autograd import Variable

from allen import Net
from torch import nn
import numpy as np

net = Net(5, 20)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-4)
mse_cost_function = torch.nn.MSELoss(reduction=‘mean’)

def allen_cahn(x):
u = net(x)
u_x = torch.autograd.grad(u, x, grad_outputs=torch.ones_like(net(x)), create_graph=True, allow_unused=True)[0]
u_t = torch.autograd.grad(u, x, grad_outputs=torch.ones_like(net(x)), create_graph=True, allow_unused=True)[0]
d_x = u_x[0][:, 1].unsqueeze(-1)
d_t = u_t[0][:, 0].unsqueeze(-1)
u_xx = torch.autograd.grad(d_x, x, grad_outputs=torch.ones_like(d_x),
create_graph=True, allow_unused=True)[0][:, 1].unsqueeze(-1)
d = 0.001 * torch.ones([2000, 1])

f = d_t - d * u_xx - 5 * (u - u**3)
return f

t_bc = np.zeros((2000, 1))
x_bc = np.random.uniform(low=-1.0, high=1.0, size=(2000, 1))
u_bc = x_bc * x_bc * np.cos(np.pi * x_bc)

x_inr = np.ones((2000, 1))
x_inl = -np.ones((2000, 1))
t_in = np.random.uniform(low=0, high=1.0, size=(2000, 1))
u_in = -np.ones((2000, 1))

iterations = 10**5
for epoch in range(iterations):
optimizer.zero_grad()

pt_x_bc = Variable(torch.from_numpy(x_bc).float(), requires_grad=False)
pt_t_bc = Variable(torch.from_numpy(t_bc).float(), requires_grad=False)
pt_u_bc = Variable(torch.from_numpy(u_bc).float(), requires_grad=False)

net_bc_out = net(torch.cat([pt_t_bc, pt_x_bc], 1))
mse_u1 = mse_cost_function(net_bc_out, pt_u_bc)

pt_x_inr = Variable(torch.from_numpy(x_inr).float(), requires_grad=False)
pt_x_inl = Variable(torch.from_numpy(x_inl).float(), requires_grad=False)
pt_t_in = Variable(torch.from_numpy(t_in).float(), requires_grad=False)
pt_u_in = Variable(torch.from_numpy(u_in).float(), requires_grad=False)

net_bc_inr = net(torch.cat([pt_t_in, pt_x_inr], 1))
net_bc_inl = net(torch.cat([pt_t_in, pt_x_inl], 1))
mse_u2r = mse_cost_function(net_bc_inr, pt_u_in)
mse_u2l = mse_cost_function(net_bc_inl, pt_u_in)

x_pde = np.random.uniform(low=-1.0, high=1.0, size=(2000, 1))
t_pde = np.random.uniform(low=0, high=1.0, size=(2000, 1))
all_zeros = np.zeros((2000, 1))
pt_x_pde = Variable(torch.from_numpy(x_pde).float(), requires_grad=True)
pt_t_pde = Variable(torch.from_numpy(t_pde).float(), requires_grad=True)
pt_all_zeros = Variable(torch.from_numpy(all_zeros).float(), requires_grad=False)

f_out = allen_cahn(torch.cat([pt_t_pde, pt_x_pde], 1))
mse_f = mse_cost_function(f_out, pt_all_zeros)

loss = mse_u1+mse_u2r+mse_u2l+mse_f

loss.backward()
optimizer.step()
with torch.autograd.no_grad():
    if epoch % 1000 == 0:
        print(epoch, "Training Loss:", loss.data)

my codes are above, but when i run it, it says ‘NoneType’ object has no attribute ‘size’, but i dont know where the problem in , please help me

Do you have a stack trace?

Also it would be helpful if you put your code in code blocks :slight_smile:

If I were to guess its probably because torch.autograd.grad is returning None, and then you call .size() on that since you do not check if .grad returned a tensor.