2.3.1 Scalars

1
2
3
4
5
import torch
x = torch.tensor(3.0)
y = torch.tensor(2.0)

x + y, x * y, x / y, x ** y
(tensor(5.), tensor(6.), tensor(1.5000), tensor(9.))

2.3.2 Vectors

1
2
3
x = torch.arange(3)
x
#It will start from 0 to 2.
tensor([0, 1, 2])
1
2
3
4
x[2]
len(x)
x.shape
#foundamental operations.
torch.Size([3])

2.3.3 Matrices

1
2
A = torch.arange(6).reshape(3, 2)
A, A.T
(tensor([[0, 1],
         [2, 3],
         [4, 5]]),
 tensor([[0, 2, 4],
         [1, 3, 5]]))
1
2
3
A = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
A == A.T
#Using bool operation creates identity matrice.
tensor([[True, True, True],
        [True, True, True],
        [True, True, True]])

2.3.4 Tensors

1
torch.arange(24).reshape(2,3,4)
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])

2.3.5 Basic Properties of Tensor Arithmetic

1
2
3
A = torch.arange(6, dtype=torch.float32).reshape(2, 3)
B = A.clone()
A, A + B
(tensor([[0., 1., 2.],
         [3., 4., 5.]]),
 tensor([[ 0.,  2.,  4.],
         [ 6.,  8., 10.]]))
1
2
A * B
#multiply by element
tensor([[ 0.,  1.,  4.],
        [ 9., 16., 25.]])
1
2
3
4
a = 2
X = torch.arange(24).reshape(2,3,4)
a + X, (a * X).shape
#every elememt is added by 2
(tensor([[[ 2,  3,  4,  5],
          [ 6,  7,  8,  9],
          [10, 11, 12, 13]],

         [[14, 15, 16, 17],
          [18, 19, 20, 21],
          [22, 23, 24, 25]]]),
 torch.Size([2, 3, 4]))

2.3.6 Reduction

1
2
3
x = torch.arange(3, dtype=torch.float32)
x, x.sum()
#Note: after sum() we also get the tensor.
(tensor([0., 1., 2.]), tensor(3.))
1
A.shape, A.sum()
(torch.Size([2, 3]), tensor(15.))
1
2
A.shape, A.sum(axis=0), A.sum(axis=0).shape
#we sum the elements by column.
(torch.Size([2, 3]), tensor([3., 5., 7.]), torch.Size([3]))
1
2
A.sum([0, 1]) == A.sum()
#We add the result of previous axis=0 and axis=1
tensor(True)
1
A.mean(), A.sum() / A.numel()
(tensor(2.5000), tensor(2.5000))
1
2
3
A.mean(axis=0), A.sum(axis=0) / A.shape[0], A.shape[0]
#shape[0] represents the first number in [2,3] that is 2.
#axis=0 represents mean of every column, and the result is written as a row, namely row is the director.
(tensor([1.5000, 2.5000, 3.5000]), tensor([1.5000, 2.5000, 3.5000]), 2)

Reduction without reducing dimension

1
2
3
sum_A = A.sum(axis=1, keepdims = True)
sum_A
#still remain two-deminsion tensor, however the row tensor only has one element.
tensor([[ 3.],
        [12.]])
1
2
A / sum_A
#due to same dimension, we can use divide broadcasting.
tensor([[0.0000, 0.3333, 0.6667],
        [0.2500, 0.3333, 0.4167]])
1
2
A.cumsum(axis = 0)
#This is cumulation summation
tensor([[0., 1., 2.],
        [3., 5., 7.]])

2.3.7 dot production

1
2
3
y = torch.ones(3, dtype = torch.float32)
#this it to generate one by on addtion tensor
x, y, torch.dot(x, y)
(tensor([0., 1., 2.]), tensor([1., 1., 1.]), tensor(3.))
1
2
torch.sum(x * y)
#This also can be used to represent dot production
tensor(3.)

2.3.8 Matrix-vector production

1
2
A, A.shape, x.shape, torch.mv(A, x)
#the result should be column vector but represented by row vector.
(tensor([[0., 1., 2.],
         [3., 4., 5.]]),
 torch.Size([2, 3]),
 torch.Size([3]),
 tensor([ 5., 14.]))

2.3.9 matrix-matrix production

1
2
B = torch.ones(3, 4)
torch.mm(A, B)
tensor([[ 3.,  3.,  3.,  3.],
        [12., 12., 12., 12.]])

2.3.10 Norm

1
2
3
u = torch.tensor([3.0, -4.0])
torch.norm(u)
#This is L2 norm
tensor(5.)
1
2
torch.abs(u).sum()
#L1 norm
tensor(7.)
1
torch.norm(torch.ones((4, 9)))
tensor(6.)

2.3 Homework

1
A.T.T == A
tensor([[True, True, True],
        [True, True, True]])
1
2
3
4
A = torch.ones(3, dtype = torch.float32)
B = torch.arange(3, dtype = torch.float32)

A.T + B.T == (A + B).T
tensor([True, True, True])
1
2
3
A = torch.ones(2, 2)
A.T == A
A + A.T == A.T + A
tensor([[True, True],
        [True, True]])
1
2
A = torch.randn(24, dtype = torch.float32).reshape(2, 3, 4)
A, len(A), len(A[1]), len(A[1][1])
(tensor([[[-0.9297, -0.2415,  1.8647, -0.0205],
          [ 0.6223, -0.7209, -1.4938,  1.2582],
          [ 0.2043,  1.3976,  0.7981, -0.3822]],

         [[ 1.1671, -0.1538, -0.1677, -0.2786],
          [-1.4894, -0.8465,  0.1436, -0.0888],
          [-2.0312, -2.1697, -0.7590, -0.6403]]]),
 2,
 3,
 4)
1
2
3
4
A = torch.randn(24, dtype=torch.float32).reshape(2,3,4)
B = torch.tensor([[[1,2,3,4], [5,6,7,8], [9,10,11,12]], [[1,1,1,1], [1,1,1,1], [1,1,1,1]]])
#仔细理解!
B.shape, B.sum(axis=0), B.sum(axis = 1), B.sum(axis = 2)
(torch.Size([2, 3, 4]),
 tensor([[ 2,  3,  4,  5],
         [ 6,  7,  8,  9],
         [10, 11, 12, 13]]),
 tensor([[15, 18, 21, 24],
         [ 3,  3,  3,  3]]),
 tensor([[10, 26, 42],
         [ 4,  4,  4]]))
1
2
3
4
5
B.sum(axis=2), B.sum(axis=1), B.sum(axis=0)
#Notice: axis=0 is the exterior one, it combine the two matrix, so it is in the third dimension ,
#the dimension is 2 of 2,3,4
#and the output is 3*4
#the same reason, when we use axis=1, the column in each matrix will be summed, so output is 2*4
(tensor([[10, 26, 42],
         [ 4,  4,  4]]),
 tensor([[15, 18, 21, 24],
         [ 3,  3,  3,  3]]),
 tensor([[ 2,  3,  4,  5],
         [ 6,  7,  8,  9],
         [10, 11, 12, 13]]))
1
2
torch.norm(torch.ones(4, 9))
#this is 6, is all the elements' square's summation's root.
tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1., 1.]])