Expected 4-dimensional input for 4-dimensional weight [6, 1, 5, 5], but got 3-dimensional input of size [1, 28, 28] instead
I am trying to make a neural network that is complex enough to fit the data ( I am using MNIST dataset) I had a small network I tried to make a new one now and I have stumbled upon this problem. the code is:
class NN1(nn.Module):

    def __init__(self):
        super(NN1, self).__init__()
       
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        
        self.fc1 = nn.Linear(16 * 5 * 5, 120)  # 5*5 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

transform_list = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.0], std=[1.0,]) ] )

mnist_trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform_list)

mnist_trainset_small =  [ mnist_trainset[i] for i in range(0,4000) ] 

mnist_testset = datasets.MNIST(root='./data', train=False, download=True, transform=transform_list)

nn1 = NN1()

tmp = nn1.forward( mnist_trainset[0][0])
tmp

how can I fix this with building a good network

You should use a DataLoader on top of your Dataset:
mnist_train_dl = torch.utils.data.DataLoader(mnist_trainset, batch_size=16)

Predefined Pytorch modules work with batch-first tensors. In your case your model expects a tensor of shape (batch_size, channels=1, height, width).
You shouldn't call forward, instead call your module directly nn1(x).
Usually, you would loop through your data loader and infer/back-propagate/update for each batch. Something like:
for x, y in mnist_train_dl:
    out = nn1(x)
    # ...

However, you can debug your model by inferring one element, by accessing the first element of the first batch:
x, y = next(mnist_train_dl)
out = nn1(x[:1]) # target is y[:1]

indexing by [:1] instead of [0] makes it so you don't squeeze the first axis.
