Issue
Hi I'm looking this GAN implementation code. code here
My question is generator class has no input parameter when defining class Generator(#38 from the link) But when training, generator gets input z(#141 from the link). I looked into the nn.Module class which is parent of class Generator but I can't find input parameter for noise z. Can anyone help?
class Generator(nn.Module): #38
def __init__(self):
super(Generator, self).__init__()
def block(in_feat, out_feat, normalize=True):
layers = [nn.Linear(in_feat, out_feat)]
if normalize:
layers.append(nn.BatchNorm1d(out_feat, 0.8))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*block(opt.latent_dim, 128, normalize=False),
*block(128, 256),
*block(256, 512),
*block(512, 1024),
nn.Linear(1024, int(np.prod(img_shape))),
nn.Tanh()
)
def forward(self, z):
img = self.model(z)
img = img.view(img.size(0), *img_shape)
return img
generator = Generator() #88
gen_imgs = generator(z) #141
I tried looking for nn.Module, variable() in pytorch docs and still can't get what I wanted.
Solution
Consider every quoted line (38, 88 and 141):
- On line 38 is a definition of class, by putting
nn.Module
in brackets it's declaring inheritance ofclass Generator
from class nn.Module (which is common way to define your own neural network). - On line 88 instance of class Generator is created -- for parameters it needs all what's inside brackets of
__init__
(line 39), besidesself
, that's why brackets on line 88 is empty. - And on line 141 there is a calling of
genearator
, behavior here defined by methodforward
(line 58) and there is one parameter to be passed --z
.
Again, line 88 creates an instance, line 141 calls forward
method of an instance.
Answered By - draw
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.