torch.Tensor.Tensor.logical_not_
Tensor.logical_not_(): thisIn-place element-wise logical NOT.
Inverts all boolean values element-wise. Converts true to false and false to true. Essential for negating boolean conditions and creating inverse masks.
Use Cases:
- Negating boolean masks for inverse filtering
- Implementing NOT logic for complex boolean operations
- Finding complement of a condition
- Unary operation: Operates on a single input tensor
- Boolean tensor: Input must be a boolean tensor
- In-place: Modifies this tensor directly and returns it for chaining
- Invertible: Applying NOT twice returns the original boolean values
Returns
this– This tensor modified in-placeExamples
// Negate a boolean mask
const mask = torch.tensor([true, false, true, false]);
mask.logical_not_(); // [false, true, false, true]
// Find negative values by negating positive check
const x = torch.tensor([1, -2, 3, -4, 5]);
const isPositive = x.gt(0);
const isNegative = isPositive.logical_not(); // [false, true, false, true, false]
// Create inverse mask for filtering outliers
const data = torch.randn(1000);
const outliers = data.abs().gt(3);
const inliers = outliers.logical_not();
const cleanData = data.masked_select(inliers);See Also
- PyTorch tensor.logical_not_()
- logical_and_ - Element-wise logical AND
- logical_or_ - Element-wise logical OR
- logical_xor_ - Element-wise logical XOR