From 342d60bb746b5708a4c585ae6685de0e2d0a598c Mon Sep 17 00:00:00 2001 From: zkatona <92223927+zka26@users.noreply.github.com> Date: Fri, 31 Oct 2025 10:31:04 +0100 Subject: [PATCH 1/4] Create remainder.md with initial content structure Add remainder.md file with the term entry template. --- .../tensors/terms/remainder/remainder.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 content/pytorch/concepts/tensors/terms/remainder/remainder.md diff --git a/content/pytorch/concepts/tensors/terms/remainder/remainder.md b/content/pytorch/concepts/tensors/terms/remainder/remainder.md new file mode 100644 index 00000000000..fe0edd5dbbf --- /dev/null +++ b/content/pytorch/concepts/tensors/terms/remainder/remainder.md @@ -0,0 +1,44 @@ +--- +Title: 'The Title' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed +Description: 'A brief description.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'A subject name' + - 'A second subject name' + - 'An nth subject name' +Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! + - 'A tag' + - 'A second tag' + - 'An nth tag' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first + - 'learn-example-course' + - 'paths/example-path' +--- + +[A brief definition - make sure first mention of term is in **bold**.] + +## Syntax + +[Text, code, images, parameters, etc. about the syntax] + +## Example + +[Text, code, images, etc. about example 1] + +## Codebyte Example (if applicable) + +We can currently support: + +- Python +- JavaScript +- Ruby +- C++ +- C# +- Go +- PHP + +See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! + +```codebyte/js +# Example runnable code block. +console.log('Hello, World!'); +``` From 1e5fad4c9fe8ed17bcbfc6c707661ae5e8f654a4 Mon Sep 17 00:00:00 2001 From: zkatona <92223927+zka26@users.noreply.github.com> Date: Fri, 31 Oct 2025 12:35:28 +0100 Subject: [PATCH 2/4] Add `.remainder()` term under PyTorch Tensor Operations. This commit adds the .remainder() term under PyTorch Tensor Operations to the previously made remainder.md template at the following path: docs/content/pytorch/concepts/tensors/terms/remainder/remainder.md Implements #7855 --- .../tensors/terms/remainder/remainder.md | 103 +++++++++++++----- 1 file changed, 73 insertions(+), 30 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/remainder/remainder.md b/content/pytorch/concepts/tensors/terms/remainder/remainder.md index fe0edd5dbbf..284e752a328 100644 --- a/content/pytorch/concepts/tensors/terms/remainder/remainder.md +++ b/content/pytorch/concepts/tensors/terms/remainder/remainder.md @@ -1,44 +1,87 @@ --- -Title: 'The Title' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed -Description: 'A brief description.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! - - 'A subject name' - - 'A second subject name' - - 'An nth subject name' -Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! - - 'A tag' - - 'A second tag' - - 'An nth tag' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first - - 'learn-example-course' - - 'paths/example-path' +Title: '.remainder()' +Description: 'Computes the remainder of tensor division; result sign matches the divisor.' +Subjects: + - 'AI' + - 'Data Science' +Tags: + - 'AI' + - 'Deep Learning' + - 'Functions' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'py-torch-for-classification' --- -[A brief definition - make sure first mention of term is in **bold**.] +The **`.remainder()`** operation computes, for each value in a tensor, the remainder after division by a number or by another tensor. The remainder **has the same sign as the divisor**. Works with both integer and floating-point tensors. ## Syntax +To compute the remainder, use either of the following forms: +```pseudo +torch.remainder(input, other) +``` +```pseudo +input.remainder(other) +``` +Parameters: +- `input:` A tensor of integer or floating-point values. +- `other:` A non-zero number or a tensor used as the divisor. -[Text, code, images, parameters, etc. about the syntax] +Returns: +A tensor of remainders. If `other` is a number, the same divisor is applied to every value in `input`. If `other` is a tensor of a compatible shape, division happens value by value. -## Example +## Example 1: Divide a 1D tensor by an integer -[Text, code, images, etc. about example 1] +```py +import torch -## Codebyte Example (if applicable) +x = torch.tensor([-3, -2, -1, 0, 1, 2, 3]) +print(torch.remainder(x, 2)) +``` +**Output:** +```shell +tensor([1, 0, 1, 0, 1, 0, 1]) +``` -We can currently support: +## Example 2: Divide a 2D tensor by an integer +```py +import torch -- Python -- JavaScript -- Ruby -- C++ -- C# -- Go -- PHP +A = torch.tensor([[ 1, 2, 3], + [-1, -2, -3]]) +print(torch.remainder(A, 4)) +``` +**Output:** +```shell +tensor([[1, 2, 3], + [3, 2, 1]]) +``` +**Note:** For a positive divisor `d`, the remainder `r` is the unique value with `0 ≤ r < d` such that `a = d*q + r`.\ +Thus for `d = 4`: `-1 → 3`, `-2 → 2`, `-3 → 1`, so `[-1, -2, -3]` becomes `[3, 2, 1]`. -See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! +## Example 3: Divide a tensor by another tensor +```py +import torch -```codebyte/js -# Example runnable code block. -console.log('Hello, World!'); +num = torch.tensor([ 3, -3, 3, -3], dtype=torch.int32) +den = torch.tensor([ 2, 2, -2, -2], dtype=torch.int32) +print(torch.remainder(num, den)) +``` +**Output:** +```shell +tensor([ 1, 1, -1, -1]) +``` +**Note:** If `input` and `other` don’t share the same shape, PyTorch tries **automatic size expansion** (*broadcasting*). Dimensions match when they’re equal or one of them is 1 (aligned from the right). If no match is possible, a size-mismatch error is raised.\ +**Examples:** shape `(2, 3)` ÷ `(3,) → expands to (2, 3)` or shape `(2, 3)` ÷ `(2,) → mismatch.` + +## Example 4: Divide a floating-point tensor by a number +```py +import torch + +xf = torch.tensor([-7.5, 7.5, 5.0]) +print(torch.remainder(xf, 4.0)) +``` +**Output:** +```shell +tensor([0.5, 3.5, 1.0]) ``` From 27ad00b5f9748753cc4d81e7f764dd757146c85b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 1 Nov 2025 16:46:33 +0530 Subject: [PATCH 3/4] Update remainder.md --- .../tensors/terms/remainder/remainder.md | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/remainder/remainder.md b/content/pytorch/concepts/tensors/terms/remainder/remainder.md index 284e752a328..26429d02c24 100644 --- a/content/pytorch/concepts/tensors/terms/remainder/remainder.md +++ b/content/pytorch/concepts/tensors/terms/remainder/remainder.md @@ -1,8 +1,8 @@ --- Title: '.remainder()' -Description: 'Computes the remainder of tensor division; result sign matches the divisor.' +Description: 'Computes the element-wise remainder of tensor division, where the result’s sign matches the divisor.' Subjects: - - 'AI' + - 'Computer Science' - 'Data Science' Tags: - 'AI' @@ -13,37 +13,49 @@ CatalogContent: - 'py-torch-for-classification' --- -The **`.remainder()`** operation computes, for each value in a tensor, the remainder after division by a number or by another tensor. The remainder **has the same sign as the divisor**. Works with both integer and floating-point tensors. +The **`.remainder()`** function computes the element-wise remainder of division between two tensors or between a tensor and a scalar. The result always has the same sign as the divisor, unlike torch.fmod(), which matches the sign of the dividend. +This operation works with both integer and floating-point tensors. ## Syntax -To compute the remainder, use either of the following forms: + ```pseudo torch.remainder(input, other) ``` -```pseudo -input.remainder(other) -``` -Parameters: -- `input:` A tensor of integer or floating-point values. -- `other:` A non-zero number or a tensor used as the divisor. -Returns: -A tensor of remainders. If `other` is a number, the same divisor is applied to every value in `input`. If `other` is a tensor of a compatible shape, division happens value by value. +**Parameters:** + +- `input` (Tensor): The input tensor containing dividend values. +- `other` (Tensor or Number): The divisor. It can be a scalar or another tensor of compatible shape. +- `out` (Tensor, optional): The output tensor to store the result. + +**Return value:** + +Returns a tensor containing the element-wise remainder of the division. + +- If `other` is a scalar, the operation is applied using the same divisor for all elements. +- If `other` is a tensor, element-wise division is performed. ## Example 1: Divide a 1D tensor by an integer +This example computes the remainder of each element in `x` when divided by 2, keeping the sign consistent with the divisor: + ```py import torch x = torch.tensor([-3, -2, -1, 0, 1, 2, 3]) print(torch.remainder(x, 2)) ``` -**Output:** + +The output of this code is: + ```shell tensor([1, 0, 1, 0, 1, 0, 1]) ``` ## Example 2: Divide a 2D tensor by an integer + +In this example each negative number wraps around within the range `[0, 4)` since the remainder must match the divisor’s sign: + ```py import torch @@ -51,15 +63,18 @@ A = torch.tensor([[ 1, 2, 3], [-1, -2, -3]]) print(torch.remainder(A, 4)) ``` -**Output:** + +The output of this code is: + ```shell tensor([[1, 2, 3], [3, 2, 1]]) ``` -**Note:** For a positive divisor `d`, the remainder `r` is the unique value with `0 ≤ r < d` such that `a = d*q + r`.\ -Thus for `d = 4`: `-1 → 3`, `-2 → 2`, `-3 → 1`, so `[-1, -2, -3]` becomes `[3, 2, 1]`. ## Example 3: Divide a tensor by another tensor + +This example demonstrates element-wise remainder calculation between two tensors of the same shape: + ```py import torch @@ -67,21 +82,28 @@ num = torch.tensor([ 3, -3, 3, -3], dtype=torch.int32) den = torch.tensor([ 2, 2, -2, -2], dtype=torch.int32) print(torch.remainder(num, den)) ``` -**Output:** + +The output of this code is: + ```shell tensor([ 1, 1, -1, -1]) ``` -**Note:** If `input` and `other` don’t share the same shape, PyTorch tries **automatic size expansion** (*broadcasting*). Dimensions match when they’re equal or one of them is 1 (aligned from the right). If no match is possible, a size-mismatch error is raised.\ -**Examples:** shape `(2, 3)` ÷ `(3,) → expands to (2, 3)` or shape `(2, 3)` ÷ `(2,) → mismatch.` + +> **Note:** If `input` and `other` don’t share the same shape, PyTorch tries automatic size expansion (broadcasting). Dimensions match when they’re equal or one of them is 1 (aligned from the right). If no match is possible, a size-mismatch error is raised. For example: shape `(2, 3)` ÷ `(3,) → expands to (2, 3)` or shape `(2, 3)` ÷ `(2,) → mismatch.` ## Example 4: Divide a floating-point tensor by a number + +This example calculates remainders for floating-point values, preserving the sign of the divisor: + ```py import torch xf = torch.tensor([-7.5, 7.5, 5.0]) print(torch.remainder(xf, 4.0)) ``` -**Output:** + +The output of this code is: + ```shell tensor([0.5, 3.5, 1.0]) ``` From c334ca58aa8c198fa401cfc65954b4d290396064 Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Tue, 4 Nov 2025 15:21:58 +0530 Subject: [PATCH 4/4] Minor changes --- .../tensors/terms/remainder/remainder.md | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/remainder/remainder.md b/content/pytorch/concepts/tensors/terms/remainder/remainder.md index 26429d02c24..6d847ef95f4 100644 --- a/content/pytorch/concepts/tensors/terms/remainder/remainder.md +++ b/content/pytorch/concepts/tensors/terms/remainder/remainder.md @@ -13,8 +13,7 @@ CatalogContent: - 'py-torch-for-classification' --- -The **`.remainder()`** function computes the element-wise remainder of division between two tensors or between a tensor and a scalar. The result always has the same sign as the divisor, unlike torch.fmod(), which matches the sign of the dividend. -This operation works with both integer and floating-point tensors. +In PyTorch, the **`.remainder()`** function computes the element-wise remainder of division between two tensors or between a tensor and a scalar. The result always has the same sign as the divisor, unlike `.fmod()`, which matches the sign of the dividend. This operation works with both integer and floating-point tensors. ## Syntax @@ -35,26 +34,26 @@ Returns a tensor containing the element-wise remainder of the division. - If `other` is a scalar, the operation is applied using the same divisor for all elements. - If `other` is a tensor, element-wise division is performed. -## Example 1: Divide a 1D tensor by an integer +## Example 1: Divide a 1D Tensor by an Integer This example computes the remainder of each element in `x` when divided by 2, keeping the sign consistent with the divisor: ```py import torch -x = torch.tensor([-3, -2, -1, 0, 1, 2, 3]) -print(torch.remainder(x, 2)) +x = torch.tensor([-3, -4, -1, -6, 4, 7, 8]) +print(torch.remainder(x, 3)) ``` The output of this code is: ```shell -tensor([1, 0, 1, 0, 1, 0, 1]) +tensor([0, 2, 2, 0, 1, 1, 2]) ``` -## Example 2: Divide a 2D tensor by an integer +## Example 2: Divide a 2D Tensor by an Integer -In this example each negative number wraps around within the range `[0, 4)` since the remainder must match the divisor’s sign: +In this example, each negative number wraps around within the range `[0, 4)` since the remainder must match the divisor’s sign: ```py import torch @@ -71,7 +70,7 @@ tensor([[1, 2, 3], [3, 2, 1]]) ``` -## Example 3: Divide a tensor by another tensor +## Example 3: Divide a Tensor by Another Tensor This example demonstrates element-wise remainder calculation between two tensors of the same shape: @@ -89,9 +88,9 @@ The output of this code is: tensor([ 1, 1, -1, -1]) ``` -> **Note:** If `input` and `other` don’t share the same shape, PyTorch tries automatic size expansion (broadcasting). Dimensions match when they’re equal or one of them is 1 (aligned from the right). If no match is possible, a size-mismatch error is raised. For example: shape `(2, 3)` ÷ `(3,) → expands to (2, 3)` or shape `(2, 3)` ÷ `(2,) → mismatch.` +> **Note:** If `input` and `other` don’t share the same shape, PyTorch tries automatic size expansion (broadcasting). Dimensions match when they’re equal or one of them is 1 (aligned from the right). If no match is possible, a size-mismatch error is raised. -## Example 4: Divide a floating-point tensor by a number +## Example 4: Divide a Floating-Point Tensor by a Number This example calculates remainders for floating-point values, preserving the sign of the divisor: