torch.logical_or
function logical_or<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Computes the element-wise logical OR.
Returns true where AT LEAST ONE of input or other is non-zero (true). Essential for:
- Combining filters: find elements matching ANY condition
- Mask union: get elements valid in either mask (inclusive)
- Conditional logic: "if condition1 OR condition2"
- Multi-option selection: elements matching any option
- Feature detection: find regions with any anomaly
- Fault tolerance: mark elements passing any test
Broadcasting: Automatically broadcasts shapes. [3] OR [1] broadcasts to [3].
- Inclusive OR: "OR" includes both being true (not exclusive, unlike XOR)
- Short-circuit possible: Stops checking once True found
- Union semantics: Takes union of both masks/sets
- Boolean output: Always returns boolean dtype
- One is enough: Only need ONE true to get true result
- Broadcasting shapes: [3] OR [1] broadcasts to [3]
- Inclusive operation: Both true gives true (not exclusive)
- Type coercion: Non-zero values 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
// Combine two boolean masks with union semantics
const mask1 = torch.tensor([true, true, false, false]);
const mask2 = torch.tensor([true, false, true, false]);
torch.logical_or(mask1, mask2); // [true, true, true, false]// Find outliers: values very low OR very high
const data = torch.tensor([1, 2, 15, 3, 20, 2, 1]);
const too_low = torch.lt(data, 2); // [false, false, false, false, false, false, false]
const too_high = torch.gt(data, 10); // [false, false, true, false, true, false, false]
const outliers = torch.logical_or(too_low, too_high); // Find values < 2 or > 10
// [false, false, true, false, true, false, false]// Data quality: mark elements that are either NaN OR out of range
const values = torch.tensor([1.5, NaN, -10, 3.5, NaN]);
const is_nan = torch.isnan(values); // [false, true, false, false, true]
const out_of_range = torch.logical_or(torch.lt(values, 0), torch.gt(values, 10));
const problematic = torch.logical_or(is_nan, out_of_range);
// Elements that are NaN OR out of [0, 10] range// Feature detection: mark regions with any noteworthy feature
const image = torch.randn(64, 64);
const bright = torch.gt(image, 2.0); // Very bright pixels
const dark = torch.lt(image, -2.0); // Very dark pixels
const interesting = torch.logical_or(bright, dark); // Either bright or dark
const interesting_count = interesting.sum().item();// Broadcasting: combining conditions across batches
const batch_valid = torch.tensor([true, false, true]); // Shape [3]
const element_has_nan = torch.tensor([[false], [true]]); // Shape [2, 1]
torch.logical_or(batch_valid, element_has_nan);
// Broadcasts to [2, 3]: mark where batch is valid OR element has NaNSee Also
- PyTorch torch.logical_or(input, other)
- logical_and - AND: true only when BOTH are true
- logical_not - Negate conditions before OR
- logical_xor - XOR: true when ONLY ONE is true
- where - Select elements based on OR results