AI Pytorch - freecodecamp.org
source: https://www.freecodecamp.org/news/learn-pytorch-for-deep-learning-in-day/
Main elements
Attributes and methods
Some operations
source:
for running pytorch code
jupyter docs:
source: https://www.freecodecamp.org/news/learn-pytorch-for-deep-learning-in-day/
Main elements
name | dimension | application tensor |
Scalar | 0 dimension | scalar = torch.tensor(7) |
vector | single dimension tensor | vector = torch.tensor([7, 7]) |
can contain many numbers | ||
matrix | Matrices are as flexible as vectors, except they've got an extra dimension | MATRIX = torch.tensor([[7, 8], [9, 10], [6.5, 7.2]]) |
Tensor | tensors can represent almost anything | TENSOR = torch.tensor([[[1, 2, 3], [3, 6, 9], [2, 4, 5]]]) |
Attributes and methods
name | type | example | result | explanation |
ndim | attribute | scalar.ndim | 0 | checks dimension |
vector.ndim | 1 | |||
MATRIX.ndim | 2 | |||
TENSOR.ndim | 3 | |||
item | method | scalar.item() | 7 | retrieve the number from tensor |
shape | attribute | vector.shape | torch.Size([2]) | How the elements inside tensors are arranged |
MATRIX.shape | torch.Size([3, 2]) | |||
TENSOR.shape | torch.Size([1, 3, 3]) | 1 for overall group, 3 for 3 brackets,and 3 is elements in each brackets |
# Create a random tensor of size (3, 4) random_tensor = torch.rand(size=(3, 4)) random_tensor, random_tensor.dtype | (tensor([[0.5949, 0.6433, 0.3059, 0.6293], [0.0861, 0.6876, 0.9790, 0.4856], [0.3752, 0.6620, 0.7464, 0.5335]]) | |
# Create a tensor of all zeros zeros = torch.zeros(size=(3, 4)) zeros, zeros.dtype | (tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]), torch.float32) | |
# Create a tensor of all ones ones = torch.ones(size=(3, 4)) ones, ones.dtype | (tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]), torch.float32) | |
# Create a range of values 0 to 10 zero_to_ten = torch.arange(start=0, end=10, step=1) zero_to_ten | range of numbers, such as 1 to 10 or 0 to 100. You can use torch.arange(start, end, step) to do so. | tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
ten_zeros = torch.zeros_like(input=zero_to_ten) | a tensor of all zeros with the same shape as a previous tensor | tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
float_16_tensor = torch.tensor([3.0, 6.0, 9.0], dtype=torch.float16) | data type float 16 |
Comments
Post a Comment