2.3.1 Scalars 1 2 3 4 5 import torchx = 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
tensor([0, 1, 2])
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
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.]]))
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
(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 ()
(tensor([0., 1., 2.]), tensor(3.))
(torch.Size([2, 3]), tensor(15.))
1 2 A.shape, A.sum (axis=0 ), A.sum (axis=0 ).shape
(torch.Size([2, 3]), tensor([3., 5., 7.]), torch.Size([3]))
1 2 A.sum ([0 , 1 ]) == A.sum ()
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 ]
(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
tensor([[ 3.],
[12.]])
tensor([[0.0000, 0.3333, 0.6667],
[0.2500, 0.3333, 0.4167]])
tensor([[0., 1., 2.],
[3., 5., 7.]])
2.3.7 dot production 1 2 3 y = torch.ones(3 , dtype = torch.float32) x, y, torch.dot(x, y)
(tensor([0., 1., 2.]), tensor([1., 1., 1.]), tensor(3.))
tensor(3.)
2.3.8 Matrix-vector production 1 2 A, A.shape, x.shape, torch.mv(A, x)
(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)
tensor(5.)
tensor(7.)
1 torch.norm(torch.ones((4 , 9 )))
tensor(6.)
2.3 Homework
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 )
(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 ))
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.]])