【问题标题】:How to use groups parameter in PyTorch conv2d function with batch?如何在 PyTorch conv2d 函数中批量使用组参数?
【发布时间】:2021-07-05 01:44:26
【问题描述】:

关注How to use groups parameter in PyTorch conv2d function中的问题

我可以知道输入批次大小是否= 4,对于每个批次它都有独立的过滤器来与之转换,我将代码修改如下,

import torch
import torch.nn.functional as F

filters = torch.autograd.Variable(torch.randn(3,4,3,3))
inputs = torch.autograd.Variable(torch.randn(4,3,10,10))
out = F.conv2d(inputs, filters, padding=1, groups=3)

我还有一个错误 RuntimeError: 给定组=3,大小为 [3, 4, 3, 3] 的权重,预期输入 [4, 3, 10, 10] 有 12 个通道,但有 3 个通道 如何解决?

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    当您有 shape (3,4,3,3) 的过滤器时,预计通道数为 12

    这应该可以工作

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

    【讨论】:

    • 假设我们有一个大小为 [4, 3, 10, 10] 的特征图 X 和大小为 [3, 4, 1, 1] 的单个内核。在进行香草卷积时,我们得到了大小为 [4, 1, 10, 10] 的特征图,而我正在寻找一种获得大小为 [4, 3, 10, 10] 的特征图的方法。我想使用 conv2d 进行元素乘法。
    • @IvanLeung 请在提问pytorch.org/docs/stable/nn.functional.html#conv2d 之前检查有关维度含义的文档,此外,对于元素乘法,您可能只需要简单的 * 或 torch.mul。请注意广播规则。 pytorch.org/docs/stable/notes/broadcasting.html
    • 感谢您的回复。我知道 elementwise mul 可以通过 *.但是,snpe 平台不支持 * 功能。因此,我需要这些技巧来让它发挥作用。
    猜你喜欢
    • 2018-03-14
    • 2019-09-23
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2016-01-07
    • 2017-05-20
    相关资源
    最近更新 更多