Hi all,
I have trained a Generator named netG (which is fixed in the following code). Given x, then I tried use adam optimizer to optimize the following function to get the z:
class loss(nn.Module):
    def __init__(self, nz):
        super().__init__()
        self.z = nn.Parameter(torch.zeros(1, 10, 1, 1),requires_grad=True)
        self.criterion = nn.BCELoss()
    def forward(self,x,netG, netD):
        l_rec = torch.norm(x-netG(self.z))
        print('L_rec:',l_rec.detach().numpy())
        return l_rec

The optimization process is:
#Number of repetitions
epoch = 1000
loss_epoch = np.zeros(epoch)
for i in range(epoch):
    #Gradient initialization used in optimizer
    opt.zero_grad()
    lss = model(x, netG, netD)
    loss_epoch[i] = lss.item()
    #Gradient setting
    lss.backward()
    #Perform optimization
    opt.step()

But the result shows my z doesn’t update (loss not decreasing), the loss is showing in the following figure

Capture976×541 3.83 KB

Can anyone help me?
Thanks!