|
| 1 | +--- |
| 2 | +Title: '.logical_not()' |
| 3 | +Description: 'Performs element-wise logical NOT on boolean tensors, returning a tensor where each element is the boolean negation of the input.' |
| 4 | +Subjects: |
| 5 | + - 'AI' |
| 6 | + - 'Computer Science' |
| 7 | + - 'Data Science' |
| 8 | + - 'Machine Learning' |
| 9 | +Tags: |
| 10 | + - 'Booleans' |
| 11 | + - 'Functions' |
| 12 | + - 'PyTorch' |
| 13 | + - 'Tensor' |
| 14 | +CatalogContent: |
| 15 | + - 'intro-to-py-torch-and-neural-networks' |
| 16 | + - 'paths/data-science' |
| 17 | +--- |
| 18 | + |
| 19 | +In PyTorch, the **`.logical_not()`** function performs an element-wise logical negation on a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a tensor where each element is `True` if the corresponding input is `False`, and `False` otherwise. For non-boolean tensors, zeros are treated as `False` and non-zeros as `True`. |
| 20 | + |
| 21 | +This function is commonly used in masking, boolean indexing, and creating complement conditions. |
| 22 | + |
| 23 | +## Syntax |
| 24 | + |
| 25 | +```pseudo |
| 26 | +torch.logical_not(input, *, out=None) |
| 27 | +``` |
| 28 | + |
| 29 | +**Parameters:** |
| 30 | + |
| 31 | +- `input`: The input tensor containing boolean or numeric values. |
| 32 | +- `out` (Optional): A tensor to store the result. It must have the same shape as the output. The dtype is typically `torch.bool`, but integer types that can represent `0` and `1` (like `torch.int16`) are also supported. The results are stored as `1` for `True` and `0` for `False`. |
| 33 | + |
| 34 | +**Return value:** |
| 35 | + |
| 36 | +Returns a new tensor containing the element-wise logical negation of the input tensor. |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +The following example demonstrates the use of `.logical_not()` for masking and boolean inversion: |
| 41 | + |
| 42 | +```py |
| 43 | +import torch |
| 44 | + |
| 45 | +# Create a boolean tensor |
| 46 | +mask = torch.tensor([True, False, True, False]) |
| 47 | + |
| 48 | +# Element-wise logical NOT |
| 49 | +inv = torch.logical_not(mask) |
| 50 | + |
| 51 | +print('mask:', mask) |
| 52 | +print('logical_not(mask):', inv) |
| 53 | + |
| 54 | +# Use logical_not to invert a condition from a numeric tensor |
| 55 | +vals = torch.tensor([0.0, 1.5, -2.0, 0.0]) |
| 56 | +cond = vals > 0 |
| 57 | +cond_inv = torch.logical_not(cond) |
| 58 | + |
| 59 | +print('\nvals:', vals) |
| 60 | +print('cond (vals > 0):', cond) |
| 61 | +print('cond inverted with logical_not:', cond_inv) |
| 62 | + |
| 63 | +# Numeric input — zeros are False, non-zeros are True |
| 64 | +print(torch.logical_not(torch.tensor([0., 2.45, -20., 3.8], dtype=torch.double))) |
| 65 | + |
| 66 | +# Using an integer out tensor (int16) to store 0/1 results |
| 67 | +out_buf = torch.empty(4, dtype=torch.int16) |
| 68 | +print(torch.logical_not(torch.tensor([0., 2.45, -20., 3.8], dtype=torch.double), out=out_buf)) |
| 69 | +``` |
| 70 | + |
| 71 | +The above code produces the following output: |
| 72 | + |
| 73 | +```shell |
| 74 | +mask: tensor([ True, False, True, False]) |
| 75 | +logical_not(mask): tensor([False, True, False, True]) |
| 76 | + |
| 77 | +vals: tensor([ 0.0000, 1.5000, -2.0000, 0.0000]) |
| 78 | +cond (vals > 0): tensor([False, True, False, False]) |
| 79 | +cond inverted with logical_not: tensor([ True, False, True, True]) |
| 80 | +tensor([ True, False, False, False]) |
| 81 | +tensor([1, 0, 0, 0], dtype=torch.int16) |
| 82 | +``` |
0 commit comments