From a5c821ce27aa3fb00acd61c8a186afc99c87d57d Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 5 Apr 2025 17:12:09 +0530 Subject: [PATCH 1/8] [Edit] Python (Pytorch): .cat() --- .../pytorch/concepts/tensors/terms/cat/cat.md | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index a8ab7cd0fc7..72828f33806 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -2,19 +2,20 @@ Title: '.cat()' Description: 'Concatenates two or more tensors in the same dimension.' Subjects: - - 'AI' + - 'Computer Science' - 'Data Science' Tags: - 'AI' - 'Deep Learning' - 'Functions' - 'Machine Learning' + - 'PyTorch' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'py-torch-for-classification' --- -The **`.cat()`** function in PyTorch concatenates two or more tensors along a specified dimension. The tensors must have the same shape in all dimensions except for the dimension along which they are concatenated. +The **`.cat()`** function in PyTorch concatenates two or more [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors) along a specified dimension. The tensors must have the same shape in all dimensions except for the dimension along which they are concatenated. ## Syntax @@ -22,13 +23,17 @@ The **`.cat()`** function in PyTorch concatenates two or more tensors along a sp torch.cat(tensors, dim=0, out=None) ``` -## Parameters +**Parameters:** - `tensors`: A sequence (like a list or tuple) of tensors to be concatenated. All tensors must have the same shape in all dimensions except for the specified dimension. - `dim`: An integer specifying the dimension along which the tensors will be concatenated. The default value is `0`, which means concatenation will occur along the first dimension. - `out`: A pre-allocated tensor with the correct shape to store the result of the concatenation. If not provided, a new tensor will be allocated. -## Example 1 +**Return value:** + +A new tensor resulting from concatenating the input tensors along the specified dimension. + +## Example 1: Concatenating tensors along the first dimension The example below showcases concatenating tensors along the first dimension using the `.cat()` function: @@ -70,7 +75,7 @@ tensor([[ 1, 2, 3], [10, 11, 12]]) ``` -## Example 2 +## Example 2: Concatenating tensors along the second dimension The example below showcases concatenating tensors along the second dimension using the `.cat()` function: @@ -110,7 +115,7 @@ tensor([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 10, 11, 12]]) ``` -## Example 3 +## Example 3: Concatenating tensors along the third dimension The example below showcases concatenating tensors along the third dimension using the `.cat()` function: @@ -158,3 +163,31 @@ tensor([[[ 1, 2, 3, 13, 14, 15], [[ 7, 8, 9, 19, 20, 21], [10, 11, 12, 22, 23, 24]]]) ``` + +## Best Practices for using `.cat()` in PyTorch + +1. **Match Shapes Across All Dimensions Except the Concatenation Axis:** Ensure all tensors have the same size in every dimension except the one you're concatenating along. Mismatched dimensions will cause errors. + +2. **Use a List or Tuple of Tensors:** Group tensors into a list or tuple when passing them to `.cat()` for cleaner, more readable code—especially when combining multiple tensors. + +3. **Avoid Unnecessary In-Place Operations:** Although torch.cat() supports an out parameter, it's usually best to let it return a new tensor unless memory usage is a concern. + +## FAQs + +
+1. What is the difference between torch.stack() and `.cat()`? +- `.cat()` combines tensors along an existing dimension. +- `.stack()` adds a new dimension and stacks tensors along that new axis. +Use stack when you want to create a new level of nesting; use cat to extend an existing one. +
+ +
+2. Can I concatenate tensors of different data types or devices? +

No. All tensors must have the same data type and must be on the same device (e.g., all on CPU or all on GPU). Mismatches will raise an error. +

+
+ +
+3. Can I concatenate along any dimension? +

Yes, as long as all other dimensions match. You can concatenate along any valid axis that exists in the input tensors.

