torch.Tensor.Tensor.count_nonzero
Tensor.count_nonzero(): Tensor<readonly [], D, Dev>Tensor.count_nonzero<Dim extends number>(dim: Dim, options?: { dim?: number }): Tensor<DynamicShape, D, Dev>Counts the number of non-zero elements in a tensor.
Returns the count of elements that are not equal to zero. Can count across all elements or along a specific dimension. Useful for:
- Measuring sparsity (how many zeros vs non-zeros)
- Monitoring network activation density
- Data quality checks (expected number of non-zero features)
- Sparse tensor analysis
- Counting active neurons/features
- Count includes any value != 0, including NaN and Infinity
- For boolean tensors, counts True values (True = 1, False = 0)
Returns
Tensor<readonly [], D, Dev>– If dim undefined: scalar with total count. If dim specified: tensor with counts per sliceExamples
// Count all non-zeros
const x = torch.tensor([0, 1, 2, 0, 3, 0]);
x.count_nonzero(); // 3 (three non-zero elements)
// Count along dimension
const matrix = torch.tensor([[0, 1, 0], [1, 0, 1], [0, 0, 1]]);
matrix.count_nonzero(0); // [1, 1, 2] - count per column
matrix.count_nonzero(1); // [1, 2, 1] - count per row
// Monitor neuron activation
const activations = torch.randn(32, 256); // 32 samples, 256 neurons
const active = activations.count_nonzero(1); // Neurons activated per sample
const sparsity = 256 - active.mean(); // Average inactive neuronsSee Also
- PyTorch torch.count_nonzero()
- sum - Sum values instead of counting
- any - Test if any element is non-zero