代码之家  ›  专栏  ›  技术社区  ›  marcman

如何在PyTorch conv2d函数中使用groups参数

  •  11
  • marcman  · 技术社区  · 8 年前

    torch.nn.functional.conv2d

    在下面的最小工作示例代码中,我得到一个错误:

    import torch
    import torch.nn.functional as F
    
    filters = torch.autograd.Variable(torch.randn(1,1,3,3))
    inputs = torch.autograd.Variable(torch.randn(1,3,10,10))
    out = F.conv2d(inputs, filters, padding=1)
    

    输入[1,3,10,10]有1个通道,但得到了3个通道

    这表明 groups 需要3岁。然而,当我 groups=3 ,我得到一个不同的错误:

    import torch
    import torch.nn.functional as F
    
    filters = torch.autograd.Variable(torch.randn(1,1,3,3))
    inputs = torch.autograd.Variable(torch.randn(1,3,10,10))
    out = F.conv2d(inputs, filters, padding=1, groups=3)
    

    运行时错误:参数4无效:超出范围

    当我在THTensor类中检查代码片段时,它引用了一系列维度检查,但我不知道我哪里出错了。

    这个错误意味着什么?我如何用这个执行我想要的卷积 conv2d 参数

    1 回复  |  直到 8 年前
        1
  •  14
  •   entrophy    8 年前

    out-channel 应该与您的 in-channel

    简而言之,这将奏效

    import torch
    import torch.nn.functional as F
    
    filters = torch.autograd.Variable(torch.randn(3,1,3,3))
    inputs = torch.autograd.Variable(torch.randn(1,3,10,10))
    out = F.conv2d(inputs, filters, padding=1, groups=3)
    

    鉴于,过滤器的尺寸 (2, 1, 3, 3) (1, 1, 3, 3)

    此外,您还可以 输出通道 通道内 . 这适用于希望每个输入通道具有多个卷积滤波器的情况。

    (4, 1, 3, 3) (5, 1, 3, 3) ,将导致 输出通道 尺寸为3。

    推荐文章