torch.nn.Conv2d()参数groups

nn.Conv2d: 可以看出通过groups划分卷积核参数量的变化:

from torch import nn

#这个在 depthwise 中的效果
t = nn.Conv2d(10, 10, 3, 2, 0, bias=False, groups=10)
t.weight.shape
#torch.Size([10, 1, 3, 3])

t = nn.Conv2d(10, 10, 3, 2, 0, bias=False, groups=2)
t.weight.shape
#torch.Size([10, 5, 3, 3])

t = nn.Conv2d(10, 10, 3, 2, 0, bias=False, groups=1)
t.weight.shape
#torch.Size([10, 10, 3, 3])

下面是卷积结果和我们手工计算结果:结果是一样的

import numpy as np
import torch
from torch import nn

input_feature_size = 5

t = nn.Conv2d(input_feature_size, input_feature_size, 3, 2, 0, bias=False, groups=input_feature_size)
print(t.weight.shape)
w0 = t.weight[0, 0]
feature_map = torch.rand([input_feature_size, 3, 3])

ret = t(feature_map)

print("torch 卷积结果:", ret)
my_ret = []
for idx, one_w in enumerate(t.weight):
    my_ret.append(torch.sum(feature_map[idx] * one_w[0]).detach().numpy())
print("手工计算 卷积结果:", my_ret)
print('end!')

下面这个是普通卷积操作,groups=1:

import numpy as np
import torch
from torch import nn

input_feature_size = 5

t = nn.Conv2d(input_feature_size, input_feature_size, 3, 2, 0, bias=False, groups=1)
print(t.weight.shape)
w0 = t.weight[0, 0]
feature_map = torch.ones([input_feature_size, 3, 3])

ret = t(feature_map)

print("torch 卷积结果:", ret)
my_ret = []
for one_w in t.weight:
    sum_ = 0
    for one_fm in feature_map:
        sum_ += torch.sum(one_fm * one_w)
    my_ret.append(sum_)
print("手工计算 卷积结果:", ret)
print('end!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值