torch.nn.utils.prune.l1_unstructured — PyTorch 2.7 documentation (original) (raw)

torch.nn.utils.prune.l1_unstructured(module, name, amount, importance_scores=None)[source][source]

Prune tensor by removing units with the lowest L1-norm.

Prunes tensor corresponding to parameter called name in moduleby removing the specified amount of (currently unpruned) units with the lowest L1-norm. Modifies module in place (and also return the modified module) by:

  1. adding a named buffer called name+'_mask' corresponding to the binary mask applied to the parameter name by the pruning method.
  2. replacing the parameter name by its pruned version, while the original (unpruned) parameter is stored in a new parameter namedname+'_orig'.

Parameters

Returns

modified (i.e. pruned) version of the input module

Return type

module (nn.Module)

Examples

m = prune.l1_unstructured(nn.Linear(2, 3), 'weight', amount=0.2) m.state_dict().keys() odict_keys(['bias', 'weight_orig', 'weight_mask'])