RuntimeError: Expected 4-dimensional input for 4-dimensional weight [8, 4, 3, 3], but got 1-dimensional input of size [16] instead

return F.conv2d(input, weight, bias, self.stride,

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [8, 4, 3, 3], but got 1-dimensional input of size [16] instead

class QNetwork(nn.Module):
def init(self, in_channels=4, num_actions=200):
super(QNetwork, self).init()
self.conv1 = nn.Conv2d(in_channels, 8, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(8, 16, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
self.fc4 = nn.Linear(7 * 7 * 64, 512)
self.fc5 = nn.Linear(512, num_actions)

def forward(self, x):
    x = F.relu(self.conv1(x))
    x = F.relu(self.conv2(x))
    x = F.relu(self.conv3(x))
    x = F.relu(self.fc4(x.view(x.size(0), -1)))
    return self.fc5(x)

def select_action(self, x):
    with torch.no_grad():
        fc5 = self.forward(x)
        action_index = torch.argmax(fc5, dim=1)
    return action_index.item()

Hi Anne!

You are passing a 1D tensor of shape [16] into your model as its
input. But the first layer of your model is a Conv2d (with
in_channels = 4).

Your Conv2d layer, and hence your model, requires an input of shape
[nBatch, in_channels = 4, height, width]. (nBatch can be 1,
but it has to be there.)

You would typically be passing into your model a batch of multi-channel
2D images. (Because of your choice of having in_channels = 4 for
conv1, in this specific case you need to pass in a batch of four-channel
images.)

Best.

K. Frank

Thank you a lot for your response. let me correct it according to your guidance. I will give you the feedback