torch.nn.utils.prune.random_structured¶
- torch.nn.utils.prune.random_structured(module, name, amount, 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