7 PyTorch functions for your next Machine Learning project | by Jimit Dholakia | Towards Data Science

PyTorch is a Machine Learning library with increasing popularity. In this article, we will explore seven functions available in PyTorch.
First, we will import PyTorch using import torch
torch.linspace is used to create a 1D equally spaced tensor between the values start and end . We can specify the size of the tensor with the steps parameters. The default is steps=100
Example-1:
torch.linspace(1, 10)Output:tensor([ 1.0000, 1.0909, 1.1818, 1.2727, 1.3636, 1.4545, 1.5455, 1.6364, 1.7273, 1.8182, 1.9091, 2.0000, 2.0909, 2.1818, 2.2727, 2.3636, 2.4545, 2.5455, 2.6364, 2.7273, 2.8182, 2.9091, 3.0000, 3.0909, 3.1818, 3.2727, 3.3636, 3.4545, 3.5455, 3.6364, 3.7273, 3.8182, 3.9091, 4.0000, 4.0909, 4.1818, 4.2727, 4.3636, 4.4545, 4.5455, 4.6364, 4.7273, 4.8182, 4.9091, 5.0000, 5.0909, 5.1818, 5.2727, 5.3636, 5.4545, 5.5455, 5.6364, 5.7273, 5.8182, 5.9091, 6.0000, 6.0909, 6.1818, 6.2727, 6.3636, 6.4545, 6.5455, 6.6364, 6.7273, 6.8182, 6.9091, 7.0000, 7.0909, 7.1818, 7.2727, 7.3636, 7.4545, 7.5455, 7.6364, 7.7273, 7.8182, 7.9091, 8.0000, 8.0909, 8.1818, 8.2727, 8.3636, 8.4545, 8.5455, 8.6364, 8.7273, 8.8182, 8.9091, 9.0000, 9.0909, 9.1818, 9.2727, 9.3636, 9.4545, 9.5455, 9.6364, 9.7273, 9.8182, 9.9091, 10.0000])
Example-2:
torch.linspace(start=1, end=10, steps=5)Output:tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])
torch.eye returns a 2D tensor with the values of diagonals as 1 and other values as 0The function expects two parameters — n and m . If m is not specified, then it returns a 2D tensor of size nxn
Example-1:
torch.eye(n=4, m=5)Output:tensor([[1., 0., 0., 0., 0.],        [0., 1., 0., 0., 0.],        [0., 0., 1., 0., 0.],        [0., 0., 0., 1., 0.]])
Example-2:
torch.eye(n=3)Output:tensor([[1., 0., 0.],        [0., 1., 0.],        [0., 0., 1.]])
torch.full returns a tensor of size size with the values filled with fill_value The size can be a list or a tuple.
Example-1:
torch.full(size=(3,2), fill_value=10)Output:tensor([[10., 10.],        [10., 10.],        [10., 10.]])
Example-2:
torch.full(size=[2, 3, 4], fill_value=5)Output:tensor([[[5., 5., 5., 5.],         [5., 5., 5., 5.],         [5., 5., 5., 5.]],[[5., 5., 5., 5.],         [5., 5., 5., 5.],         [5., 5., 5., 5.]]])
torch.cat concatenates a sequence of tensors over the specified dimension dim. All the tensors must be of the same shape
Example-1:
a = torch.ones(3,2)b = torch.zeros(3,2)torch.cat((a, b)) # default dim=0Output:tensor([[1., 1.],        [1., 1.],        [1., 1.],        [0., 0.],        [0., 0.],        [0., 0.]])
Example-2:
x = torch.full((3,3), fill_value=4)y = torch.full((3,3), fill_value=7)torch.cat((x, y), dim=1)Output:tensor([[4., 4., 4., 7., 7., 7.],        [4., 4., 4., 7., 7., 7.],        [4., 4., 4., 7., 7., 7.]])
torch.take returns a tensor with the elements of the input tensors at the given indices. The input tensor is treated as a 1D tensor to return the values.
Example-1:
# 1D input Tensorb = torch.tensor([10, 20, 30, 40, 50])torch.take(b, torch.tensor([2]))Output:tensor([30])
Example-2:
# 2D input tensora = torch.tensor([[1, 2, 3],                  [4, 5, 6]])torch.take(a, torch.tensor([3,4]))Output:tensor([4, 5])
torch.unbind removes a tensor dimension along the given dimension dim The default dimension is 0 i.e. dim=0
Example-1:
a = torch.tensor([[1, 2, 3],                  [4, 5, 6]])torch.unbind(a)Output:(tensor([1, 2, 3]), tensor([4, 5, 6]))
Example-2:
a = torch.tensor([[1, 2, 3],                  [4, 5, 6]])torch.unbind(a, dim=1)Output:(tensor([1, 4]), tensor([2, 5]), tensor([3, 6]))
torch.Tensor.clone returns a copy of the tensor with the same size and data type.
When we create a copy of the tensor using x=y , changing one variable also affects the other variable since it points to the same memory location.
For example,
a = torch.tensor([[1., 2.],                  [3., 4.],                  [5., 6.]])b = aa[1,0]=9bOutput:tensor([[1., 2.],        [9., 4.],        [5., 6.]])
To avoid this, we can create a deepcopy of the tensor using .clone method.
Example:
a = torch.tensor([[1., 2.],                  [3., 4.],                  [5., 6.]])b = a.clone()a[1,0]=9bOutput:tensor([[1., 2.],        [3., 4.],        [5., 6.]])
In this article, we saw the working of seven functions available in PyTorch. Hope this article helped you understand these function. There are a number of other useful functions. You can refer to the official documentation for the complete list of available functions.
https://pytorch.org/docs/stable/torch.html
https://pytorch.org/docs/stable/tensors.html
The code snippets used in this article are available on my GitHub Page.
LinkedIn: https://www.linkedin.com/in/jimit105/GitHub: https://github.com/jimit105Twitter: https://twitter.com/jimit105
Some rights reserved