Why can't my gan's implementation generate images like real ones?

username: caiyuanqin
cooked: <pre><code>    self.meta_optim.zero_grad()
    loss_q.backward(retain_graph=True)
    # optimize the discriminator
    self.meta_optim.step()

    for i in range(tasks_per_batch):
        qry_gen = self.generator(x_qry[i], vars=None, bn_training=True)
        for p in fast_weights[i]:
            p.detach()
        dis_feat = self.discriminator(x_qry[i], fast_weights[i], bn_training=True, feat=True)
        gen_feat = self.discriminator(qry_gen, fast_weights[i], bn_training=True, feat=True)
        fm_loss = torch.mean(torch.abs(torch.mean(gen_feat, 0) - torch.mean(dis_feat, 0)))

        nsample = gen_feat.size(0)
        gen_feat_norm = gen_feat / gen_feat.norm(p=2, dim=1).reshape(-1, 1).expand_as(gen_feat)
        cosine = torch.mm(gen_feat_norm, gen_feat_norm.t())
        mask = Variable((torch.ones(cosine.size()) - torch.diag(torch.ones(nsample))).cuda())
        pt_loss = 0.8 * torch.sum((cosine * mask) ** 2) / (nsample * (nsample - 1))

        loss_gen = fm_loss + pt_loss
        with torch.no_grad():
            corrects["gen_discrim"][0] += fm_loss

    self.gen_optim.zero_grad()
    loss_gen.backward()
    # optimize the generator
    self.gen_optim.step()
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/1/b12751fb87b5f0183dd40af12e26ad4b17004b30.png" alt="images_step6000" data-base62-sha1="phaKQ9QfRHx1bVDqhEhvSzrCpYA" width="640" height="480"></p>
<p>The left is real images, and the right is generated images. Is my backward procedure mistaked? Or maybe other problems like model, loss function, etc.?</p>

username: ptrblck
cooked: <p>I’m not familiar with your code, but some minor issues you should check:</p>
<ul>
<li>
<code>p.detach()</code> won’t do anything, as it’s not the inplace version (<code>p.detach_()</code> would be the inplace method)</li>
<li>
<code>Variables</code> are deprecated since PyTorch <code>0.4</code> so you can use tensors in newer versions</li>
<li>Are you sure you need <code>retain_graph=True</code>? Sometimes this line is added as a workaround for valid errors, which will not fix the actual error.</li>
</ul>

username: caiyuanqin
cooked: <p>Thanks vey much for your reply. I will try to fix some problems you mentioned.<br>
If not using retain_graph=True, the program will error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/a/aabdc3adf8d73b3e9fe6edebfae4002e3d2c3d49.png" data-download-href="https://discuss.pytorch.org/uploads/default/aabdc3adf8d73b3e9fe6edebfae4002e3d2c3d49" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/a/aabdc3adf8d73b3e9fe6edebfae4002e3d2c3d49.png" alt="image" data-base62-sha1="omrJLUGVramlrDiOo7lSY4FHfuh" width="690" height="83" data-dominant-color="322E30"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1205×145 7.34 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>