pytorch异常——RuntimeError:Given groups=1, weight of size..., expected of...

文章目录

省流

  • nn.Conv2d 需要的输入张量格式为 (batch_size, channels, height, width),但您的示例输入张量 x 是 (batch_size, height, width, channels)。因此,需要对输入张量进行转置。

  • 注意,TensorFlow使用"NHWC"(批次、高度、宽度、通道)格式,而PyTorch使用"NCHW"(批次、通道、高度、宽度)格式

异常报错

bash 复制代码
RuntimeError: Given groups=1, weight of size [16, 3, 2, 3], 
expected input[8, 65, 66, 3] to have 3 channels, 
but got 65 channels instead

异常截图

异常代码

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3],stride = 1, **kwargs):
    batch_size,H,W,channels = x.shape

    padding = (0,0,
        int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),
        int(filters_size[0]) - 1 , 0,
        0,0)
    x_paded = nn.functional.pad(x, padding)
    print(x_paded.shape)
    conv_layer = nn.Conv2d(in_channels=channels, out_channels=num_filters, 
                           kernel_size=filters_size,
                           stride=stride, **kwargs)
    
    return conv_layer(x_paded)
# Example usage
x = torch.randn(8, 64, 64, 3)  # Example input with batch size 8, height and width 64, and 3 channels
num_filters = 16
output = down_shifted_conv2d(x, num_filters)
print(output.shape)

原因解释

  • 在pytorch中,"nn.Conv2d"需要输入的张量格式为(batch_size,channels,height,width),原图输入的x的格式是(batch_size,height ,weight,channel)所以需要对tensor进行转置。

  • 矩阵交换维度的函数permute,按照编号,将新的顺序填好即可。

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3], stride = 1, **kwargs):
    batch_size, H, W, channels = x.shape
    
    # Transpose the input tensor to (batch_size, channels, height, width)
    x = x.permute(0, 3, 1, 2)
    
    # Padding
    padding = (int((filters_size[1] - 1) / 2), int((filters_size[1] - 1) / 2),
               filters_size[0] - 1, 0)
    
    x_paded = F.pad(x, padding)

修正代码

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3],stride = 1, **kwargs):
    batch_size,H,W,channels = x.shape
    # 按照顺序对4个维度分别进行填充
    padding = (0,0,
        int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),
        int(filters_size[0]) - 1 , 0,
        0,0)
    x_paded = nn.functional.pad(x, padding)
    x_paded = x_paded.permute(0,3,1,2)
    # 进行卷积
    conv_layer = nn.Conv2d(in_channels=channels, out_channels=num_filters, 
                           kernel_size=filters_size,
                           stride=stride, **kwargs)
    
    return conv_layer(x_paded)
# Example usage
x = torch.randn(8, 64, 64, 3)  
num_filters = 16
output = down_shifted_conv2d(x, num_filters)
print(output.shape)

执行结果

相关推荐
Web3&Basketball7 小时前
TorchTPU 迁移实战:3 个 CUDA 卡点
pytorch·性能调优·cuda·tpu·模型迁移·torchtpu
田里的水稻14 小时前
FA_CNN卷积神经网络十二_CIFAR10全流程PyTorch
人工智能·pytorch·深度学习·神经网络·cnn
论文复现现场17 小时前
RTX 3090 云GPU租赁价格对比:8家平台配置、计费方式与环境测试
linux·运维·人工智能·pytorch·深度学习·云计算·3090
liwulin050618 小时前
【PYTORCH】迷你DECODER-ONLY模型
pytorch·python·深度学习
田里的水稻1 天前
FA_CNN卷积神经网络四---线性层及其拉平PyTorch
人工智能·pytorch·python·深度学习·神经网络·cnn
GPU实战笔记2 天前
NVIDIA L40S 云端任务验证:训练、推理与生成工作流怎样先试再租
pytorch·ai生成·gpu租用·nvidia l40s
QN1幻化引擎2 天前
信号场注意力(SFA):把 KV Cache 变成 O(1) 的双通道注意力——0.1B/0.3B 受控 A/B 精度反升 15.5% / 24.3%
pytorch·深度学习·大模型·transformer·注意力机制·推理优化
田里的水稻2 天前
FA_CNN卷积神经网络二---PyTorch
人工智能·pytorch·python·深度学习·cnn
lucas_AI2 天前
Google 和 Meta 把 TPU 变成 PyTorch「原生设备」:TorchTPU 开源,device="cuda" 改一个词就能上 TPU
pytorch
Time_Memory_cici2 天前
在Windows系统安装Pycharm以及PyTorch方法
pytorch·windows·pycharm