具体地说,我有一个298x160x160维度的张量( 298帧中的面),我需要对最后两个维度中的每个4x4元素求和,这样我就可以得到298x40x40张量。
我怎样才能做到这一点呢?
发布于 2020-03-20 21:01:43
可以创建具有单个4x4通道的卷积层,并将其权重设置为1,步幅为4 (also see Conv2D doc):
a = torch.ones((298,160,160))
# add a dimension for the channels. Conv2D expects the input to be : (N,C,H,W)
# where N=number of samples, C=number of channels, H=height, W=width
a = a.unsqueeze(1)
a.shape
Out: torch.Size([298, 1, 160, 160])
with torch.no_grad(): # I assume you don't need to backprop, otherwise remove this check
m = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=4,stride=4,bias=False)
# set the kernel values to 1
m.weight.data = m.weight.data * 0. + 1.
# apply the kernel and squeeze the channel dim out again
res = m(a).squeeze()
res.shape
Out: torch.Size([298, 40, 40])
https://stackoverflow.com/questions/60769227
复制相似问题