How can i sovle pinn-pde in this situation

calm22: <p>import torch.optim<br>
from torch.autograd import Variable</p>
<p>from allen import Net<br>
from torch import nn<br>
import numpy as np</p>
<p>net = Net(5, 20)<br>
optimizer = torch.optim.Adam(net.parameters(), lr=1e-4)<br>
mse_cost_function = torch.nn.MSELoss(reduction=‘mean’)</p>
<p>def allen_cahn(x):<br>
u = net(x)<br>
u_x = torch.autograd.grad(u, x, grad_outputs=torch.ones_like(net(x)), create_graph=True, allow_unused=True)[0]<br>
u_t = torch.autograd.grad(u, x, grad_outputs=torch.ones_like(net(x)), create_graph=True, allow_unused=True)[0]<br>
d_x = u_x[0][:, 1].unsqueeze(-1)<br>
d_t = u_t[0][:, 0].unsqueeze(-1)<br>
u_xx = torch.autograd.grad(d_x, x, grad_outputs=torch.ones_like(d_x),<br>
create_graph=True, allow_unused=True)[0][:, 1].unsqueeze(-1)<br>
d = 0.001 * torch.ones([2000, 1])</p>
<pre><code>f = d_t - d * u_xx - 5 * (u - u**3)
return f
</code></pre>
<p>t_bc = np.zeros((2000, 1))<br>
x_bc = np.random.uniform(low=-1.0, high=1.0, size=(2000, 1))<br>
u_bc = x_bc * x_bc * np.cos(np.pi * x_bc)</p>
<p>x_inr = np.ones((2000, 1))<br>
x_inl = -np.ones((2000, 1))<br>
t_in = np.random.uniform(low=0, high=1.0, size=(2000, 1))<br>
u_in = -np.ones((2000, 1))</p>
<p>iterations = 10**5<br>
for epoch in range(iterations):<br>
optimizer.zero_grad()</p>
<pre><code>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)
</code></pre>
<p>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</p>

soulitzer: <p>Do you have a stack trace?</p>
<p>Also it would be helpful if you put your code in code blocks <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>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.</p>