PyTorch加载旧模型报错:如何将[1,x]张量转为[x]张量
Got it, let's work through this issue clearly. The core problem here is a breaking change between PyTorch 0.3.1 and 0.4.0: older versions stored BatchNorm layer parameters (like bias and weight) with an extra singleton dimension (e.g., [1, 64]), while 0.4.0 expects these to be 1D tensors (e.g., [64]).
Solution 1: Fix the Specific Problematic Parameter
You don’t need transpose or the unavailable torch.flatten—instead, use squeeze() which exists in 0.4.0 and is purpose-built to remove singleton dimensions. Here’s how to fix that bias tensor:
# Load your checkpoint first checkpoint = torch.load('your_model_checkpoint.pth') # Remove the extra singleton dimension from the bias checkpoint['conv1_7x7_s2_bn.bias'] = checkpoint['conv1_7x7_s2_bn.bias'].squeeze(dim=0) # Verify the dimension now matches print(checkpoint['conv1_7x7_s2_bn.bias'].size()) # Should output torch.Size([64])
Specifying dim=0 ensures we only target the first dimension (the 1 in [1,64]), so we don’t accidentally modify other tensors.
Solution 2: Batch Fix for All Similar Parameters
If you have other BatchNorm layers with the same dimension issue, loop through all checkpoint parameters to fix them automatically:
checkpoint = torch.load('your_model_checkpoint.pth') for key in list(checkpoint.keys()): # Target BatchNorm bias and weight parameters if 'bn.bias' in key or 'bn.weight' in key: param = checkpoint[key] # Check if it's a 2D tensor with a singleton first dimension if param.dim() == 2 and param.size(0) == 1: checkpoint[key] = param.squeeze(dim=0)
Alternative: Use view(-1)
Another reliable 0.4.0-compatible method is view(-1), which automatically flattens the tensor into a 1D shape, no matter how many singleton dimensions exist:
checkpoint['conv1_7x7_s2_bn.bias'] = checkpoint['conv1_7x7_s2_bn.bias'].view(-1)
view(-1) tells PyTorch to infer the correct dimension size—for [1,64], it resolves directly to 64.
Why Your Previous Attempts Failed
transposeswaps dimensions, turning[1,64]into[64,1]—still a 2D tensor, which doesn’t match the expected 1D shape.torch.flattenwasn’t added until PyTorch 1.0+, so it’s unavailable in 0.4.0—stick tosqueezeorviewinstead.
Once you’ve adjusted the checkpoint parameters, load the model normally with model.load_state_dict(checkpoint) and the dimension mismatch error should disappear.
内容的提问来源于stack exchange,提问作者SomethingSomething

