for j in range(n_rnn_batches):
    print x.size()
    h_t = Variable(torch.zeros(x.size(0), 20))
    c_t = Variable(torch.zeros(x.size(0), 20))
    h_t2 = Variable(torch.zeros(x.size(0), 20))
    c_t2 = Variable(torch.zeros(x.size(0), 20))
    for s in range(n_steps / n_bptt_steps):
        h_t, c_t, h_t2, c_t2 = Variable(h_t.data), Variable(c_t.data), Variable(h_t2.data), Variable(c_t2.data)
        optimizer.zero_grad()
        for s2 in range(n_bptt_steps):
            data, target = next(iter((train_loader)))
            data, target = Variable(data), Variable(target)
            output = optimizee(data, x)
            loss = F.nll_loss(output, target)
            grads = autograd.grad(loss, x, retain_graph=True)[0].view(x.size(0),-1)
            grads.volatile = False
            grads = grads.detach()

            out, h_t, c_t, h_t2, c_t2 = rnn(grads, h_t, c_t, h_t2, c_t2)
            x = x - out
            print x.volatile
        loss.backward()
        optimizer.step()

I’m trying to implement Learning to Learn by Gradient Descent By Gradient Descent 5. But I’m getting the Trying to backward through the graph a second time error in the loss.backward() step, even though I have set retain_graph to True. Can someone please help?