【问题标题】:Multiply feature map by a scalar in pytorch将特征图乘以pytorch中的标量
【发布时间】:2020-01-22 04:26:23
【问题描述】:

我有一个二元分类问题,数据集中有图像和变量,我有一个比较图像和变量的想法。

每次通过 conv-layer 时,我都想将权重标量与所有特征图相乘,其中权重标量是从 fc-layer 计算的。

例如,假设batch size为8,则有x1x2两个张量,其中x1的大小为(8,3,224,224),x2的大小为(8,16) .

import torch 
from torch.nn import Module, Sequential
from torch.nn import Conv2d, BatchNorm2d, ReLU, MaxPool2d, Softmax, Linear
import numpy
batch_size = 8
x1 = torch.rand(batch_size*3*224*224).view(batch_size,3,224,224)
x2 = torch.rand(batch_size*16).view(batch_size,16)

我定义了 conv-layer 和 fc-layer 并计算图像和变量的输出。

conv_01 = Conv2d(in_channels=3, out_channels= 9, kernel_size=3, stride=1, padding=1)
linear_02 = Linear(16, 1)
c1 = conv_01(x1) ## torch.Size([8, 9, 224, 224])
c2 = linear_02(x2) ## torch.Size([8, 1])

问题是编写如下合适的代码。

## I want to do like blow
## c1[0,:,:,:] = c1[0,:,:,:] * c2[0,0] # 1st data in the mini-batch
## c1[1,:,:,:] = c1[1,:,:,:] * c2[0,1] # 2nd data in the mini-batch
## c1[2,:,:,:] = c1[2,:,:,:] * c2[0,2]
## c1[3,:,:,:] = c1[3,:,:,:] * c2[0,3]
## c1[4,:,:,:] = c1[4,:,:,:] * c2[0,4]
## c1[5,:,:,:] = c1[5,:,:,:] * c2[0,5]
## c1[6,:,:,:] = c1[6,:,:,:] * c2[0,6]
## c1[7,:,:,:] = c1[7,:,:,:] * c2[0,7]
## output is a (8, 9, 224, 224)
## and do more layer like this operation

我已经看过Multiply feature map by a learnable scalar。 但这仅在批量大小为 1 时支持,但在我的情况下,批量大小大于 1。 在我的情况下,如何为 forward 函数编写合适的代码? 非常感谢。

【问题讨论】:

    标签: pytorch


    【解决方案1】:
    result = c1 * c2.reshape((-1,1,1,1))
    

    您可以使用torch.reshapec2 形状torch.Size([8, 1]) 重塑为torch.Size([8, 1, 1, 1]),这样您就可以使用c1 形状torch.Size([8, 9, 224, 224]) 进行乘法运算

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2018-05-31
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多