pytorch 如何执行通道卷积

ryoqjall  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(146)

我有一个形状为(10,3,4,6)的输入Tensor。6是通道尺寸。如果我需要使用Pytorch逐通道执行卷积(1D和2D)(每个通道应该有不同的权重和偏置),我该如何实现它?
请给予我一个示例代码

jmo0nnb3

jmo0nnb31#

深度卷积是你需要的:

import torch
from torch import nn
input = torch.randn(10,3,4,6)
# change the channel dimension as conv in torch need BxCxHxW
input = input.permute(0, 3, 1, 2).contiguous() 
# set the group as the number of channels
conv = nn.Conv2d(6, 6, kernel_size=3, stride=1, padding=1, groups=6)
print(conv(input))

相关问题