Conv2d() received an invalid combination of arguments

I am trying to make the AlexNet from scratch. I am new in pytorch, can someone explain me what went wrong please

#create convolution 1 and 2
class conv_block(nn.Module):
  def __init__(self, in_channels, out_channels, kernel_size, stride, padding):
    super(conv_block, self).__init__()
    self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
    self.relu = nn.ReLU()
    self.pool_max = nn.MaxPool2d(kernel_size=(3,3), stride=(2,2))
    #self.maths = None

  def forward(self, x):
    self.pool_max(self.relu(self.conv(x)))
#create AlexNet
#fiiiiiiiiiix convolution 5

class AlexNet(nn.Module):
  def __init__(self):
    super(AlexNet, self).__init__()
    self.arch = nn.Sequential(
        conv_block(3, 96, kernel_size=(11,11), stride=(4,4), padding=(0,0)),
        conv_block(96, 256, kernel_size=(5,5), stride=(1,1), padding=(0,0)),
        nn.Conv2d(256, 384, kernel_size=(3,3), stride=(1,1), padding=(1,1)), #conv3
        nn.ReLU(),
        nn.Conv2d(384, 384, kernel_size=(3,3), stride=(1,1), padding=(1,1)), #conv4
        nn.ReLU(),
        nn.Conv2d(384, 256, kernel_size=(3,3), stride=(1,1), padding=(1,1)), #conv5
        nn.ReLU(),
        nn.Flatten(0, -1), #flatten for fully connected layer
        nn.Linear(4096, 4096), #fc1
        nn.ReLU(),
        nn.Linear(4096, 4096), #fc2
        nn.ReLU(),
        nn.Linear(4096, 10), #fc3
    )
  
  def forward(self, x):
    return softmax(self.arch(x))
#load CIFAR10
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

batch_size = 4

Here I get the error:

model(images)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
442 _pair(0), self.dilation, self.groups)
443 return F.conv2d(input, weight, bias, self.stride,
→ 444 self.padding, self.dilation, self.groups)
445
446 def forward(self, input: Tensor) → Tensor:

TypeError: conv2d() received an invalid combination of arguments - got (NoneType, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:

  • (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
    didn’t match because some of the arguments have invalid types: (!NoneType!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
  • (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
    didn’t match because some of the arguments have invalid types: (!NoneType!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)

Your conv_block is missing a return statement and will thus return None.

Also, check if the softmax layer in AlexNet is really needed as neither nn.CrossEntropyLoss nor nn.NLLLoss expect probabilities as the model outputs (the former expects raw logits while the latter expects log probabilities).

Thank you , this works now!