Hi dear community, I want to post a question about autograd.
Here are my functions:
def gradients(net, inputdata):
inputs_res = inputdata.clone()
outputs_res = net(inputs_res)
gradients = autograd.grad(outputs=outputs_res, inputs=inputs_res,
grad_outputs=torch.ones(outputs_res.size()),
create_graph=True, retain_graph=True, only_inputs=True)[0]
return gradients
def nn_sym4(x0, y0, h, net):
gamma1 = torch.Tensor(np.asarray(1 / (2 - 2**(1/3)))).reshape(-1,1)
gamma2 = torch.Tensor(1 - 2 * gamma1)
x, y = x0, y0
x, y = nn_verlet(x, y, h * gamma1, net)
x, y = nn_verlet(x, y, h * gamma2, net)
x, y = nn_verlet(x, y, h * gamma1, net)
return x, y
def nn_verlet(x0, y0, h, net):
a = time.time()
xy0 = torch.cat((x0, y0), dim = 1)
b = time.time()
print('cat1:',b-a)
retx = xy0[:,0:2].reshape(-1,2) + (gradients(net, xy0)[:,2:4].reshape(-1,2))* h / 2.0
c = time.time()
print('grad1:',c-b)
pv1 = torch.cat((retx, xy0[:,2:4].reshape(-1,2)), dim = 1)
d = time.time()
print('cat2:',d-c)
rety = xy0[:,2:4].reshape(-1,2) - (gradients(net, pv1)[:,0:2].reshape(-1,2)) * h
e = time.time()
print('grad2:',e-d)
pv2 = torch.cat((retx, rety), dim = 1)
f = time.time()
print('cat3:',f-e)
retx = retx + (gradients(net, pv2)[:,2:4].reshape(-1,2)) * h / 2.0
g = time.time()
print('grad3:',g-f)
return retx, rety
def generate_data(neth, pos_t0, vel_t0, batch_size, NT):
pos, vel = autograd.Variable(torch.Tensor(pos_t0.clone()), requires_grad=True),autograd.Variable(torch.Tensor(vel_t0.clone()), requires_grad=True)
for t in range(NT):
pos, vel= nn_sym4(pos, vel, dt, neth)
return pos_g, vel_g
When I run generate_data(), I found it really slow to compute “nn_sym4()”, because as the number of iteration grows, the function gradients() needs more and more time. I guess it is because I use too many autograd()? So the graph is accumulated to bigger and bigger? Or is here anyway to make the process quicker. I tried to release the graph but failed, and make grad as 0 doesn’t work either.
Thanks!