torch.logical_xor
function logical_xor<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Computes the element-wise logical XOR (exclusive OR).
Returns true where EXACTLY ONE of input or other is non-zero (true), but not both. Useful for:
- Symmetric difference: elements in one mask but not both
- Anomaly detection: find elements that differ between versions
- Change detection: identify which values changed between iterations
- Alternating logic: toggle/flip patterns
- Parity checking: verify odd number of conditions true
- State transitions: mark elements that changed state
Broadcasting: Automatically broadcasts shapes. [3] XOR [1] broadcasts to [3].
- Exclusive: both true = false: Unlike OR which gives true
- Symmetric difference: Elements in exactly one input
- Self-XOR = false: logical_xor(x, x) always gives false
- Parity: Can chain for odd-number-of-trths logic
- Boolean output: Always returns boolean dtype
- Not the same as OR: Both true gives false in XOR (different from OR)
- Broadcasting: Verify expected shapes - [3] XOR [1] broadcasts to [3]
- Type coercion: Non-zero treated as true in numerical inputs
Parameters
inputTensor<S1>- First tensor (any dtype and shape)
otherTensor<S2>- Second tensor (must be broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Boolean tensor with shape = broadcast(input.shape, other.shape)Examples
// XOR: true where inputs differ (exactly one is true)
const a = torch.tensor([true, true, false, false]);
const b = torch.tensor([true, false, true, false]);
torch.logical_xor(a, b); // [false, true, true, false]// Symmetric difference: elements in one set but not both
const in_set_a = torch.tensor([true, true, false, false, true]);
const in_set_b = torch.tensor([true, false, true, false, false]);
const in_either_not_both = torch.logical_xor(in_set_a, in_set_b);
// [false, true, true, false, true] - elements unique to each set// Change detection: find pixels that changed
const previous_frame = torch.randn(64, 64);
const current_frame = torch.randn(64, 64);
const was_active_prev = torch.gt(previous_frame, 0.5);
const is_active_now = torch.gt(current_frame, 0.5);
const changed = torch.logical_xor(was_active_prev, is_active_now);
// [true] where activation status changed, [false] where stayed same// Parity checking: odd number of conditions true?
const cond1 = torch.tensor([true, true, false, false]);
const cond2 = torch.tensor([true, false, true, false]);
const cond3 = torch.tensor([true, true, true, false]);
const xor12 = torch.logical_xor(cond1, cond2);
const parity = torch.logical_xor(xor12, cond3);
// [true, false, false, false] - odd number of trues gives true// Toggle/flip logic: invert where condition is true
const state = torch.tensor([true, true, false, false]);
const toggle_mask = torch.tensor([false, true, true, false]);
const new_state = torch.logical_xor(state, toggle_mask);
// [true, false, true, false] - flipped where toggle_mask is trueSee Also
- PyTorch torch.logical_xor(input, other)
- logical_and - AND: both must be true
- logical_or - OR: at least one must be true
- logical_not - Negate before XOR for inverted logic
- where - Select elements based on XOR results