Pytorch Tensors - Operations

[This Tutorial is under development....... Please don't use it right now]

Multiply all elements in a tensor with a given number

import torch

x = torch.tensor([[1,2,3,4,5], [6,7,8,9,10]])
print(x * 5)

Output:

tensor([[ 5, 10, 15, 20, 25],
        [30, 35, 40, 45, 50]])

Add a given number to all elements of a tensor

import torch

x = torch.tensor([[1,2,3,4,5], [6,7,8,9,10]])
print(x.add(10))

Output

tensor([[11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20]])

Changing the shape of a tensor

(a) using view function

import torch

x = torch.tensor([[1,2,3,4,5], [6,7,8,9,10]])
print(x)
print(x.shape)
y = x.view(1,10)
print(y)
print(y.shape)

Output:

tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])
torch.Size([2, 5])
tensor([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]])
torch.Size([1, 10])

Matrix multiplication of tensor

# Matrix multiplication for two tensors
import torch
x = torch.tensor([[1,2,3],[4,5,6]]) # a tensor of shape 2X3
y = torch.tensor([[1,2],[3,4],[5,6]]) # a tensor of shape 3X2

z = torch.matmul(x,y)
print("z = ", z)
print("Shape of z: ", z.shape)

Output:

z =  tensor([[22, 28],
        [49, 64]])
Shape of z:  torch.Size([2, 2])

We can also use @ symbol for matrix multiplication

# Matrix multiplication for two tensors
import torch
x = torch.tensor([[1,2,3],[4,5,6]]) # a tensor of shape 2X3
y = torch.tensor([[1,2],[3,4],[5,6]]) # a tensor of shape 3X2

z = x@y
print("z = ", z)
print("Shape of z: ", z.shape)

Output:

z =  tensor([[22, 28],
        [49, 64]])
Shape of z:  torch.Size([2, 2])

Concatenate Tensors

# concatenate tensor
import torch
x = torch.tensor([[1,1],[2,2]])
y = torch.tensor([[3,3],[4,4]])
print("x shape: ", x.shape)
print("y shape: ", y.shape)
z = torch.cat([x,y])
print("z shape: ", z.shape)
print("z = ", z)
z1 = torch.cat([x,y], axis=1)
print("z1 = ", z1)
z2 = torch.cat([x,y], axis=0)
print("z2 = ", z2)

Output:

x shape:  torch.Size([2, 2])
y shape:  torch.Size([2, 2])
z shape:  torch.Size([4, 2])
z =  tensor([[1, 1],
        [2, 2],
        [3, 3],
        [4, 4]])
z1 =  tensor([[1, 1, 3, 3],
        [2, 2, 4, 4]])
z2 =  tensor([[1, 1],
        [2, 2],
        [3, 3],
        [4, 4]])

as you can see if you don't provide any axis value (default value for axis is 0)

Find maximum and minimum value in tensor elements

# Finding maximum and minimum value of element in a tensor
import torch
x = torch.tensor([[1,2,3,4],[9,8,7,6]])

print(x.max())
print(x.min())
print(x.min(axis=0))
print(x.min(axis=1))

Output:

tensor(9)
tensor(1)
torch.return_types.min(
values=tensor([1, 2, 3, 4]),
indices=tensor([0, 0, 0, 0]))
torch.return_types.min(
values=tensor([1, 6]),
indices=tensor([0, 3]))

when using min with axis = 0 mean search minimum values across rows

Reshaping tensors

import torch
x = torch.rand(10)
print(x)
print(x.reshape(2,5))

Output:

tensor([0.8314, 0.3080, 0.2729, 0.3462, 0.1767, 0.1631, 0.5814, 0.7745, 0.7754,
        0.9908])
tensor([[0.8314, 0.3080, 0.2729, 0.3462, 0.1767],
        [0.1631, 0.5814, 0.7745, 0.7754, 0.9908]])

It is very much un necessary to include all usuages of all operations available in PyTorch on tensors, basically if you have already worked on numpy arrays then all operation in numpy arrays are someway available on pytorch tensors too

To see all available operations available in pytorch tensors run following code

import torch
print(dir(torch.Tensor))