PyTorch 0.4.0 Windows环境下CNN stride报错问题求助
Hey there! Let's break down the stride-related error you're facing with your CNN code. First, let's look at the most likely causes and how to fix them step by step.
Common Root Causes & Fixes
1. Mismatch Between Feature Map Size and Fully Connected Layer Input
Your current fully connected layer self.dense1 = nn.Linear(32 * 28 * 24, 60) expects a flattened feature map of size 32*28*24, but this depends entirely on the size of your input tensor and how the convolutions modify it.
With your current conv layers (no padding, stride=1, kernel size (1,3)):
- For an input tensor of shape
(batch_size, 1, H, W):- After
conv1, the shape becomes(batch_size, 16, H, W-2)(sinceW - 3 + 1 = W-2; H stays the same because kernel height is 1) - After
conv2, the shape becomes(batch_size, 32, H, W-4)
- After
Your code assumes H=28 and W-4=24 (so original W=28). If your input doesn't match (batch_size,1,28,28), you'll get a dimension mismatch error tied to stride/shape calculations.
Fix Steps:
- First, add print statements in your
forwardmethod to check tensor shapes at each step:def forward(self, input): print("Input shape:", input.size()) x = F.relu(self.conv1(input)) print("After conv1:", x.size()) x = F.relu(self.conv2(x)) print("After conv2:", x.size()) x = x.view(x.size(0), -1) # ... rest of your forward logic - Once you see the actual shape after conv layers, either:
- Adjust your input tensor to match the expected
(batch_size,1,28,28)shape, or - Update the
dense1layer's input dimension to match the flattened size you see in the print output.
- Adjust your input tensor to match the expected
2. Missing Padding Causing Unexpected Dimension Shrinkage
If you want your convolutions to preserve the input width (instead of shrinking it by 2 each time), you can add padding to your conv layers. For a kernel size of (1,3), adding padding=(0,1) will keep the width unchanged:
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(1,3), stride=1, padding=(0,1)) self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(1,3), stride=1, padding=(0,1))
This way, if your input is (batch_size,1,28,28), both conv layers will output (batch_size,16,28,28) and (batch_size,32,28,28) respectively—you'll just need to update dense1 to nn.Linear(32*28*28, 60).
3. Input Tensor Dimension Issues
Make sure your input is a 4D tensor in the format (batch_size, channels, height, width). If you're passing a 3D tensor (e.g., (1,28,28) without a batch dimension), PyTorch will throw an error related to stride/input shape mismatch. Fix this by adding a batch dimension with input.unsqueeze(0) before passing it to the model.
4. Use Adaptive Pooling to Avoid Manual Dimension Calculation
If you want your model to handle varying input sizes without manual shape adjustments, add an adaptive pooling layer after the convolutions to fix the feature map size:
def forward(self, input): x = F.relu(self.conv1(input)) x = F.relu(self.conv2(x)) # Fix feature map to (28,24) regardless of input size x = F.adaptive_avg_pool2d(x, (28, 24)) x = x.view(x.size(0), -1) x = F.relu(self.dense1(x)) x = self.out(x) return x
This ensures the flattened tensor always matches the 32*28*24 input expected by dense1.
内容的提问来源于stack exchange,提问作者Ddj

