Hi everyone, long time TF user here hoping to switch over to PyTorch. I’m trying to mimic a CNN I wrote with Keras and am running into quite a bit of trouble.
First off, I am trying to feed in pre-made numpy arrays (ran into a host of problems trying to make my own dataset class with the arrays as is, so I figured it would be better to just feed them in more directly) using:
my_dataset = TensorDataset(x_train, y_train) training_loader = DataLoader(my_dataset)
The numpy arrays are of the shape (# of samples, # of channels, height, width) - does simply feeding in batch_size=256 as an argument to DataLoader take care of the whole batch size problem for me? I have been unsure about this.
I am unsure how to calculate the input shapes for each layer in my CNN - I’ve read some threads on here about it but they all solve the problem directly and don’t actually explain HOW to do this. I am getting a size mismatch runtime error at my first fully connected layer, which in my case appears after a max pooling layer. How can I go about calculating this?
For reference I will include the error message and my code:
RuntimeError: size mismatch, m1: [1 x 2302720], m2: [768 x 120] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290
class MyModel(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 32, 2, 1) self.conv2 = nn.Conv2d(32, 32, 2, 1) self.conv3 = nn.Conv2d(32, 64, 3, 1) self.conv4 = nn.Conv2d(64, 64, 3, 1) self.fc1 = nn.Linear(896, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 1) def forward(self, x): x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) x = F.max_pool2d(x, 2, 2) x = F.relu(self.conv3(x)) x = F.relu(self.conv4(x)) x = F.max_pool2d(x, 2, 2) x = torch.flatten(x) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.sigmoid(self.fc3(x)) return x
Thank you for any help!!