How to input the image with the size 4x4 and replace the Fully Connected Layers using the Convolution Layers?
I'm a beginner at the PyTorch library, and I got stuck in an exercise.
The code below works for the input image with size 2x2. I'm trying to do the same thing as below but the input image with size 4x4.
The code:
import torch 

Assume that we have a 2x2 input image
inputs = torch.tensor([[[[1., 2.],
                         [3., 4.]]]])
inputs.shape

Output: torch.Size([1,1,2,2]

A fully connected layer, which maps the 4 input features two 2 outputs, would be computed as follows:
fc = torch.nn.Linear(4, 2)

weights = torch.tensor([[1.1, 1.2, 1.3, 1.4],
                        [1.5, 1.6, 1.7, 1.8]])
bias = torch.tensor([1.9, 2.0])
fc.weight.data = weights
fc.bias.data = bias
torch.relu(fc(inputs.view(-1, 4)))

Output: torch.Size([2, 1, 2, 2])
Output: torch.Size([2])

Obtain the same outputs if we use convolutional layers where the kernel size is the same size as the input feature array:
conv = torch.nn.Conv2d(in_channels=1,
                       out_channels=2,
                       kernel_size=inputs.squeeze(dim=(0)).squeeze(dim=(0)).size())
print(conv.weight.size())
print(conv.bias.size())

Output: torch.Size([2, 1, 2, 2])
Output: torch.Size([2])
conv.weight.data = weights.view(2, 1, 2, 2)
conv.bias.data = bias
torch.relu(conv(inputs))

Output: tensor([[[[14.9000]],

         [[19.0000]]]], grad_fn=&lt;ReluBackward0&gt;)

Replace the fully connected layer using a convolutional layer when we reshape the input image into a num_inputs x 1 x 1 image:
conv = torch.nn.Conv2d(in_channels=4,
                       out_channels=2,
                       kernel_size=(1, 1))

conv.weight.data = weights.view(2, 4, 1, 1)
conv.bias.data = bias
torch.relu(conv(inputs.view(1, 4, 1, 1)))

Output: tensor([[[[14.9000]],

         [[19.0000]]]], grad_fn=&lt;ReluBackward0&gt;)

So based on this code how to input an image that has a size 4x4 and replace the Fully Connected Layers using Convolution Layers?

You simply need to change the shape of input and reshape weights as per 4x4.
inputs = torch.randn(1, 1, 4, 4)
fc = torch.nn.Linear(16, 2)
torch.relu(fc(inputs.view(-1, 16)))

# output
tensor([[0.0000, 0.2525]], grad_fn=&lt;ReluBackward0&gt;)

Now, for conv layer
conv = torch.nn.Conv2d(in_channels=1,
                       out_channels=2,
                       kernel_size=inputs.squeeze(dim=(0)).squeeze(dim=(0)).size())
conv.weight.data = fc.weight.data.view(2, 1, 4, 4)
conv.bias.data = fc.bias.data
torch.relu(conv(inputs))

# output
tensor([[[[0.0000]],
         [[0.2525]]]], grad_fn=&lt;ReluBackward0&gt;)

You can read Converting FC layers to CONV layers if not sure how conv layers params are taken.