+
From 3450c1348c44a3baf48de5d5f0aa398678a42922 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 9 Apr 2025 10:31:53 +0530 Subject: [PATCH 2/8] Update cat.md --- .../pytorch/concepts/tensors/terms/cat/cat.md | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index 72828f33806..a0f5e9d6312 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -1,6 +1,6 @@ --- Title: '.cat()' -Description: 'Concatenates two or more tensors in the same dimension.' +Description: 'Concatenates two or more tensors along a specified dimension.' Subjects: - 'Computer Science' - 'Data Science' @@ -33,9 +33,9 @@ torch.cat(tensors, dim=0, out=None) A new tensor resulting from concatenating the input tensors along the specified dimension. -## Example 1: Concatenating tensors along the first dimension +## Example 1: Merging Tensors Along Dimension 0 -The example below showcases concatenating tensors along the first dimension using the `.cat()` function: +This demonstration highlights how to combine tensors along the first axis (dimension 0) with the help of the `.cat()` method: ```py import torch @@ -75,9 +75,9 @@ tensor([[ 1, 2, 3], [10, 11, 12]]) ``` -## Example 2: Concatenating tensors along the second dimension +## Example 2: Stacking Tensors Across the Second Axis -The example below showcases concatenating tensors along the second dimension using the `.cat()` function: +In this case, tensors are joined along the second dimension (axis 1) using the `.cat()` function to demonstrate horizontal concatenation: ```py import torch @@ -115,9 +115,9 @@ tensor([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 10, 11, 12]]) ``` -## Example 3: Concatenating tensors along the third dimension +## Example 3: Appending Tensors on the Depth Dimension -The example below showcases concatenating tensors along the third dimension using the `.cat()` function: +This example illustrates how to append tensors along the third dimension (axis 2), effectively stacking them in depth using the `.cat()` method: ```py import torch @@ -164,26 +164,22 @@ tensor([[[ 1, 2, 3, 13, 14, 15], [10, 11, 12, 22, 23, 24]]]) ``` -## Best Practices for using `.cat()` in PyTorch - -1. **Match Shapes Across All Dimensions Except the Concatenation Axis:** Ensure all tensors have the same size in every dimension except the one you're concatenating along. Mismatched dimensions will cause errors. - -2. **Use a List or Tuple of Tensors:** Group tensors into a list or tuple when passing them to `.cat()` for cleaner, more readable code—especially when combining multiple tensors. - -3. **Avoid Unnecessary In-Place Operations:** Although torch.cat() supports an out parameter, it's usually best to let it return a new tensor unless memory usage is a concern. - ## FAQs
-1. What is the difference between torch.stack() and `.cat()`? -- `.cat()` combines tensors along an existing dimension. -- `.stack()` adds a new dimension and stacks tensors along that new axis. +1. What is the difference between `.stack()` and `.cat()`? +

+

    +
  • `.cat()` combines tensors along an existing dimension.
  • +
  • `.stack()` adds a new dimension and stacks tensors along that new axis.
  • +
Use stack when you want to create a new level of nesting; use cat to extend an existing one. +

2. Can I concatenate tensors of different data types or devices? -

No. All tensors must have the same data type and must be on the same device (e.g., all on CPU or all on GPU). Mismatches will raise an error. +

