当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python PyTorch random_unstructured用法及代码示例


本文简要介绍python语言中 torch.nn.utils.prune.random_unstructured 的用法。

用法:

torch.nn.utils.prune.random_unstructured(module, name, amount)

参数

  • module(torch.nn.Module) -包含要修剪的张量的模块

  • name(str) -module 中的参数名称,将对其进行修剪。

  • amount(int或者float) -要修剪的参数数量。如果 float ,应介于 0.0 和 1.0 之间,表示要修剪的参数比例。如果 int ,它表示要修剪的参数的绝对数量。

返回

输入模块的修改(即修剪)版本

返回类型

模块(nn.Module)

通过删除随机选择的(当前未修剪的)单元的指定 amount 来修剪与 module 中名为 name 的参数相对应的张量。通过以下方式修改模块(并返回修改后的模块):

  1. 添加一个名为 name+'_mask' 的命名缓冲区,该缓冲区对应于通过修剪方法应用于参数 name 的二进制掩码。

  2. 将参数 name 替换为其修剪版本,而原始(未修剪)参数存储在名为 name+'_orig' 的新参数中。

例子

>>> m = prune.random_unstructured(nn.Linear(2, 3), 'weight', amount=1)
>>> torch.sum(m.weight_mask == 0)
tensor(1)

相关用法


注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.nn.utils.prune.random_unstructured。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。