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

torch.nn.utils.prune.random_structured(module, name, amount, dim)[source][source]

Prune tensor by removing random channels along the specified dimension.

Prunes tensor corresponding to parameter called name in moduleby removing the specified amount of (currently unpruned) channels along the specified dim selected at random. 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.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