torch.Tensor.Tensor.not_equal
Tensor.not_equal(other: number): Tensor<S, 'bool', Dev>Tensor.not_equal<O extends Shape>(other: Tensor<O>): Tensor<DynamicShape, 'bool', Dev>Alias for ne() - element-wise inequality (not-equal) comparison.
Returns a boolean tensor indicating which elements are not equal to the comparison value. Useful for:
- Finding non-zero elements
- Detecting changes or differences
- Filtering outliers
- Excluding specific values
- Finding non-matching elements
- Comparison is element-wise; broadcasting applies if shapes differ
- Result is always a boolean tensor, not 0/1
- For NaN: NaN.not_equal(NaN) returns true (NaN ≠ NaN mathematically)
- Inverse of equal(): not_equal(x) = !equal(x)
Parameters
othernumber- Scalar or tensor to compare with (broadcasts to match shape)
Returns
Tensor<S, 'bool', Dev>– Boolean tensor where true indicates self[i] != other[i]Examples
// Compare with scalar
const x = torch.tensor([1, 2, 1, 3]);
const result = x.not_equal(1); // [false, true, false, true]
// Compare two tensors
const a = torch.tensor([1, 2, 3]);
const b = torch.tensor([1, 5, 3]);
a.not_equal(b); // [false, true, false]
// Find changed values after update
const before = torch.tensor([10, 20, 30]);
const after = torch.tensor([10, 25, 30]);
const changed = before.not_equal(after); // [false, true, false]
const change_count = changed.sum(); // Count changes
// Exclude specific values - remove padding tokens
const tokens = torch.tensor([101, 102, 0, 103, 0, 104]);
const padding = 0;
const non_padding = tokens.not_equal(padding); // [true, true, false, true, false, true]See Also
- PyTorch torch.not_equal()
- ne - Canonical name for this operation
- equal - Equality comparison (inverse)
- masked_select - Select elements using boolean mask