|
| 1 | +--- |
| 2 | +Title: '.neg()' |
| 3 | +Description: 'Returns a new tensor with the negative of each element in the input tensor.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Deep Learning' |
| 9 | + - 'Methods' |
| 10 | + - 'PyTorch' |
| 11 | + - 'Tensor' |
| 12 | +CatalogContent: |
| 13 | + - 'intro-to-py-torch-and-neural-networks' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.neg()`** method in PyTorch returns a new [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) with the negative of each element in the input tensor. This operation multiplies each element by -1, effectively flipping the sign of all values. The method is commonly used in mathematical operations, gradient computations, and transformations in neural networks. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +torch.neg(input, *, out=None) → Tensor |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `input` (Tensor): The input tensor. |
| 28 | +- `out` (Tensor, optional): The output tensor to store the result. Must have the same shape as `input`. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns a new tensor where each element is the negative of the corresponding element in `input`. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +The following example demonstrates how to use the `.neg()` method to negate tensor elements: |
| 37 | + |
| 38 | +```py |
| 39 | +import torch |
| 40 | + |
| 41 | +# Create a tensor with positive and negative values |
| 42 | +tensor = torch.tensor([1.5, -2.3, 0.0, 4.8, -1.2]) |
| 43 | + |
| 44 | +# Compute the negative using the method form |
| 45 | +neg_tensor = tensor.neg() |
| 46 | + |
| 47 | +# Alternative: use the functional form |
| 48 | +neg_functional = torch.neg(tensor) |
| 49 | + |
| 50 | +# Alternative: use the operator form |
| 51 | +neg_operator = -tensor |
| 52 | + |
| 53 | +print("Original Tensor:") |
| 54 | +print(tensor) |
| 55 | + |
| 56 | +print("\nNegated Tensor (using .neg()):") |
| 57 | +print(neg_tensor) |
| 58 | + |
| 59 | +print("\nNegated Tensor (using torch.neg()):") |
| 60 | +print(neg_functional) |
| 61 | + |
| 62 | +print("\nNegated Tensor (using - operator):") |
| 63 | +print(neg_operator) |
| 64 | +``` |
| 65 | + |
| 66 | +This example results in the following output: |
| 67 | + |
| 68 | +```shell |
| 69 | +Original Tensor: |
| 70 | +tensor([ 1.5000, -2.3000, 0.0000, 4.8000, -1.2000]) |
| 71 | + |
| 72 | +Negated Tensor (using .neg()): |
| 73 | +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) |
| 74 | + |
| 75 | +Negated Tensor (using torch.neg()): |
| 76 | +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) |
| 77 | + |
| 78 | +Negated Tensor (using - operator): |
| 79 | +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) |
| 80 | +``` |
| 81 | +
|
| 82 | +In this example: |
| 83 | +
|
| 84 | +- Positive values become negative: `1.5` → `-1.5`, `4.8` → `-4.8` |
| 85 | +- Negative values become positive: `-2.3` → `2.3`, `-1.2` → `1.2` |
| 86 | +- Zero remains zero: `0.0` → `-0.0` (negative zero in floating-point) |
| 87 | +- All three forms (`.neg()`, `torch.neg()`, and `-`) produce identical results |
0 commit comments