C1w3_wgan_gp

While running the following block in[18]:
gen = Generator(z_dim).to(device)
gen_opt = torch.optim.Adam(gen.parameters(), lr=lr, betas=(beta_1, beta_2))
crit = Critic().to(device)
crit_opt = torch.optim.Adam(crit.parameters(), lr=lr, betas=(beta_1, beta_2))

def weights_init(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
torch.nn.init.normal_(m.weight, 0.0, 0.02)
if isinstance(m, nn.BatchNorm2d):
torch.nn.init.normal_(m.weight, 0.0, 0.02)
torch.nn.init.constant_(m.bias, 0)
gen = gen.apply(weights_init)
crit = crit.apply(weights_init)

I get this error:

TypeError Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 gen = Generator(z_dim).to(device)
2 gen_opt = torch.optim.Adam(gen.parameters(), lr=lr, betas=(beta_1, beta_2))
3 crit = Critic().to(device)

Input In [14], in Generator.init(self, z_dim, im_chan, hidden_dim)
12 self.z_dim = z_dim
13 # Build the neural network
14 self.gen = nn.Sequential(
15 self.make_gen_block(z_dim, hidden_dim * 4),
16 self.make_gen_block(hidden_dim * 4, hidden_dim * 2, kernel_size=4, stride=1),
17 self.make_gen_block(hidden_dim * 2, hidden_dim),
—> 18 self.make_gen_block(hidden_dim, im_chan, kernel_size=4, final_layer=True),
19 )

Input In [14], in Generator.make_gen_block(self, input_channels, output_channels, kernel_size, stride, final_layer)
34 return nn.Sequential(
35 nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
36 nn.BatchNorm2d(output_channels),
37 nn.ReLU(inplace=True),
38 )
39 else:
—> 40 return nn.Sequential(
41 nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
42 nn.Tanh(),311
43 )

File /usr/local/lib/python3.8/dist-packages/torch/nn/modules/container.py:91, in Sequential.init(self, *args)
89 else:
90 for idx, module in enumerate(args):
—> 91 self.add_module(str(idx), module)

File /usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py:444, in Module.add_module(self, name, module)
434 r"““Adds a child module to the current module.
435
436 The module can be accessed as an attribute using the given name.
(…)
441 module (Module): child module to be added to the module.
442 “””
443 if not isinstance(module, Module) and module is not None:
→ 444 raise TypeError(”{} is not a Module subclass".format(
445 torch.typename(module)))
446 elif not isinstance(name, torch._six.string_classes):
447 raise TypeError(“module name should be a string. Got {}”.format(
448 torch.typename(name)))

TypeError: int is not a Module subclass

I think all the code you are executing there was just given to you. Are you sure you didn’t accidentally modify some of the syntax in the “Generator” block?

It might be worth getting a clean copy of the notebook and then just carefully copying over your code from the “YOUR CODE HERE” sections.

Thank you for your help. It seems like indeed I inserted something accediently.

1 Like