0
votes

I am new to neural networks and currently trying to build a CNN with 2 conv layers.

class CNN(nn.Module):
  def __init__(self):
    super(CNN, self).__init__()
    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size = 3, stride = 1, padding = 1), 
    self.maxp1 = nn.MaxPool2d(2),
    self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 16, kernel_size = 3, stride = 1, padding = 1),
    self.fc1 = nn.Linear(16, 64),
    self.fc2 = nn.Linear(64, 10)

  def forward(self, x):
    x = nn.ReLU(self.maxp1(self.conv1(x)))
    x = nn.ReLU(self.maxp2(self.conv1(x)))
    x = x.view(x.size(0), -1)
    x = nn.ReLu(self.fc1(x))
    return self.fc2

What I was trying to do was ConvLayer- ReLu activation - Max Pooling 2x2 - ConvLayer - ReLu activation - Flatten Layer - Fully Connect - ReLu - Fully Connected

However, this gives me TypeError: 'tuple' object is not callable on x = nn.ReLU(self.maxp1(self.conv1(x)))

How can I fix this?

1

1 Answers

1
votes

You may change nn.ReLU to F.relu.

If you want to use nn.ReLU(), you may better to declare it as part of __init__ method, and call it later in forward():

class CNN(nn.Module):
  def __init__(self):
    super(CNN, self).__init__()
    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size = 3, stride = 1, padding = 1), 
    self.maxp1 = nn.MaxPool2d(2),
    self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 16, kernel_size = 3, stride = 1, padding = 1),
    self.fc1 = nn.Linear(16, 64),
    self.fc2 = nn.Linear(64, 10)
    self.relu = nn.ReLU(inplace=True)

  def forward(self, x):
    x = self.relu(self.maxp1(self.conv1(x)))
    x = self.relu(self.maxp2(self.conv1(x)))
    x = x.view(x.size(0), -1)
    x = self.relu(self.fc1(x))
    return self.fc2