From 7c5e89c0baf3612bd1af5916cc7571d6d36e516c Mon Sep 17 00:00:00 2001 From: Krishna Kaushik <131583096+kRiShNa-429407@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:24:12 +0530 Subject: [PATCH 1/6] Add files via upload --- .../machine-learning/PyTorch_Fundamentals.md | 427 ++++++++++++++++++ 1 file changed, 427 insertions(+) create mode 100644 contrib/machine-learning/PyTorch_Fundamentals.md diff --git a/contrib/machine-learning/PyTorch_Fundamentals.md b/contrib/machine-learning/PyTorch_Fundamentals.md new file mode 100644 index 0000000..a504ffc --- /dev/null +++ b/contrib/machine-learning/PyTorch_Fundamentals.md @@ -0,0 +1,427 @@ +# PyTorch Fundamentals + + +```python +# Import pytorch in our codespace +import torch +print(torch.__version__) +``` + + 2.3.0+cu121 + + +2.3.0 is the pytorch version and 121 is the cuda version + +Now you have already seen how to create a tensor in pytorch. In this notebook i am going to show you the operations which can be applied on a tensor with a quick previous revision. + +### 1. Creating tensors + + +```python +# Scalar tensor ( a zero dimension tensor) +scalar = torch.tensor(7) +print(scalar) +``` + + tensor(7) + + + +```python +# Check the dimension of the above tensor +print(scalar.ndim) +``` + + 0 + + + +```python +# To retrieve the number from the tensor we use `item()` +print(scalar.item()) +``` + + 7 + + + +```python +# Vector (It is a single dimension tensor but contain many numbers) +vector = torch.tensor([1,2]) +print(vector) +``` + + tensor([1, 2]) + + + +```python +# Check the dimensions +print(vector.ndim) +``` + + 1 + + + +```python +# Check the shape of the vector +print(vector.shape) +``` + + torch.Size([2]) + + +The above returns torch.Size([2]) which means our vector has a shape of [2]. This is because of the two elements we placed inside the square brackets ([1,2]) + +Note: +I'll let you in on a trick. + +You can tell the number of dimensions a tensor in PyTorch has by the number of square brackets on the outside ([) and you only need to count one side. + + +```python +# Let's create a matrix +MATRIX = torch.tensor([[1,2], + [4,5]]) +print(MATRIX) +``` + + tensor([[1, 2], + [4, 5]]) + + +There are two brackets so it must be 2 dimensions , lets check + + +```python +print(MATRIX.ndim) +``` + + 2 + + + +```python +# Shape +print(MATRIX.shape) +``` + + torch.Size([2, 2]) + + +It means MATRIX has 2 rows and 2 columns. + + +```python +# Let's create a TENSOR +TENSOR = torch.tensor([[[1,2,3], + [4,5,6], + [7,8,9]]]) +print(TENSOR) +``` + + tensor([[[1, 2, 3], + [4, 5, 6], + [7, 8, 9]]]) + + + +```python +# Let's check the dimensions +print(TENSOR.ndim) +``` + + 3 + + + +```python +# shape? +print(TENSOR.shape) +``` + + torch.Size([1, 3, 3]) + + +The dimensions go outer to inner. + +That means there's 1 dimension of 3 by 3. + +##### Let's summarise + +* scalar -> a single number having 0 dimension. +* vector -> have many numbers but having 1 dimension. +* matrix -> a array of numbers having 2 dimensions. +* tensor -> a array of numbers having n dimensions. + +### Random Tensors + +We can create them using `torch.rand()` and passing in the `size` parameter. + + +```python +# creating a random tensor of size (3,4) +rand_tensor = torch.rand(size = (3,4)) +print(rand_tensor) +``` + + tensor([[0.7462, 0.4950, 0.7851, 0.8277], + [0.6112, 0.5159, 0.1728, 0.6847], + [0.4472, 0.1612, 0.6481, 0.3236]]) + + + +```python +# Check the dimensions +print(rand_tensor.ndim) +``` + + 2 + + + +```python +# Shape +print(rand_tensor.shape) +``` + + torch.Size([3, 4]) + + + +```python +# datatype +print(rand_tensor.dtype) +``` + + torch.float32 + + +### Zeros and ones + +Here we will create a tensor of any shape filled with zeros and ones + + +```python +# Create a tensor of all zeros +zeros = torch.zeros(size = (3,4)) +print(zeros) +``` + + tensor([[0., 0., 0., 0.], + [0., 0., 0., 0.], + [0., 0., 0., 0.]]) + + + +```python +# create a tensor of ones +ones = torch.ones(size = (3,4)) +print(ones) +``` + + tensor([[1., 1., 1., 1.], + [1., 1., 1., 1.], + [1., 1., 1., 1.]]) + + +### Create a tensor having range of numbers + +You can use `torch.arange(start, end, step)` to do so. + +Where: + +* start = start of range (e.g. 0) +* end = end of range (e.g. 10) +* step = how many steps in between each value (e.g. 1) + +> Note: In Python, you can use range() to create a range. However in PyTorch, torch.range() is deprecated show error, show use `torch.arange()` + + +```python +zero_to_ten = torch.arange(start = 0, + end = 10, + step = 1) +print(zero_to_ten) +``` + + tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + + +# 2. Manipulating tensors (tensor operations) + +The operations are : + +* Addition +* Substraction +* Multiplication (element-wise) +* Division +* Matrix multiplication + +### 1. Addition + + +```python +tensor = torch.tensor([1,2,3]) +print(tensor+10) +``` + + tensor([11, 12, 13]) + + +We have add 10 to each tensor element. + + +```python +tensor1 = torch.tensor([4,5,6]) +print(tensor+tensor1) +``` + + tensor([5, 7, 9]) + + +We have added two tensors , remember that addition takes place element wise. + +### 2. Subtraction + + +```python +print(tensor-8) +``` + + tensor([-7, -6, -5]) + + +We've subtracted 8 from the above tensor. + + +```python +print(tensor-tensor1) +``` + + tensor([-3, -3, -3]) + + +### 3. Multiplication + + +```python +# Multiply the tensor with 10 (element wise) +print(tensor*10) +``` + + tensor([10, 20, 30]) + + +Each element of tensor gets multiplied by 10. + +Note: + +PyTorch also has a bunch of built-in functions like `torch.mul()` (short for multiplication) and `torch.add()` to perform basic operations. + + +```python +# let's see them +print(torch.add(tensor,10)) +``` + + tensor([11, 12, 13]) + + + +```python +print(torch.mul(tensor,10)) +``` + + tensor([10, 20, 30]) + + +### Matrix multiplication (is all you need) +One of the most common operations in machine learning and deep learning algorithms (like neural networks) is matrix multiplication. + +PyTorch implements matrix multiplication functionality in the `torch.matmul()` method. + +The main two rules for matrix multiplication to remember are: + +The inner dimensions must match: +* (3, 2) @ (3, 2) won't work +* (2, 3) @ (3, 2) will work +* (3, 2) @ (2, 3) will work +The resulting matrix has the shape of the outer dimensions: +* (2, 3) @ (3, 2) -> (2, 2) +* (3, 2) @ (2, 3) -> (3, 3) + + +Note: "@" in Python is the symbol for matrix multiplication. + + +```python +# let's perform the matrix multiplication +tensor1 = torch.tensor([[[1,2,3], + [4,5,6], + [7,8,9]]]) +tensor2 = torch.tensor([[[1,1,1], + [2,2,2], + [3,3,3]]]) + +print(tensor1) , print(tensor2) +``` + + tensor([[[1, 2, 3], + [4, 5, 6], + [7, 8, 9]]]) + tensor([[[1, 1, 1], + [2, 2, 2], + [3, 3, 3]]]) + + + + + + (None, None) + + + + +```python +# let's check the shape +print(tensor1.shape) , print(tensor2.shape) +``` + + torch.Size([1, 3, 3]) + torch.Size([1, 3, 3]) + + + + + + (None, None) + + + + +```python + # Matrix multiplication +print(torch.matmul(tensor1, tensor2)) +``` + + tensor([[[14, 14, 14], + [32, 32, 32], + [50, 50, 50]]]) + + + +```python +# Can also use the "@" symbol for matrix multiplication, though not recommended +print(tensor1 @ tensor2) +``` + + tensor([[[14, 14, 14], + [32, 32, 32], + [50, 50, 50]]]) + + +Note: + +If shape is not perfect you can transpose the tensor and perform the matrix multiplication. From 03e9338a91c16d46dc36bc46d2c5b5ca223b81fc Mon Sep 17 00:00:00 2001 From: Krishna Kaushik <131583096+kRiShNa-429407@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:34:39 +0530 Subject: [PATCH 2/6] Update index.md --- contrib/machine-learning/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 0bf39f9..b52371a 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -20,3 +20,4 @@ - [Grid Search](grid-search.md) - [Transformers](transformers.md) - [K-nearest neighbor (KNN)](knn.md) +- [PyTorch Fundamentals](PyTorch_Fundamentals.md) From 71bf180dd5d15b1f4af8a622d076364d477a35c8 Mon Sep 17 00:00:00 2001 From: Krishna Kaushik <131583096+kRiShNa-429407@users.noreply.github.com> Date: Thu, 13 Jun 2024 09:24:26 +0530 Subject: [PATCH 3/6] Update PyTorch_Fundamentals.md --- .../machine-learning/PyTorch_Fundamentals.md | 171 +++++++----------- 1 file changed, 64 insertions(+), 107 deletions(-) diff --git a/contrib/machine-learning/PyTorch_Fundamentals.md b/contrib/machine-learning/PyTorch_Fundamentals.md index a504ffc..3248a08 100644 --- a/contrib/machine-learning/PyTorch_Fundamentals.md +++ b/contrib/machine-learning/PyTorch_Fundamentals.md @@ -5,9 +5,9 @@ # Import pytorch in our codespace import torch print(torch.__version__) -``` - 2.3.0+cu121 +output -> 2.3.0+cu121 +``` 2.3.0 is the pytorch version and 121 is the cuda version @@ -21,27 +21,26 @@ Now you have already seen how to create a tensor in pytorch. In this notebook i # Scalar tensor ( a zero dimension tensor) scalar = torch.tensor(7) print(scalar) -``` - tensor(7) +output -> tensor(7) +``` ```python # Check the dimension of the above tensor print(scalar.ndim) -``` - 0 - +output -> 0 +``` ```python # To retrieve the number from the tensor we use `item()` print(scalar.item()) -``` - 7 +output -> 7 +``` @@ -49,27 +48,25 @@ print(scalar.item()) # Vector (It is a single dimension tensor but contain many numbers) vector = torch.tensor([1,2]) print(vector) -``` - tensor([1, 2]) - +output -> tensor([1, 2]) +``` ```python # Check the dimensions print(vector.ndim) -``` - 1 - +output -> 1 +``` ```python # Check the shape of the vector print(vector.shape) -``` - torch.Size([2]) +output -> torch.Size([2]) +``` The above returns torch.Size([2]) which means our vector has a shape of [2]. This is because of the two elements we placed inside the square brackets ([1,2]) @@ -85,30 +82,27 @@ You can tell the number of dimensions a tensor in PyTorch has by the number of s MATRIX = torch.tensor([[1,2], [4,5]]) print(MATRIX) -``` - tensor([[1, 2], +output -> tensor([[1, 2], [4, 5]]) - +``` There are two brackets so it must be 2 dimensions , lets check ```python print(MATRIX.ndim) -``` - 2 - +output -> 2 +``` ```python # Shape print(MATRIX.shape) -``` - torch.Size([2, 2]) - +output -> torch.Size([2, 2]) +``` It means MATRIX has 2 rows and 2 columns. @@ -119,30 +113,27 @@ TENSOR = torch.tensor([[[1,2,3], [4,5,6], [7,8,9]]]) print(TENSOR) -``` - tensor([[[1, 2, 3], +output -> tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) - +``` ```python # Let's check the dimensions print(TENSOR.ndim) -``` - 3 - +output -> 3 +``` ```python # shape? print(TENSOR.shape) -``` - torch.Size([1, 3, 3]) - +output -> torch.Size([1, 3, 3]) +``` The dimensions go outer to inner. @@ -164,39 +155,35 @@ We can create them using `torch.rand()` and passing in the `size` parameter. # creating a random tensor of size (3,4) rand_tensor = torch.rand(size = (3,4)) print(rand_tensor) -``` - tensor([[0.7462, 0.4950, 0.7851, 0.8277], +output -> tensor([[0.7462, 0.4950, 0.7851, 0.8277], [0.6112, 0.5159, 0.1728, 0.6847], [0.4472, 0.1612, 0.6481, 0.3236]]) - +``` ```python # Check the dimensions print(rand_tensor.ndim) -``` - 2 - +output -> 2 +``` ```python # Shape print(rand_tensor.shape) -``` - torch.Size([3, 4]) - +output -> torch.Size([3, 4]) +``` ```python # datatype print(rand_tensor.dtype) -``` - torch.float32 - +output -> torch.float32 +``` ### Zeros and ones @@ -207,24 +194,22 @@ Here we will create a tensor of any shape filled with zeros and ones # Create a tensor of all zeros zeros = torch.zeros(size = (3,4)) print(zeros) -``` - tensor([[0., 0., 0., 0.], +output -> tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) - +``` ```python # create a tensor of ones ones = torch.ones(size = (3,4)) print(ones) -``` - tensor([[1., 1., 1., 1.], +output -> tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) - +``` ### Create a tensor having range of numbers @@ -244,10 +229,9 @@ zero_to_ten = torch.arange(start = 0, end = 10, step = 1) print(zero_to_ten) -``` - tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - +output -> tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) +``` # 2. Manipulating tensors (tensor operations) @@ -265,10 +249,9 @@ The operations are : ```python tensor = torch.tensor([1,2,3]) print(tensor+10) -``` - tensor([11, 12, 13]) - +output -> tensor([11, 12, 13]) +``` We have add 10 to each tensor element. @@ -276,10 +259,9 @@ We have add 10 to each tensor element. ```python tensor1 = torch.tensor([4,5,6]) print(tensor+tensor1) -``` - tensor([5, 7, 9]) - +output -> tensor([5, 7, 9]) +``` We have added two tensors , remember that addition takes place element wise. @@ -288,20 +270,18 @@ We have added two tensors , remember that addition takes place element wise. ```python print(tensor-8) -``` - tensor([-7, -6, -5]) - +output -> tensor([-7, -6, -5]) +``` We've subtracted 8 from the above tensor. ```python print(tensor-tensor1) -``` - tensor([-3, -3, -3]) - +output -> tensor([-3, -3, -3]) +``` ### 3. Multiplication @@ -309,10 +289,9 @@ print(tensor-tensor1) ```python # Multiply the tensor with 10 (element wise) print(tensor*10) -``` - tensor([10, 20, 30]) - +output -> tensor([10, 20, 30]) +``` Each element of tensor gets multiplied by 10. @@ -324,18 +303,16 @@ PyTorch also has a bunch of built-in functions like `torch.mul()` (short for mul ```python # let's see them print(torch.add(tensor,10)) -``` - tensor([11, 12, 13]) - +output -> tensor([11, 12, 13]) +``` ```python print(torch.mul(tensor,10)) -``` - tensor([10, 20, 30]) - +output -> tensor([10, 20, 30]) +``` ### Matrix multiplication (is all you need) One of the most common operations in machine learning and deep learning algorithms (like neural networks) is matrix multiplication. @@ -366,61 +343,41 @@ tensor2 = torch.tensor([[[1,1,1], [3,3,3]]]) print(tensor1) , print(tensor2) -``` - tensor([[[1, 2, 3], +output1 -> tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) - tensor([[[1, 1, 1], +output2 -> tensor([[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]) - - - - - - (None, None) - - - +``` ```python # let's check the shape print(tensor1.shape) , print(tensor2.shape) + +output1 -> torch.Size([1, 3, 3]) +output2 ->torch.Size([1, 3, 3]) ``` - torch.Size([1, 3, 3]) - torch.Size([1, 3, 3]) - - - - - - (None, None) - - - - ```python # Matrix multiplication print(torch.matmul(tensor1, tensor2)) -``` - tensor([[[14, 14, 14], +output -> tensor([[[14, 14, 14], [32, 32, 32], [50, 50, 50]]]) - +``` ```python # Can also use the "@" symbol for matrix multiplication, though not recommended print(tensor1 @ tensor2) -``` - tensor([[[14, 14, 14], +output -> tensor([[[14, 14, 14], [32, 32, 32], [50, 50, 50]]]) - +``` Note: From a6bc70dad9f70cfe0e8b66cc42184e63c195a566 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 18:52:17 +0530 Subject: [PATCH 4/6] Update PyTorch_Fundamentals.md --- .../machine-learning/PyTorch_Fundamentals.md | 217 ++++++++++++------ 1 file changed, 151 insertions(+), 66 deletions(-) diff --git a/contrib/machine-learning/PyTorch_Fundamentals.md b/contrib/machine-learning/PyTorch_Fundamentals.md index 3248a08..b244ec1 100644 --- a/contrib/machine-learning/PyTorch_Fundamentals.md +++ b/contrib/machine-learning/PyTorch_Fundamentals.md @@ -5,8 +5,11 @@ # Import pytorch in our codespace import torch print(torch.__version__) +``` -output -> 2.3.0+cu121 +#### Output +``` +2.3.0+cu121 ``` @@ -16,56 +19,72 @@ Now you have already seen how to create a tensor in pytorch. In this notebook i ### 1. Creating tensors +Scalar tensor ( a zero dimension tensor) ```python -# Scalar tensor ( a zero dimension tensor) scalar = torch.tensor(7) print(scalar) +``` -output -> tensor(7) +#### Output +``` +tensor(7) ``` - +Check the dimension of the above tensor ```python -# Check the dimension of the above tensor print(scalar.ndim) +``` -output -> 0 -``` - +#### Output +``` +0 +``` + +To retrieve the number from the tensor we use `item()` ```python -# To retrieve the number from the tensor we use `item()` print(scalar.item()) +``` -output -> 7 +#### Output +``` +7 ``` - +Vector (It is a single dimension tensor but contain many numbers) ```python -# Vector (It is a single dimension tensor but contain many numbers) vector = torch.tensor([1,2]) print(vector) +``` -output -> tensor([1, 2]) +#### Output +``` +tensor([1, 2]) ``` +Check the dimensions ```python -# Check the dimensions print(vector.ndim) +``` -output -> 1 +#### Output +``` +1 ``` +Check the shape of the vector ```python -# Check the shape of the vector print(vector.shape) +``` -output -> torch.Size([2]) +#### Output +``` +torch.Size([2]) ``` @@ -82,8 +101,11 @@ You can tell the number of dimensions a tensor in PyTorch has by the number of s MATRIX = torch.tensor([[1,2], [4,5]]) print(MATRIX) +``` -output -> tensor([[1, 2], +#### Output +``` +tensor([[1, 2], [4, 5]]) ``` @@ -92,47 +114,60 @@ There are two brackets so it must be 2 dimensions , lets check ```python print(MATRIX.ndim) +``` -output -> 2 +#### Output +``` +2 ``` ```python # Shape print(MATRIX.shape) +``` -output -> torch.Size([2, 2]) +#### Output +``` +torch.Size([2, 2]) ``` It means MATRIX has 2 rows and 2 columns. +Let's create a TENSOR ```python -# Let's create a TENSOR TENSOR = torch.tensor([[[1,2,3], [4,5,6], [7,8,9]]]) print(TENSOR) +``` -output -> tensor([[[1, 2, 3], +#### Output +``` +tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) ``` - +Let's check the dimensions ```python -# Let's check the dimensions print(TENSOR.ndim) +``` -output -> 3 +#### Output +``` +3 ``` - +shape ```python -# shape? print(TENSOR.shape) +``` -output -> torch.Size([1, 3, 3]) +#### Output +``` +torch.Size([1, 3, 3]) ``` The dimensions go outer to inner. @@ -150,39 +185,48 @@ That means there's 1 dimension of 3 by 3. We can create them using `torch.rand()` and passing in the `size` parameter. - -```python -# creating a random tensor of size (3,4) +Creating a random tensor of size (3,4) +```python rand_tensor = torch.rand(size = (3,4)) print(rand_tensor) +``` -output -> tensor([[0.7462, 0.4950, 0.7851, 0.8277], +#### Output +``` +tensor([[0.7462, 0.4950, 0.7851, 0.8277], [0.6112, 0.5159, 0.1728, 0.6847], [0.4472, 0.1612, 0.6481, 0.3236]]) ``` +Check the dimensions ```python -# Check the dimensions print(rand_tensor.ndim) +``` -output -> 2 +#### Output +``` +2 ``` - +Shape ```python -# Shape print(rand_tensor.shape) +``` -output -> torch.Size([3, 4]) +#### Output +``` +torch.Size([3, 4]) ``` - +Datatype ```python -# datatype print(rand_tensor.dtype) +``` -output -> torch.float32 +#### Output +``` +torch.float32 ``` ### Zeros and ones @@ -194,19 +238,24 @@ Here we will create a tensor of any shape filled with zeros and ones # Create a tensor of all zeros zeros = torch.zeros(size = (3,4)) print(zeros) +``` -output -> tensor([[0., 0., 0., 0.], +#### Output +``` +tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) ``` - -```python -# create a tensor of ones +Create a tensor of ones +```python ones = torch.ones(size = (3,4)) print(ones) +``` -output -> tensor([[1., 1., 1., 1.], +#### Output +``` +tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) ``` @@ -229,8 +278,11 @@ zero_to_ten = torch.arange(start = 0, end = 10, step = 1) print(zero_to_ten) +``` -output -> tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) +#### Output +``` +tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) ``` # 2. Manipulating tensors (tensor operations) @@ -249,8 +301,11 @@ The operations are : ```python tensor = torch.tensor([1,2,3]) print(tensor+10) +``` -output -> tensor([11, 12, 13]) +#### Output +``` +tensor([11, 12, 13]) ``` We have add 10 to each tensor element. @@ -259,8 +314,11 @@ We have add 10 to each tensor element. ```python tensor1 = torch.tensor([4,5,6]) print(tensor+tensor1) +``` -output -> tensor([5, 7, 9]) +#### Output +``` +tensor([5, 7, 9]) ``` We have added two tensors , remember that addition takes place element wise. @@ -270,8 +328,11 @@ We have added two tensors , remember that addition takes place element wise. ```python print(tensor-8) +``` -output -> tensor([-7, -6, -5]) +#### Output +``` +tensor([-7, -6, -5]) ``` We've subtracted 8 from the above tensor. @@ -279,8 +340,11 @@ We've subtracted 8 from the above tensor. ```python print(tensor-tensor1) +``` -output -> tensor([-3, -3, -3]) +#### Output +``` +tensor([-3, -3, -3]) ``` ### 3. Multiplication @@ -289,8 +353,11 @@ output -> tensor([-3, -3, -3]) ```python # Multiply the tensor with 10 (element wise) print(tensor*10) +``` -output -> tensor([10, 20, 30]) +#### Output +``` +tensor([10, 20, 30]) ``` Each element of tensor gets multiplied by 10. @@ -303,15 +370,21 @@ PyTorch also has a bunch of built-in functions like `torch.mul()` (short for mul ```python # let's see them print(torch.add(tensor,10)) +``` -output -> tensor([11, 12, 13]) +#### Output +``` +tensor([11, 12, 13]) ``` ```python print(torch.mul(tensor,10)) +``` -output -> tensor([10, 20, 30]) +#### Output +``` +tensor([10, 20, 30]) ``` ### Matrix multiplication (is all you need) @@ -344,37 +417,49 @@ tensor2 = torch.tensor([[[1,1,1], print(tensor1) , print(tensor2) -output1 -> tensor([[[1, 2, 3], +``` + +#### Output +``` +tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) -output2 -> tensor([[[1, 1, 1], +tensor([[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]) ``` +Let's check the shape ```python -# let's check the shape print(tensor1.shape) , print(tensor2.shape) - -output1 -> torch.Size([1, 3, 3]) -output2 ->torch.Size([1, 3, 3]) ``` -```python - # Matrix multiplication -print(torch.matmul(tensor1, tensor2)) +#### Output +``` +torch.Size([1, 3, 3]) +torch.Size([1, 3, 3]) +``` -output -> tensor([[[14, 14, 14], +Matrix multiplication +```python +print(torch.matmul(tensor1, tensor2)) +``` + +#### Output +``` +tensor([[[14, 14, 14], [32, 32, 32], [50, 50, 50]]]) ``` - +Can also use the "@" symbol for matrix multiplication, though not recommended ```python -# Can also use the "@" symbol for matrix multiplication, though not recommended print(tensor1 @ tensor2) +``` -output -> tensor([[[14, 14, 14], +#### Output +``` +tensor([[[14, 14, 14], [32, 32, 32], [50, 50, 50]]]) ``` From 2770cd082aadcbde21e7bdb94ebdec51d01e9b19 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 18:52:37 +0530 Subject: [PATCH 5/6] Rename PyTorch_Fundamentals.md to pytorch-fundamentals.md --- .../{PyTorch_Fundamentals.md => pytorch-fundamentals.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/machine-learning/{PyTorch_Fundamentals.md => pytorch-fundamentals.md} (100%) diff --git a/contrib/machine-learning/PyTorch_Fundamentals.md b/contrib/machine-learning/pytorch-fundamentals.md similarity index 100% rename from contrib/machine-learning/PyTorch_Fundamentals.md rename to contrib/machine-learning/pytorch-fundamentals.md From d09dd856613fa0c680407db69aa5d763c4113317 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 18:52:59 +0530 Subject: [PATCH 6/6] Update index.md --- contrib/machine-learning/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index b52371a..df47be8 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -20,4 +20,4 @@ - [Grid Search](grid-search.md) - [Transformers](transformers.md) - [K-nearest neighbor (KNN)](knn.md) -- [PyTorch Fundamentals](PyTorch_Fundamentals.md) +- [PyTorch Fundamentals](pytorch-fundamentals.md)