Pytorch Torch Ne
## PyTorch `torch.ne` API Reference
The `torch.ne` function in PyTorch is used to perform element-wise **not-equal** ($a \neq b$) comparison operations. It compares each element of the input tensor with another tensor or a scalar value and returns a boolean tensor indicating the result of the comparison.
---
## Function Definition
### Syntax
```python
torch.ne(input, other, *, out=None) -> Tensor
```
Alternatively, you can use the equivalent operator alias or in-place method:
* **Operator Alias:** `input != other`
* **In-place Method:** `input.ne_(other)`
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `input` | `Tensor` | The input tensor to compare. |
| `other` | `Tensor` or `Scalar` | The tensor or scalar value to compare against. If it is a tensor, its shape must be broadcastable with the `input` tensor. |
| `out` | `Tensor` (Optional) | The output tensor to store the result. Must be a `BoolTensor`. |
### Return Value
* **Type:** `Tensor` (specifically, a `torch.BoolTensor`)
* **Description:** A boolean tensor of the same shape as the broadcasted inputs, where each element is `True` if the corresponding elements in `input` and `other` are **not equal**, and `False` otherwise.
---
## Code Examples
### Example 1: Comparing Two Tensors of the Same Shape
This is the most common use case, where two tensors of identical dimensions are compared element-by-element.
```python
import torch
# Define two 1D tensors
a = torch.tensor([1, 2, 3, 4])
b = torch.tensor([1, 2, 0, 4])
# Perform element-wise "not equal" comparison
result = torch.ne(a, b)
print("Tensor a: ", a)
print("Tensor b: ", b)
print("Result: ", result)
```
**Output:**
```text
Tensor a: tensor([1, 2, 3, 4])
Tensor b: tensor([1, 2, 0, 4])
Result: tensor([False, False, True, False])
```
---
### Example 2: Comparing a Tensor with a Scalar
You can also compare an entire tensor against a single scalar value. PyTorch automatically compares every element of the tensor to this scalar.
```python
import torch
# Define a 2D tensor
matrix = torch.tensor([[5, 10],
[15, 5]])
# Check which elements are not equal to 5
result = torch.ne(matrix, 5)
print("Matrix:\n", matrix)
print("Result (not equal to 5):\n", result)
```
**Output:**
```text
Matrix:
tensor([[ 5, 10],
[15, 5]])
Result (not equal to 5):
tensor([[False, True],
[ True, False]])
```
---
### Example 3: Using the Operator Alias (`!=`)
For cleaner and more readable code, you can use the standard Python inequality operator `!=`, which maps directly to `torch.ne`.
```python
import torch
x = torch.tensor([10, 20, 30])
y = torch.tensor([10, 25, 30])
# Using the != operator
result = (x != y)
print("Result using '!=':", result)
```
**Output:**
```text
Result using '!=': tensor([False, True, False])
```
---
### Example 4: Broadcasting Support
If the shapes of `input` and `other` do not match but are broadcastable, PyTorch will automatically expand them to a common shape before performing the comparison.
```python
import torch
# 2x1 Tensor
tensor_2d = torch.tensor([,
])
# 1x3 Tensor
tensor_1d = torch.tensor([1, 2, 3])
# Broad-casted comparison
result = torch.ne(tensor_2d, tensor_1d)
print("Resulting shape:", result.shape)
print("Result:\n", result)
```
**Output:**
```text
Resulting shape: torch.Size([2, 3])
Result:
tensor([[False, True, True],
[ True, False, True]])
```
---
## Important Considerations
1. **Data Type of Output:** The returned tensor is always of type `torch.bool`, regardless of the data types of the input tensors.
2. **Broadcasting Rules:** When comparing two tensors of different shapes, ensure they follow standard (https://pytorch.org/docs/stable/notes/broadcasting.html). If they are not broadcastable, PyTorch will throw a `RuntimeError`.
3. **In-place Operations:** If you want to modify a tensor in-place with the comparison results, use `input.ne_(other)`. Note that the input tensor must be of type `torch.bool` to store the boolean results in-place.
YouTip