AI Pytorch - freecodecamp.org

source: https://www.freecodecamp.org/news/learn-pytorch-for-deep-learning-in-day/

Main elements

namedimensionapplication tensor
Scalar0 dimensionscalar = torch.tensor(7)
vector
single dimension tensor
vector = torch.tensor([7, 7])
can contain many numbers
matrixMatrices are as flexible as vectors, except they've got an extra dimensionMATRIX = torch.tensor([[7, 8],
[9, 10], [6.5, 7.2]])
Tensortensors can represent almost anythingTENSOR = torch.tensor([[[1, 2, 3], [3, 6, 9], [2, 4, 5]]])


 Attributes and methods

nametypeexampleresultexplanation
ndim
attribute
scalar.ndim0
checks dimension
vector.ndim1
MATRIX.ndim2
TENSOR.ndim3
itemmethodscalar.item()7retrieve the number from tensor
shape
attribute
vector.shapetorch.Size([2])
How the elements inside tensors are arranged
MATRIX.shapetorch.Size([3, 2])
TENSOR.shapetorch.Size([1, 3, 3])1 for overall group, 3 for 3 brackets,and 3 is elements in each brackets

Some operations

# 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 tensortensor([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


source:


for running pytorch code
jupyter docs:

Comments

Popular posts from this blog

Programming- AI, machine learning

Programming -- JS, C, C++, Python

links - Robotic