YouTip LogoYouTip

Pytorch Torch Logical_Or

## PyTorch `torch.logical_or` Reference `torch.logical_or` is a built-in PyTorch function used to compute the element-wise logical OR operation between two input tensors. This function is highly useful for masking, filtering, and implementing conditional logic within deep learning models and data preprocessing pipelines. --- ## Function Definition ```python torch.logical_or(input, other, *, out=None) ``` ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `input` | `Tensor` | The first input tensor. | | `other` | `Tensor` | The second input tensor. | | `out` | `Tensor` (Optional) | The output tensor to store the result. | ### Return Value * Returns a new tensor of type `torch.bool` containing the element-wise logical OR result. * If non-boolean tensors are passed as inputs, non-zero values are treated as `True` (logical 1), and zero values are treated as `False` (logical 0). --- ## Code Examples ### Example 1: Basic Logical OR with Boolean Tensors This example demonstrates the standard logical OR operation using two 1D boolean tensors. ```python import torch # Create two boolean tensors x = torch.tensor([True, False, True, False]) y = torch.tensor([True, True, False, False]) # Perform element-wise logical OR z = torch.logical_or(x, y) print("Input x: ", x) print("Input y: ", y) print("logical_or: ", z) ``` **Output:** ```text Input x: tensor([ True, False, True, False]) Input y: tensor([ True, True, False, False]) logical_or: tensor([ True, True, True, False]) ``` --- ### Example 2: Logical OR with Numeric Tensors (Type Casting) When working with integer or floating-point tensors, `torch.logical_or` treats `0` and `0.0` as `False`, and any non-zero value as `True`. ```python import torch # Create two integer tensors x = torch.tensor([0, 5, 0, -3]) y = torch.tensor([0, 0, 2, 0]) # Perform logical OR z = torch.logical_or(x, y) print("Input x: ", x) print("Input y: ", y) print("logical_or: ", z) ``` **Output:** ```text Input x: tensor([ 0, 5, 0, -3]) Input y: tensor([ 0, 0, 2, 0]) logical_or: tensor([False, True, True, True]) ``` --- ### Example 3: Element-wise Broadcasting `torch.logical_or` supports PyTorch broadcasting rules. If the shapes of the two input tensors do not match, PyTorch will automatically expand the singleton dimensions to match the larger tensor. ```python import torch # 2D tensor of shape (2, 3) x = torch.tensor([[True, False, False], [False, False, True]]) # 1D tensor of shape (3,) - will be broadcasted to (2, 3) y = torch.tensor([True, False, False]) # Perform logical OR with broadcasting z = torch.logical_or(x, y) print("Result:\n", z) ``` **Output:** ```text Result: tensor([[ True, False, False], [ True, False, True]]) ``` --- ### Example 4: In-Place Operation and Out Parameter You can use the `out` parameter to write the results directly into an existing tensor to optimize memory usage. Alternatively, you can use the in-place method `.logical_or_()`. ```python import torch x = torch.tensor([True, False]) y = torch.tensor([False, False]) # Using the out parameter out_tensor = torch.empty(2, dtype=torch.bool) torch.logical_or(x, y, out=out_tensor) print("Using out parameter: ", out_tensor) # Using the in-place method x.logical_or_(y) print("Using in-place method:", x) ``` **Output:** ```text Using out parameter: tensor([ True, False]) Using in-place method: tensor([ True, False]) ``` --- ## Considerations 1. **Broadcasting Constraints**: The shapes of `input` and `other` must be broadcastable. If their dimensions are incompatible, PyTorch will raise a `RuntimeError`. 2. **Data Type of Output**: Regardless of the input tensor data types (e.g., `float32`, `int64`), the output of `torch.logical_or` is always a boolean tensor (`torch.bool`). 3. **Alternative Operator**: You can also use the `|` operator for element-wise logical OR operations on boolean tensors in PyTorch (e.g., `x | y`). However, `torch.logical_or` is preferred when explicit type casting of numeric tensors is required, or when using the `out` parameter.
← Pytorch Torch LogitPytorch Torch Log β†’