【问题标题】:How do I do a convolution in pytorch where the kernel is different for each minibatch?如何在 pytorch 中进行卷积,其中每个小批量的内核都不同?
【发布时间】:2020-06-24 02:01:43
【问题描述】:

让我用一个例子来表达标题:

令 A 为形状为 [16, 15, 128, 128] 的张量(表示 [batchsize, channels, height, width])

令 B 为形状为 [16, 3, 128, 128] 的张量(表示 [batchsize, channels, height, width])

我想输出一个形状为 [16, 5, 128, 128] 的张量(表示[batchsize, channels, height, width])

其中输出的 5 个通道中的 i_th 通道由下式计算 将元素 B 与 A 的 3 个通道的 i_th 切片相乘,然后它们沿通道维度进行求和。

您将如何在 pytorch 中执行该操作?

谢谢!

PD:很难表达我想从手术中得到什么,如果我不清楚,请问我,我会尝试重新解释

【问题讨论】:

    标签: python machine-learning deep-learning neural-network pytorch


    【解决方案1】:

    我认为您正在寻找 torch.repeat_interleave 来帮助您“扩展”B 张量以拥有 15 个通道(3 个输入通道中的 5 组):

    extB = torch.repeat_interleave(B, 3, dim=1)  # extend B to have 15 channels
    m = A * extB  # element wise multiplication of A with the extended version of B
    # using some reshaping and mean we can get the 5 output channels you want
    out = m.view(m.shape[0], 5, 3, *m.shape[2:]).mean(dim=2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 2020-09-23
      • 2019-09-25
      相关资源
      最近更新 更多