PyTorch convolutional block - CIFAR10 - RuntimeError
I am using PyTorch 1.7 and Python 3.8 with CIFAR-10 dataset. I am trying to create a block with: conv -&gt; conv -&gt; pool -&gt; fc. Fully connected layer (fc) has 256 neurons. The code for this is as follows:
# Testing-
conv1 = nn.Conv2d(
    in_channels = 3, out_channels = 64,
    kernel_size = 3, stride = 1,
    padding = 1, bias = True
    )
conv2 = nn.Conv2d(
    in_channels = 64, out_channels = 64,
    kernel_size = 3, stride = 1,
    padding = 1, bias = True
    )
pool = nn.MaxPool2d(
    kernel_size = 2, stride = 2
    )
fc1 = nn.Linear(
    in_features = 64 * 16 * 16, out_features = 256
    bias = True
)

images.shape
# torch.Size([32, 3, 32, 32])

x = conv1(images)
x.shape
# torch.Size([32, 64, 32, 32])

x = conv2(x)
x.shape
# torch.Size([32, 64, 32, 32])

x = pool(x)
x.shape
# torch.Size([32, 64, 16, 16])

# This line of code gives error-
x = fc1(x)


RuntimeError: mat1 and mat2 shapes cannot be multiplied (32768x16 and
16384x256)

What is going wrong?

You are nearly there! As you will have noticed nn.MaxPool returns a shape (32, 64, 16, 16) which is incompatible with a nn.Linear's input: a 2D dimensional tensor (batch, in_features). You need to broadcast to (batch, 64*16*16).
I would recommend using a nn.Flatten layer rather than broadcasting yourself. It will act as x.view(x.size(0), -1) but is clearer. By default it preserves the first dimension:
conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1)
conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1)
pool = nn.MaxPool2d(kernel_size=2, stride=2)
flatten = nn.Flatten()
fc1 = nn.Linear(in_features=64*16*16, out_features=256)

x = conv1(images)
x = conv2(x)
x = pool(x)
x = flatten(x)
x = fc1(x)

Alternatively, you could use the functional alternative torch.flatten, where you will have to provide the start_dim as 1: x = torch.flatten(x, start_dim=1).

When you're done debugging, you could assemble your layers with nn.Sequential:
model = nn.Sequential(
    nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1),
    nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Flatten(),
    nn.Linear(in_features=64*16*16, out_features=256)
)

x = model(images)

