I am new to pytorch, my task is to fix the weight of the network and update the input of the network iteratively for many times.  In the 1st iteration(t = 0), I can get the grad for the input. However for the second run (says t = 1), when I use .backward. I cannot fetch the grad of the input, the grad is None. I’m really apprecaite if anyone here can help. Thank you in advance!
 for t in range(self._opt.temperature):
            syn_energy = self._Des128(sample_seq) # deep network
            print(syn_energy.sum())
            syn_energy.sum().backward(retain_graph=True) # backward the output
            temp = sample_seq + 0.5 * self._opt.sampling_step * self._opt.sampling_step * sample_seq.grad
            sample_seq = sample_seq * (1-mask) + temp * mask
            sample_seq.clamp_(0.0, 1.0) # min , max