torch.nn.utils.prune.random_structured#
- torch.nn.utils.prune.random_structured(module, name, amount, dim)[source]#
透過移除指定維度上的隨機通道來修剪張量。
透過隨機選擇的指定
dim維度,移除module中名為name的引數對應的張量,移除指定的amount量(當前未被修剪的)通道。透過以下方式就地修改模組(並返回修改後的模組):新增一個名為
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