torch.nn.utils.prune.random_structured#
- torch.nn.utils.prune.random_structured(module, name, amount, dim)[source]#
通过沿着指定的维度随机移除通道来剪枝张量。
通过在指定的
dim
上随机选择指定的amount
的(当前未剪枝的)通道来剪枝module
中名为name
的参数。通过以下方式就地修改模块(并返回修改后的模块):将一个名为
name+'_mask'
的缓冲区添加到剪枝方法所应用的参数name
上。用剪枝后的版本替换参数
name
,同时将原始(未剪枝)参数存储在名为name+'_orig'
的新参数中。
- 参数
- 返回
模块的修改(即剪枝)后的版本
- 返回类型
module (nn.Module)
示例
>>> m = prune.random_structured(nn.Linear(5, 3), "weight", amount=3, dim=1) >>> columns_pruned = int(sum(torch.sum(m.weight, dim=0) == 0)) >>> print(columns_pruned) 3