kopia lustrzana https://github.com/animator/learn-python
Update PyTorch_Fundamentals.md
rodzic
03e9338a91
commit
71bf180dd5
|
@ -5,9 +5,9 @@
|
||||||
# Import pytorch in our codespace
|
# Import pytorch in our codespace
|
||||||
import torch
|
import torch
|
||||||
print(torch.__version__)
|
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
|
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 tensor ( a zero dimension tensor)
|
||||||
scalar = torch.tensor(7)
|
scalar = torch.tensor(7)
|
||||||
print(scalar)
|
print(scalar)
|
||||||
```
|
|
||||||
|
|
||||||
tensor(7)
|
output -> tensor(7)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Check the dimension of the above tensor
|
# Check the dimension of the above tensor
|
||||||
print(scalar.ndim)
|
print(scalar.ndim)
|
||||||
|
|
||||||
|
output -> 0
|
||||||
```
|
```
|
||||||
|
|
||||||
0
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# To retrieve the number from the tensor we use `item()`
|
# To retrieve the number from the tensor we use `item()`
|
||||||
print(scalar.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 (It is a single dimension tensor but contain many numbers)
|
||||||
vector = torch.tensor([1,2])
|
vector = torch.tensor([1,2])
|
||||||
print(vector)
|
print(vector)
|
||||||
|
|
||||||
|
output -> tensor([1, 2])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([1, 2])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Check the dimensions
|
# Check the dimensions
|
||||||
print(vector.ndim)
|
print(vector.ndim)
|
||||||
|
|
||||||
|
output -> 1
|
||||||
```
|
```
|
||||||
|
|
||||||
1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Check the shape of the vector
|
# Check the shape of the vector
|
||||||
print(vector.shape)
|
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])
|
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,31 +82,28 @@ You can tell the number of dimensions a tensor in PyTorch has by the number of s
|
||||||
MATRIX = torch.tensor([[1,2],
|
MATRIX = torch.tensor([[1,2],
|
||||||
[4,5]])
|
[4,5]])
|
||||||
print(MATRIX)
|
print(MATRIX)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[1, 2],
|
output -> tensor([[1, 2],
|
||||||
[4, 5]])
|
[4, 5]])
|
||||||
|
```
|
||||||
|
|
||||||
There are two brackets so it must be 2 dimensions , lets check
|
There are two brackets so it must be 2 dimensions , lets check
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
print(MATRIX.ndim)
|
print(MATRIX.ndim)
|
||||||
|
|
||||||
|
output -> 2
|
||||||
```
|
```
|
||||||
|
|
||||||
2
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Shape
|
# Shape
|
||||||
print(MATRIX.shape)
|
print(MATRIX.shape)
|
||||||
|
|
||||||
|
output -> torch.Size([2, 2])
|
||||||
```
|
```
|
||||||
|
|
||||||
torch.Size([2, 2])
|
|
||||||
|
|
||||||
|
|
||||||
It means MATRIX has 2 rows and 2 columns.
|
It means MATRIX has 2 rows and 2 columns.
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,31 +113,28 @@ TENSOR = torch.tensor([[[1,2,3],
|
||||||
[4,5,6],
|
[4,5,6],
|
||||||
[7,8,9]]])
|
[7,8,9]]])
|
||||||
print(TENSOR)
|
print(TENSOR)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[[1, 2, 3],
|
output -> tensor([[[1, 2, 3],
|
||||||
[4, 5, 6],
|
[4, 5, 6],
|
||||||
[7, 8, 9]]])
|
[7, 8, 9]]])
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Let's check the dimensions
|
# Let's check the dimensions
|
||||||
print(TENSOR.ndim)
|
print(TENSOR.ndim)
|
||||||
|
|
||||||
|
output -> 3
|
||||||
```
|
```
|
||||||
|
|
||||||
3
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# shape?
|
# shape?
|
||||||
print(TENSOR.shape)
|
print(TENSOR.shape)
|
||||||
|
|
||||||
|
output -> torch.Size([1, 3, 3])
|
||||||
```
|
```
|
||||||
|
|
||||||
torch.Size([1, 3, 3])
|
|
||||||
|
|
||||||
|
|
||||||
The dimensions go outer to inner.
|
The dimensions go outer to inner.
|
||||||
|
|
||||||
That means there's 1 dimension of 3 by 3.
|
That means there's 1 dimension of 3 by 3.
|
||||||
|
@ -164,40 +155,36 @@ We can create them using `torch.rand()` and passing in the `size` parameter.
|
||||||
# creating a random tensor of size (3,4)
|
# creating a random tensor of size (3,4)
|
||||||
rand_tensor = torch.rand(size = (3,4))
|
rand_tensor = torch.rand(size = (3,4))
|
||||||
print(rand_tensor)
|
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.6112, 0.5159, 0.1728, 0.6847],
|
||||||
[0.4472, 0.1612, 0.6481, 0.3236]])
|
[0.4472, 0.1612, 0.6481, 0.3236]])
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Check the dimensions
|
# Check the dimensions
|
||||||
print(rand_tensor.ndim)
|
print(rand_tensor.ndim)
|
||||||
|
|
||||||
|
output -> 2
|
||||||
```
|
```
|
||||||
|
|
||||||
2
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Shape
|
# Shape
|
||||||
print(rand_tensor.shape)
|
print(rand_tensor.shape)
|
||||||
|
|
||||||
|
output -> torch.Size([3, 4])
|
||||||
```
|
```
|
||||||
|
|
||||||
torch.Size([3, 4])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# datatype
|
# datatype
|
||||||
print(rand_tensor.dtype)
|
print(rand_tensor.dtype)
|
||||||
|
|
||||||
|
output -> torch.float32
|
||||||
```
|
```
|
||||||
|
|
||||||
torch.float32
|
|
||||||
|
|
||||||
|
|
||||||
### Zeros and ones
|
### Zeros and ones
|
||||||
|
|
||||||
Here we will create a tensor of any shape filled with zeros and ones
|
Here we will create a tensor of any shape filled with 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
|
# Create a tensor of all zeros
|
||||||
zeros = torch.zeros(size = (3,4))
|
zeros = torch.zeros(size = (3,4))
|
||||||
print(zeros)
|
print(zeros)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[0., 0., 0., 0.],
|
output -> tensor([[0., 0., 0., 0.],
|
||||||
[0., 0., 0., 0.],
|
[0., 0., 0., 0.],
|
||||||
[0., 0., 0., 0.]])
|
[0., 0., 0., 0.]])
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# create a tensor of ones
|
# create a tensor of ones
|
||||||
ones = torch.ones(size = (3,4))
|
ones = torch.ones(size = (3,4))
|
||||||
print(ones)
|
print(ones)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[1., 1., 1., 1.],
|
output -> tensor([[1., 1., 1., 1.],
|
||||||
[1., 1., 1., 1.],
|
[1., 1., 1., 1.],
|
||||||
[1., 1., 1., 1.]])
|
[1., 1., 1., 1.]])
|
||||||
|
```
|
||||||
|
|
||||||
### Create a tensor having range of numbers
|
### Create a tensor having range of numbers
|
||||||
|
|
||||||
|
@ -244,11 +229,10 @@ zero_to_ten = torch.arange(start = 0,
|
||||||
end = 10,
|
end = 10,
|
||||||
step = 1)
|
step = 1)
|
||||||
print(zero_to_ten)
|
print(zero_to_ten)
|
||||||
|
|
||||||
|
output -> tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
|
||||||
|
|
||||||
|
|
||||||
# 2. Manipulating tensors (tensor operations)
|
# 2. Manipulating tensors (tensor operations)
|
||||||
|
|
||||||
The operations are :
|
The operations are :
|
||||||
|
@ -265,22 +249,20 @@ The operations are :
|
||||||
```python
|
```python
|
||||||
tensor = torch.tensor([1,2,3])
|
tensor = torch.tensor([1,2,3])
|
||||||
print(tensor+10)
|
print(tensor+10)
|
||||||
|
|
||||||
|
output -> tensor([11, 12, 13])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([11, 12, 13])
|
|
||||||
|
|
||||||
|
|
||||||
We have add 10 to each tensor element.
|
We have add 10 to each tensor element.
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
tensor1 = torch.tensor([4,5,6])
|
tensor1 = torch.tensor([4,5,6])
|
||||||
print(tensor+tensor1)
|
print(tensor+tensor1)
|
||||||
|
|
||||||
|
output -> tensor([5, 7, 9])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([5, 7, 9])
|
|
||||||
|
|
||||||
|
|
||||||
We have added two tensors , remember that addition takes place element wise.
|
We have added two tensors , remember that addition takes place element wise.
|
||||||
|
|
||||||
### 2. Subtraction
|
### 2. Subtraction
|
||||||
|
@ -288,32 +270,29 @@ We have added two tensors , remember that addition takes place element wise.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
print(tensor-8)
|
print(tensor-8)
|
||||||
|
|
||||||
|
output -> tensor([-7, -6, -5])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([-7, -6, -5])
|
|
||||||
|
|
||||||
|
|
||||||
We've subtracted 8 from the above tensor.
|
We've subtracted 8 from the above tensor.
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
print(tensor-tensor1)
|
print(tensor-tensor1)
|
||||||
|
|
||||||
|
output -> tensor([-3, -3, -3])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([-3, -3, -3])
|
|
||||||
|
|
||||||
|
|
||||||
### 3. Multiplication
|
### 3. Multiplication
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Multiply the tensor with 10 (element wise)
|
# Multiply the tensor with 10 (element wise)
|
||||||
print(tensor*10)
|
print(tensor*10)
|
||||||
|
|
||||||
|
output -> tensor([10, 20, 30])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([10, 20, 30])
|
|
||||||
|
|
||||||
|
|
||||||
Each element of tensor gets multiplied by 10.
|
Each element of tensor gets multiplied by 10.
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
|
@ -324,19 +303,17 @@ PyTorch also has a bunch of built-in functions like `torch.mul()` (short for mul
|
||||||
```python
|
```python
|
||||||
# let's see them
|
# let's see them
|
||||||
print(torch.add(tensor,10))
|
print(torch.add(tensor,10))
|
||||||
|
|
||||||
|
output -> tensor([11, 12, 13])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([11, 12, 13])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
print(torch.mul(tensor,10))
|
print(torch.mul(tensor,10))
|
||||||
|
|
||||||
|
output -> tensor([10, 20, 30])
|
||||||
```
|
```
|
||||||
|
|
||||||
tensor([10, 20, 30])
|
|
||||||
|
|
||||||
|
|
||||||
### Matrix multiplication (is all you need)
|
### 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.
|
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]]])
|
[3,3,3]]])
|
||||||
|
|
||||||
print(tensor1) , print(tensor2)
|
print(tensor1) , print(tensor2)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[[1, 2, 3],
|
output1 -> tensor([[[1, 2, 3],
|
||||||
[4, 5, 6],
|
[4, 5, 6],
|
||||||
[7, 8, 9]]])
|
[7, 8, 9]]])
|
||||||
tensor([[[1, 1, 1],
|
output2 -> tensor([[[1, 1, 1],
|
||||||
[2, 2, 2],
|
[2, 2, 2],
|
||||||
[3, 3, 3]]])
|
[3, 3, 3]]])
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(None, None)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# let's check the shape
|
# let's check the shape
|
||||||
print(tensor1.shape) , print(tensor2.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
|
```python
|
||||||
# Matrix multiplication
|
# Matrix multiplication
|
||||||
print(torch.matmul(tensor1, tensor2))
|
print(torch.matmul(tensor1, tensor2))
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[[14, 14, 14],
|
output -> tensor([[[14, 14, 14],
|
||||||
[32, 32, 32],
|
[32, 32, 32],
|
||||||
[50, 50, 50]]])
|
[50, 50, 50]]])
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Can also use the "@" symbol for matrix multiplication, though not recommended
|
# Can also use the "@" symbol for matrix multiplication, though not recommended
|
||||||
print(tensor1 @ tensor2)
|
print(tensor1 @ tensor2)
|
||||||
```
|
|
||||||
|
|
||||||
tensor([[[14, 14, 14],
|
output -> tensor([[[14, 14, 14],
|
||||||
[32, 32, 32],
|
[32, 32, 32],
|
||||||
[50, 50, 50]]])
|
[50, 50, 50]]])
|
||||||
|
```
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue