|
| 1 | +--- |
| 2 | +Title: '.logical_or()' |
| 3 | +Description: 'Computes the element-wise logical OR between two tensors.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Booleans' |
| 9 | + - 'PyTorch' |
| 10 | + - 'Tensor' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/data-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`.logical_or()`** function performs an element-wise logical OR operation between two [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). Each input value is converted to a Boolean value (zero → `False`, non-zero → `True`) before the operation. The output is a Boolean tensor where each element is `True` if at least one of the corresponding input elements is `True`. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +torch.logical_or(input, other, out) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `input`: A tensor whose elements are treated as Boolean. |
| 27 | +- `other`: A tensor broadcastable with input. |
| 28 | +- `out` (optional): A tensor to store the result. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns a Boolean tensor where each element is `True` if at least one of the corresponding input elements is `True`. |
| 33 | + |
| 34 | +## Example 1 |
| 35 | + |
| 36 | +In this example, `.logical_or()` evaluates pairs of values from two 2-D tensors and returns a Boolean matrix showing which positions satisfy the OR condition: |
| 37 | + |
| 38 | +```py |
| 39 | +import torch |
| 40 | + |
| 41 | +a = torch.tensor([[0, 1], [2, 0]]) |
| 42 | +b = torch.tensor([[3, 0], [0, 5]]) |
| 43 | + |
| 44 | +result = torch.logical_or(a, b) |
| 45 | + |
| 46 | +print("Tensor A:\n", a) |
| 47 | +print("\nTensor B:\n", b) |
| 48 | +print("\nA OR B:\n", result) |
| 49 | +``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | +Tensor A: |
| 55 | + tensor([[0, 1], |
| 56 | + [2, 0]]) |
| 57 | + |
| 58 | +Tensor B: |
| 59 | + tensor([[3, 0], |
| 60 | + [0, 5]]) |
| 61 | + |
| 62 | +A OR B: |
| 63 | + tensor([[True, True], |
| 64 | + [True, True]]) |
| 65 | +``` |
| 66 | + |
| 67 | +## Example 2 |
| 68 | + |
| 69 | +In this example, a 1-D tensor is combined with a scalar using broadcasting to show how `.logical_or()` can apply a Boolean condition efficiently across all elements: |
| 70 | + |
| 71 | +```py |
| 72 | +import torch |
| 73 | + |
| 74 | +x = torch.tensor([0, 4, 0, 7]) |
| 75 | +y = torch.tensor(1) # scalar |
| 76 | + |
| 77 | +result = torch.logical_or(x, y) |
| 78 | + |
| 79 | +print(result) |
| 80 | +``` |
| 81 | + |
| 82 | +The output of this code is: |
| 83 | + |
| 84 | +```shell |
| 85 | +tensor([True, True, True, True]) |
| 86 | +``` |
| 87 | + |
| 88 | +## Frequently Asked Questions |
| 89 | + |
| 90 | +### 1. What are the tensor operations in PyTorch? |
| 91 | + |
| 92 | +Tensor operations in PyTorch include arithmetic, logical operations, reductions, reshaping, indexing, broadcasting, and matrix operations. These operations run efficiently on CPU or GPU and form the core building blocks of neural network workflows. |
| 93 | + |
| 94 | +### 2. Why use tensor instead of NumPy? |
| 95 | + |
| 96 | +Tensors support automatic differentiation and execute seamlessly on GPUs, which makes them suited for deep learning workloads. They also integrate tightly with PyTorch's computation graph, while NumPy arrays operate only on the CPU and lack gradient support. |
| 97 | + |
| 98 | +### 3. What does cpu() do in PyTorch? |
| 99 | + |
| 100 | +The `.cpu()` method moves a tensor or model from a GPU device to the system’s CPU. This is useful for running operations on hardware without CUDA support or preparing data for libraries that operate only on CPU-based arrays. |
0 commit comments