From a138a3164231d667ffbaa36862cb2f81e04828b1 Mon Sep 17 00:00:00 2001 From: Google <53668973+GooglyBlox@users.noreply.github.com> Date: Wed, 29 Oct 2025 15:27:36 -0700 Subject: [PATCH 1/3] docs(pytorch): Add new entry for .neg() --- .../tensor-operations/terms/neg/neg.md | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/neg/neg.md diff --git a/content/pytorch/concepts/tensor-operations/terms/neg/neg.md b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md new file mode 100644 index 00000000000..5d1a8088c3e --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md @@ -0,0 +1,90 @@ +--- +Title: '.neg()' +Description: 'Returns a new tensor with the negative of each element in the input tensor.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Deep Learning' + - 'Methods' + - 'PyTorch' + - 'Tensor' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +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. + +## Syntax + +```pseudo +torch.neg(input, *, out=None) → Tensor +``` + +**Parameters:** + +- `input` (Tensor): The input tensor. + +**Keyword Arguments:** + +- `out` (Tensor, optional): The output tensor to store the result. If provided, the result is written to this tensor. + +**Return value:** + +Returns a new tensor where each element is the negative of the corresponding element in `input`. + +## Example + +The following example demonstrates how to use the `.neg()` method to negate tensor elements: + +```py +import torch + +# Create a tensor with positive and negative values +tensor = torch.tensor([1.5, -2.3, 0.0, 4.8, -1.2]) + +# Compute the negative using the method form +neg_tensor = tensor.neg() + +# Alternative: use the functional form +neg_functional = torch.neg(tensor) + +# Alternative: use the operator form +neg_operator = -tensor + +print("Original Tensor:") +print(tensor) + +print("\nNegated Tensor (using .neg()):") +print(neg_tensor) + +print("\nNegated Tensor (using torch.neg()):") +print(neg_functional) + +print("\nNegated Tensor (using - operator):") +print(neg_operator) +``` + +This example results in the following output: + +```shell +Original Tensor: +tensor([ 1.5000, -2.3000, 0.0000, 4.8000, -1.2000]) + +Negated Tensor (using .neg()): +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) + +Negated Tensor (using torch.neg()): +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) + +Negated Tensor (using - operator): +tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) +``` + +In this example: + +- Positive values become negative: 1.5 → -1.5, 4.8 → -4.8 +- Negative values become positive: -2.3 → 2.3, -1.2 → 1.2 +- Zero remains zero: 0.0 → -0.0 (negative zero in floating-point) +- All three forms (`.neg()`, `torch.neg()`, and `-`) produce identical results From ceeb83efd0d0ff902bc8ebc46bc3a14222ae5569 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 31 Oct 2025 13:07:44 +0530 Subject: [PATCH 2/3] Update neg.md with content fixes --- content/pytorch/concepts/tensor-operations/terms/neg/neg.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/neg/neg.md b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md index 5d1a8088c3e..e4b7a8f6cfa 100644 --- a/content/pytorch/concepts/tensor-operations/terms/neg/neg.md +++ b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md @@ -25,10 +25,7 @@ torch.neg(input, *, out=None) → Tensor **Parameters:** - `input` (Tensor): The input tensor. - -**Keyword Arguments:** - -- `out` (Tensor, optional): The output tensor to store the result. If provided, the result is written to this tensor. +- `out` (Tensor, optional): The output tensor to store the result. Must have the same shape as `input`. **Return value:** From d0749f7ecdadba06340bc2b45fb9c211c20bf2c1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 14:30:57 +0530 Subject: [PATCH 3/3] Update neg.md --- content/pytorch/concepts/tensor-operations/terms/neg/neg.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/neg/neg.md b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md index e4b7a8f6cfa..1d07a3cb18d 100644 --- a/content/pytorch/concepts/tensor-operations/terms/neg/neg.md +++ b/content/pytorch/concepts/tensor-operations/terms/neg/neg.md @@ -81,7 +81,7 @@ tensor([-1.5000, 2.3000, -0.0000, -4.8000, 1.2000]) In this example: -- Positive values become negative: 1.5 → -1.5, 4.8 → -4.8 -- Negative values become positive: -2.3 → 2.3, -1.2 → 1.2 -- Zero remains zero: 0.0 → -0.0 (negative zero in floating-point) +- Positive values become negative: `1.5` → `-1.5`, `4.8` → `-4.8` +- Negative values become positive: `-2.3` → `2.3`, `-1.2` → `1.2` +- Zero remains zero: `0.0` → `-0.0` (negative zero in floating-point) - All three forms (`.neg()`, `torch.neg()`, and `-`) produce identical results