Hi,
I’m very confused when first creating a convolutional network for classification. In input I have an [13,13] tensor but the convnet needs a 4-D input, how comes? and how can I convert my input to fit the convnet?
Folowing is my implementation of the convnet, then an example of the shape of tensors I use in input, then the error I get.
Can anyone help me please?
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
# Convolution 1
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=4, stride=1, padding=3)
self.relu1 = nn.ReLU()
# Max pool 1
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
# Convolution 2
self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2)
self.relu2 = nn.ReLU()
# Max pool 2
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
# Fully connected 1 (readout)
self.fc1 = nn.Linear(32 * 4 * 4, 13 * 13)
def forward(self, x):
# Convolution 1
out = self.cnn1(x)
out = self.relu1(out)
# Max pool 1
out = self.maxpool1(out)
# Convolution 2
out = self.cnn2(out)
out = self.relu2(out)
# Max pool 2
out = self.maxpool2(out)
# Linear function (readout)
out = self.fc1(out)
return out
Example of input:
tensor([[0., 0., 1., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 2., 0., 2., 2., 1., 0., 0., 0., 0., 2., 0.],
[0., 0., 0., 1., 2., 2., 2., 1., 0., 0., 0., 1., 0.],
[0., 1., 0., 2., 1., 1., 2., 1., 1., 1., 0., 0., 2.],
[0., 0., 0., 2., 2., 1., 0., 2., 2., 1., 0., 2., 0.],
[0., 2., 0., 2., 2., 1., 1., 1., 2., 1., 1., 1., 2.],
[0., 0., 0., 1., 2., 0., 0., 1., 2., 2., 0., 0., 0.],
[0., 0., 0., 2., 0., 1., 1., 1., 0., 2., 0., 2., 0.],
[0., 0., 1., 2., 0., 0., 1., 0., 1., 0., 0., 0., 2.],
[0., 1., 2., 2., 0., 1., 2., 0., 1., 2., 0., 0., 0.],
[0., 2., 2., 2., 2., 2., 1., 0., 0., 2., 0., 0., 0.],
[0., 1., 1., 1., 0., 2., 2., 2., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 1., 2., 0., 1., 0., 0., 0., 0.]])
The error I get when applying the convnet to a number of tensors (one by one) like the one above.
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [16, 1, 4, 4], but got input of size [13, 13] instead