I am a beginner in PyTorch and I am trying to code one of the equations that is mentioned in a research paper.
This equation will solve by using a trained GAN model. Here the z is the latent space of trained gan and x is the generated image.
In terms of coding, how can I do it? How can I have the z of pretrained gan?
The equation you posted an image of means that you are to find
the values of
z[i] that minimize
MSELoss (x[?], G (z)), while
holding the parameters that define
(I believe that the use of
i as the index on both sides of the
equation is a notational inconsistency – that’s why I used
the index for
x – but please explain if you believe otherwise.)
To use pytorch to do this, you would use pytorch’s autograd and
gradient-descent-optimization machinery to minimize your “loss”
with respect to
To do this you would start with
z as a one-dimensional tensor
[d]), and wrap it as a
z somehow – perhaps to zero or perhaps randomly or perhaps
to an initial guess, if you have one. Let me assume that
G is a
pytorch “model,” that is, some sort of
Module. Go through all of
Parameters, and for each of them set
requires_grad = False.
(This is so that you won’t unnecessarily calculate gradients for the
G.) Instantiate a pytorch optimizer with
z as its
Parameter. Then run an optimization loop who’s iteration is:
loss = torch.nn.MSELoss() (x, G (z))
Each optimization step moves
z in the direction that lowers the
G (z) and
The explanation of this equation is:
We first embed x1, x2 into latent space of G. Specifically, we optimize over the latent space to find vectors that produce inputs close to x1 and x2 in l2-norm.
I don’t think using of i on both sides of equation is notational inconsistency. Can you explain why you think this is inconsistent?
Can you please provide your thoughts on it?