torch.Tensor.Tensor.logical_xor_
In-place element-wise logical XOR.
Computes the logical XOR (exclusive OR) between two boolean tensors element-wise. Returns true when inputs differ (one true, one false). Useful for toggle operations and finding differences between boolean masks.
Use Cases:
- Toggle between boolean states based on another condition
- Finding symmetric difference of two boolean masks
- Detecting changes or mismatches between conditions
- Broadcasting: Both tensors are broadcast to compatible shapes
- Boolean tensors: Both inputs must be boolean tensors
- In-place: Modifies this tensor directly and returns it for chaining
- Symmetry: XOR is symmetric: a XOR b = b XOR a
- Self-canceling: a XOR a = false always
Parameters
otherTensor- The other tensor (will be broadcast if needed)
Returns
this– This tensor modified in-placeExamples
// Find differing values between two boolean masks
const mask1 = torch.tensor([true, true, false, true]);
const mask2 = torch.tensor([true, false, false, true]);
mask1.logical_xor_(mask2); // [false, true, false, false]
// Symmetric difference - values in either range but not both
const x = torch.tensor([0, 2, 7, 3, 9]);
const inFirst = x.gt(0).logical_and(x.lt(4)); // [2, 3]
const inSecond = x.gt(5).logical_and(x.lt(10)); // [7, 9]
const symmetric = inFirst.logical_xor(inSecond); // [true for 2, 3, 7, 9]
// Toggle condition based on another mask
const predictions = torch.tensor([true, true, false, true]);
const hasError = torch.tensor([false, true, false, false]);
predictions.logical_xor_(hasError); // Flip where there are errorsSee Also
- PyTorch tensor.logical_xor_()
- logical_and_ - Element-wise logical AND
- logical_or_ - Element-wise logical OR
- logical_not_ - Element-wise logical NOT