评价此页

L1Unstructured#

class torch.nn.utils.prune.L1Unstructured(amount)[源代码]#

通过将 L1 范数最小的单元置零来修剪(当前未修剪)张量中的单元。

参数

amount (intfloat) – 要剪枝的参数数量。如果是 float,则应介于 0.0 和 1.0 之间,表示要剪枝的参数的比例。如果是 int,则表示要剪枝的参数的绝对数量。

classmethod apply(module, name, amount, importance_scores=None)[源代码]#

Add pruning on the fly and reparametrization of a tensor.

Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.

参数
  • module (nn.Module) – module containing the tensor to prune

  • name (str) – 在 module 中执行剪枝操作的参数名称。

  • amount (intfloat) – 要剪枝的参数数量。如果是 float,则应介于 0.0 和 1.0 之间,表示要剪枝的参数的比例。如果是 int,则表示要剪枝的参数的绝对数量。

  • importance_scores (torch.Tensor) – 用于计算修剪掩码的重要性分数张量(形状与模块参数相同)。此张量中的值表示要修剪的参数中相应元素的重要性。如果未指定或为 None,则将使用模块参数本身。

apply_mask(module)[源代码]#

Simply handles the multiplication between the parameter being pruned and the generated mask.

Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.

参数

module (nn.Module) – module containing the tensor to prune

返回

pruned version of the input tensor

返回类型

pruned_tensor (torch.Tensor)

prune(t, default_mask=None, importance_scores=None)[源代码]#

Compute and returns a pruned version of input tensor t.

According to the pruning rule specified in compute_mask().

参数
  • t (torch.Tensor) – 要剪枝的张量(维度与 default_mask 相同)。

  • importance_scores (torch.Tensor) – 重要性分数张量(与 t 形状相同),用于计算剪枝 t 的掩码。此张量中的值指示正在剪枝的 t 中相应元素的 গুরুত্ব。如果未指定或为 None,则将使用张量 t 本身。

  • default_mask (torch.Tensor, optional) – 前一个剪枝迭代的掩码(如果有)。在确定剪枝应作用于张量的哪个部分时需要考虑。如果为 None,则默认为一个全为 1 的掩码。

返回

张量 t 的修剪版本。

remove(module)[源代码]#

从模块中移除修剪重参数化。

名为 name 的已剪枝参数将永久保持剪枝状态,名为 name+'_orig' 的参数将从参数列表中移除。类似地,名为 name+'_mask' 的缓冲区也将从缓冲区中移除。

注意

修剪本身**不会**被撤销或恢复!