import numpy as np
import torch
import os
x = torch.arange(start=10, end=20, step=4)
y = torch.arange(start=5, end=15, step=4)
x.shape, y.shape
(torch.Size([3]), torch.Size([3]))
x
tensor([10, 14, 18])
y
tensor([ 5, 9, 13])
x + y
tensor([15, 23, 31])
x - y
tensor([5, 5, 5])
x * y #<< Element wise multiplication
tensor([ 50, 126, 234])
torch.dot(x, y)
tensor(410)
sum([ 50, 126, 234])
410
x / y #<< Element wise division
tensor([2.0000, 1.5556, 1.3846])
z = torch.ones(3)
z
tensor([1., 1., 1.])
z = z + x
z
tensor([11., 15., 19.])
## inplace addition
z.add_(x)
z
tensor([31., 43., 55.])
x.pow(2)
tensor([100, 196, 324])
x**2
tensor([100, 196, 324])
x > 0
tensor([True, True, True])
# Matrix multpltplication
A = torch.rand(size=(3,5))
B = torch.rand(size=(5,4))
A @ B
tensor([[1.0718, 1.8589, 2.2422, 0.8966], [0.6847, 1.3138, 1.1647, 0.5111], [1.0766, 1.7835, 1.9630, 0.9556]])
A.matmul(B)
tensor([[1.0718, 1.8589, 2.2422, 0.8966], [0.6847, 1.3138, 1.1647, 0.5111], [1.0766, 1.7835, 1.9630, 0.9556]])
X = torch.rand((3,3))
X.matrix_exp()
tensor([[2.7414, 2.0749, 1.5089], [2.2362, 3.0089, 1.1585], [2.8463, 2.8138, 2.9833]])
X.matrix_power(2)
tensor([[1.3818, 1.2499, 0.7981], [1.1508, 1.3000, 0.8949], [1.7752, 1.8222, 1.2387]])
# Broadcasting
A
tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647], [0.3658, 0.6612, 0.0644, 0.5575, 0.2324], [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])
A + 1
tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647], [1.3658, 1.6612, 1.0644, 1.5575, 1.2324], [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])
A - 1
tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353], [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676], [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])
A * 5
tensor([[2.7087, 2.4553, 3.7167, 2.3822, 3.8236], [1.8292, 3.3058, 0.3218, 2.7874, 1.1621], [0.3798, 4.8824, 3.4330, 2.6328, 4.1660]])
A.shape
torch.Size([3, 5])
C = torch.ones((1,5))
C
tensor([[1., 1., 1., 1., 1.]])
A - C
tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353], [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676], [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])
A + C
tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647], [1.3658, 1.6612, 1.0644, 1.5575, 1.2324], [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])
D = torch.ones((3, 1))
D
tensor([[1.], [1.], [1.]])
A - D
tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353], [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676], [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])
A + D
tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647], [1.3658, 1.6612, 1.0644, 1.5575, 1.2324], [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])
D
tensor([[1.], [1.], [1.]])
torch.sum(D)
tensor(3.)
torch.sum(A)
tensor(7.9974)
A
tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647], [0.3658, 0.6612, 0.0644, 0.5575, 0.2324], [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])
torch.max(A)
tensor(0.9765)
torch.min(A)
tensor(0.0644)
torch.min(A, dim=1)
torch.return_types.min( values=tensor([0.4764, 0.0644, 0.0760]), indices=tensor([3, 2, 0]))
torch.max(A, dim=1) # row wise max
torch.return_types.max( values=tensor([0.7647, 0.6612, 0.9765]), indices=tensor([4, 1, 1]))
torch.max(A, dim=0) # col wise max
torch.return_types.max( values=tensor([0.5417, 0.9765, 0.7433, 0.5575, 0.8332]), indices=tensor([0, 2, 0, 1, 2]))
torch.abs(-A)
tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647], [0.3658, 0.6612, 0.0644, 0.5575, 0.2324], [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])
A
tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647], [0.3658, 0.6612, 0.0644, 0.5575, 0.2324], [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])
torch.argmax(A)
tensor(11)
torch.argmax(A, dim=1) # row wise argmax
tensor([4, 1, 1])
torch.argmax(A, dim=0) # col wise argmax
tensor([0, 2, 0, 1, 2])
torch.argmin(A)
tensor(7)
torch.mean(A)
tensor(0.5332)
torch.mean(A, dim=1) # row wise mean
tensor([0.6035, 0.3763, 0.6198])
torch.mean(A, dim=0) # col wise mean
tensor([0.3278, 0.7096, 0.4981, 0.5202, 0.6101])
A
tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647], [0.3658, 0.6612, 0.0644, 0.5575, 0.2324], [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])
D = torch.ones(A.shape)
D
tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
torch.eq(A, D)
tensor([[False, False, False, False, False], [False, False, False, False, False], [False, False, False, False, False]])
torch.eq(D, D)
tensor([[True, True, True, True, True], [True, True, True, True, True], [True, True, True, True, True]])
torch.sort(A)
torch.return_types.sort( values=tensor([[0.4764, 0.4911, 0.5417, 0.7433, 0.7647], [0.0644, 0.2324, 0.3658, 0.5575, 0.6612], [0.0760, 0.5266, 0.6866, 0.8332, 0.9765]]), indices=tensor([[3, 1, 0, 2, 4], [2, 4, 0, 3, 1], [0, 3, 2, 4, 1]]))
torch.clamp(A, min=0.5)
tensor([[0.5417, 0.5000, 0.7433, 0.5000, 0.7647], [0.5000, 0.6612, 0.5000, 0.5575, 0.5000], [0.5000, 0.9765, 0.6866, 0.5266, 0.8332]])