python - 如何在 pytorch 中进行卷积,其中每个小批量的内核都不同?
问题描述
让我用一个例子来表达标题:
令 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: 很难表达我想从手术中得到什么,如果我不清楚,请问我,我会尝试重新解释
解决方案
我认为您正在寻找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)
推荐阅读
- html - 使用 conic-gradient() 制作的 Square 没有出现在 Firefox 中,但在 Chrome 中运行良好。解决方法是什么?
- xcode - 我可以强制 Xcode 通过项目而不是文件调用任务控制器中的窗口吗?
- javascript - 可以使用 ES2016 代理来捕获函数的构造,但不能捕获类
- java - 不在文本视图Android中设置文本
- reactjs - 如何在 reactstrap 列表项上显示来自 restapi 的值列表
- python - 以不同格式解析 PYTHON 中的 XML 文件
- assembly - 从内存移动到寄存器。为什么我需要内存中操作数的显式前缀?
- c# - 如何在 C#.NET 中创建持久的 AesCng 密钥或 TripleDesCng 密钥?
- java - 尽管使用了我被明确告知要使用的代码,但它还是向我抛出了一个错误
- amazon-web-services - 有没有办法在 AWS CDK 中实现 SES 发送授权?