No. All tensors must have the same data type and must be on the same device (e.g., all on [CPU](https://www.codecademy.com/resources/blog/what-is-a-cpu/) or all on GPU). Mismatches will raise an error.

From a03cec3b062da503e08a95a6c9187712c85290a4 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 10 Apr 2025 15:55:10 +0530 Subject: [PATCH 3/8] Update content/pytorch/concepts/tensors/terms/cat/cat.md Co-authored-by: Radhika-okhade --- content/pytorch/concepts/tensors/terms/cat/cat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index a0f5e9d6312..fa64f1282ef 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -35,7 +35,7 @@ A new tensor resulting from concatenating the input tensors along the specified ## Example 1: Merging Tensors Along Dimension 0 -This demonstration highlights how to combine tensors along the first axis (dimension 0) with the help of the `.cat()` method: +This example highlights how to combine tensors along the first axis (dimension 0) with the help of the `.cat()` method: ```py import torch From 1e372577f2088713804fd743fabc220bda252c03 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 10 Apr 2025 15:55:20 +0530 Subject: [PATCH 4/8] Update content/pytorch/concepts/tensors/terms/cat/cat.md Co-authored-by: Radhika-okhade --- content/pytorch/concepts/tensors/terms/cat/cat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index fa64f1282ef..5a7c2793519 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -77,7 +77,7 @@ tensor([[ 1, 2, 3], ## Example 2: Stacking Tensors Across the Second Axis -In this case, tensors are joined along the second dimension (axis 1) using the `.cat()` function to demonstrate horizontal concatenation: +In this example, tensors are joined along the second dimension (axis 1) using the `.cat()` function to demonstrate horizontal concatenation: ```py import torch From e9ec266dd938a6c4981d51ed7e1c14110aa934c4 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 10 Apr 2025 16:00:32 +0530 Subject: [PATCH 5/8] Update cat.md --- content/pytorch/concepts/tensors/terms/cat/cat.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index 5a7c2793519..0fc34e98353 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -33,9 +33,9 @@ torch.cat(tensors, dim=0, out=None) A new tensor resulting from concatenating the input tensors along the specified dimension. -## Example 1: Merging Tensors Along Dimension 0 +## Example 1: Concatenating tensors along the first dimension -This example highlights how to combine tensors along the first axis (dimension 0) with the help of the `.cat()` method: +This demonstration highlights how to combine tensors along the first axis (dimension 0) with the help of the `.cat()` method: ```py import torch @@ -77,7 +77,7 @@ tensor([[ 1, 2, 3], ## Example 2: Stacking Tensors Across the Second Axis -In this example, tensors are joined along the second dimension (axis 1) using the `.cat()` function to demonstrate horizontal concatenation: +In this case, tensors are joined along the second dimension (axis 1) using the `.cat()` function to demonstrate horizontal concatenation: ```py import torch @@ -115,9 +115,9 @@ tensor([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 10, 11, 12]]) ``` -## Example 3: Appending Tensors on the Depth Dimension +## Example 3: Concatenating tensors along the third dimension -This example illustrates how to append tensors along the third dimension (axis 2), effectively stacking them in depth using the `.cat()` method: +This example shows how to append tensors along the third dimension (axis 2), effectively stacking them in depth using the `.cat()` method: ```py import torch @@ -164,7 +164,7 @@ tensor([[[ 1, 2, 3, 13, 14, 15], [10, 11, 12, 22, 23, 24]]]) ``` -## FAQs +## Frequently Asked Questions
1. What is the difference between `.stack()` and `.cat()`? From acd9581bfa185583e8d632d40e11ca5e41e77220 Mon Sep 17 00:00:00 2001 From: Avdhoot Fulsundar Date: Thu, 10 Apr 2025 23:10:07 +0530 Subject: [PATCH 6/8] Lint --- content/pytorch/concepts/tensors/terms/cat/cat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index 0fc34e98353..6d904333190 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -169,7 +169,7 @@ tensor([[[ 1, 2, 3, 13, 14, 15],
1. What is the difference between `.stack()` and `.cat()`?

-

    +
    • `.cat()` combines tensors along an existing dimension.
    • `.stack()` adds a new dimension and stacks tensors along that new axis.
    From 8ae0d7b5084ad5e44db7aa4c65877701aef8459a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 14 Apr 2025 18:31:01 +0530 Subject: [PATCH 7/8] Update cat.md --- .../pytorch/concepts/tensors/terms/cat/cat.md | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index 6d904333190..495a3caeff6 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -166,24 +166,15 @@ tensor([[[ 1, 2, 3, 13, 14, 15], ## Frequently Asked Questions -
    -1. What is the difference between `.stack()` and `.cat()`? -

    -

      -
    • `.cat()` combines tensors along an existing dimension.
    • -
    • `.stack()` adds a new dimension and stacks tensors along that new axis.
    • -
    +### 1. What is the difference between `.stack()` and `.cat()`? +- `.cat()` combines tensors along an existing dimension. +- `.stack()` adds a new dimension and stacks tensors along that new axis. + Use stack when you want to create a new level of nesting; use cat to extend an existing one. -

    -
    - -
    -2. Can I concatenate tensors of different data types or devices? -

    No. All tensors must have the same data type and must be on the same device (e.g., all on [CPU](https://www.codecademy.com/resources/blog/what-is-a-cpu/) or all on GPU). Mismatches will raise an error. -

    -
    - -
    -3. Can I concatenate along any dimension? -

    Yes, as long as all other dimensions match. You can concatenate along any valid axis that exists in the input tensors.

    -
    + +### 2. Can I concatenate tensors of different data types or devices? +No. All tensors must have the same data type and must be on the same device (e.g., all on [CPU](https://www.codecademy.com/resources/blog/what-is-a-cpu/) or all on GPU). Mismatches will raise an error. + +### 3. Can I concatenate along any dimension? +Yes, as long as all other dimensions match. You can concatenate along any valid axis that exists in the input tensors. + From 159fce191f729a4c0881edd1b12cc0ece5275b70 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 14 Apr 2025 18:35:45 +0530 Subject: [PATCH 8/8] lint and format fixed --- content/pytorch/concepts/tensors/terms/cat/cat.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensors/terms/cat/cat.md b/content/pytorch/concepts/tensors/terms/cat/cat.md index 495a3caeff6..81822c4f341 100644 --- a/content/pytorch/concepts/tensors/terms/cat/cat.md +++ b/content/pytorch/concepts/tensors/terms/cat/cat.md @@ -167,14 +167,16 @@ tensor([[[ 1, 2, 3, 13, 14, 15], ## Frequently Asked Questions ### 1. What is the difference between `.stack()` and `.cat()`? + - `.cat()` combines tensors along an existing dimension. - `.stack()` adds a new dimension and stacks tensors along that new axis. Use stack when you want to create a new level of nesting; use cat to extend an existing one. ### 2. Can I concatenate tensors of different data types or devices? + No. All tensors must have the same data type and must be on the same device (e.g., all on [CPU](https://www.codecademy.com/resources/blog/what-is-a-cpu/) or all on GPU). Mismatches will raise an error. ### 3. Can I concatenate along any dimension? + Yes, as long as all other dimensions match. You can concatenate along any valid axis that exists in the input tensors. -