PruningMask<T>
Models & Types · AiDotNet.Pruning
Binary mask for pruning neural network weights.
Implements: IPruningMask<T>
For Beginners
A pruning mask is like a stencil for your neural network weights.
Imagine you have a grid of numbers (your neural network weights):
- Some numbers are important and should stay
- Some numbers can be removed to make the model smaller
The pruning mask marks which ones to keep (1) and which to remove (0). When you apply the mask, all the marked weights become zero, effectively removing those connections from your neural network.
This helps create smaller, faster models that still work well!
How It Works
PruningMask represents a binary matrix where 1 indicates weights to keep and 0 indicates weights to prune (set to zero). It provides methods to apply the mask to weight matrices and tensors, compute sparsity levels, and combine multiple masks.
Constructors
| Constructor | Summary |
|---|---|
PruningMask(Matrix<T> maskMatrix) |
Initializes a pruning mask from a 2D boolean array (for matrices). |
PruningMask(bool[] keepIndices) |
Initializes a pruning mask from a 2D boolean array (for matrices). |
PruningMask(bool[] keepIndices) |
Initializes a pruning mask from a 2D boolean array (for matrices). |
PruningMask(int rows, int cols) |
Initializes a new pruning mask with all ones (no pruning). |
Properties
| Property | Summary |
|---|---|
SparsityPattern Pattern { get; } |
Gets the sparsity pattern type (unstructured for this implementation). |
int[] Shape { get; } |
Gets the shape of the mask as [rows, columns]. |
Methods
| Method | Summary |
|---|---|
IPruningMask<T> CombineWith(IPruningMask<T> otherMask) |
Combines two masks using logical AND operation. |
Matrix<T> Apply(Matrix<T> weights) |
Applies the mask to a weight vector. |
T[] GetMaskData() |
Gets the raw mask data as a flat array. |
Tensor<T> Apply(Tensor<T> weights) |
Applies the mask to a weight vector. |
Vector<T> Apply(Vector<T> weights) |
Applies the mask to a weight vector. |
double GetSparsity() |
Calculates the sparsity level of the mask. |
int[] GetKeptIndices() |
Gets indices of non-zero (kept) elements. |
int[] GetPrunedIndices() |
Gets indices of zero (pruned) elements. |
void UpdateMask(Array keepIndices) |
Updates the mask from an Array (supports both 1D and 2D arrays). |
void UpdateMask(bool[] keepIndices) |
Updates the mask from an Array (supports both 1D and 2D arrays). |
void UpdateMask(bool[] keepIndices) |
Updates the mask from an Array (supports both 1D and 2D arrays). |