Oct-22-2020, 11:11 PM
I am taking a tutorial course in Generative Adversarial Networks with Python. I have been following along with the teacher. I have been executing the code in chunks to make sure that everything is working. I have everything working so far in this block of code with the exception of the very last line. I get the following error message:
TypeError: __init__() takes 1 positional argument but 2 were given
I posted a question for the teacher, but he can take a long time to get around to answering questions. Here is the rest of my code. It is the very last line that causes the error.
import torch
import numpy as np
from matplotlib import pyplot as plt
device = torch.device('cpu')
noise_dim = 100
class Generator(torch.nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.fcn = torch.nn.Sequential(
torch.nn.Linear(
in_features=noise_dim,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.Sigmoid()
)
def forward(self, batch):
ret=batch.view(batch.size(0), -1)
ret=self.fcn(ret)
return ret
class Maxout(torch.nn.Module):
def __(self, num_pieces):
super(Maxout, self).__init__()
self.num_pieces
def forward(self, x):
assert x.shape[1] % self.num_pieces == 0
ret = x.view(
*x.shape[:1],
x.shape[1] // self.num_pieces,
self.num_pieces,
*x.shape[2:]
)
ret, _ = ret.max(dim=2)
return ret
class Discriminator(torch.nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.fcn = torch.nn.Sequential(
torch.nn.Linear(
in_features=784,
out_features=625,
bias=True
),
Maxout(5),
torch.nn.Linear(
in_features=125,
out_features=625,
bias=True
),
Maxout(5),
torch.nn.Linear(
in_features=125,
out_features=1,
bias=True
),
torch.nn.Sigmoid()
)
def forward(self, batch):
ret = batch.view(batch.size(0), -1)
ret = self.fcn(ret)
return ret
import torchvision
class FlattenTransform:
def __call__(self, inputs):
return inputs.view(inputs.shape[0], -1)
data_train = torchvision.datasets.MNIST(
'./data/mnist',
train=True,
download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
FlattenTransform()
])
)
BATCH_SIZE = 32
train_loader = torch.utils.data.DataLoader(
data_train,
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=4
)
generator = Generator().to(device)
discriminator = Discriminator().to(device)
discriminator_optimizer = torch.optim.SGD(
discriminator.parameters(),
lr=0.0002,
momentum=0.5
)
TypeError: __init__() takes 1 positional argument but 2 were given
I posted a question for the teacher, but he can take a long time to get around to answering questions. Here is the rest of my code. It is the very last line that causes the error.
import torch
import numpy as np
from matplotlib import pyplot as plt
device = torch.device('cpu')
noise_dim = 100
class Generator(torch.nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.fcn = torch.nn.Sequential(
torch.nn.Linear(
in_features=noise_dim,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(
in_features=1200,
out_features=1200,
bias=True
),
torch.nn.Sigmoid()
)
def forward(self, batch):
ret=batch.view(batch.size(0), -1)
ret=self.fcn(ret)
return ret
class Maxout(torch.nn.Module):
def __(self, num_pieces):
super(Maxout, self).__init__()
self.num_pieces
def forward(self, x):
assert x.shape[1] % self.num_pieces == 0
ret = x.view(
*x.shape[:1],
x.shape[1] // self.num_pieces,
self.num_pieces,
*x.shape[2:]
)
ret, _ = ret.max(dim=2)
return ret
class Discriminator(torch.nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.fcn = torch.nn.Sequential(
torch.nn.Linear(
in_features=784,
out_features=625,
bias=True
),
Maxout(5),
torch.nn.Linear(
in_features=125,
out_features=625,
bias=True
),
Maxout(5),
torch.nn.Linear(
in_features=125,
out_features=1,
bias=True
),
torch.nn.Sigmoid()
)
def forward(self, batch):
ret = batch.view(batch.size(0), -1)
ret = self.fcn(ret)
return ret
import torchvision
class FlattenTransform:
def __call__(self, inputs):
return inputs.view(inputs.shape[0], -1)
data_train = torchvision.datasets.MNIST(
'./data/mnist',
train=True,
download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
FlattenTransform()
])
)
BATCH_SIZE = 32
train_loader = torch.utils.data.DataLoader(
data_train,
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=4
)
generator = Generator().to(device)
discriminator = Discriminator().to(device)
discriminator_optimizer = torch.optim.SGD(
discriminator.parameters(),
lr=0.0002,
momentum=0.5
)