01. Cộng hai ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([4,5,6])
m3 = m1.add(m2)print(m3)
Kết quả:
tensor([5., 7., 9.])
02. Phép trừ hai ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([[4,5,6]])
m3 = m1 - m2
print(m3)
Kết quả:
tensor([[-3., -3., -3.]])
03. Tính định thức của ma trận
import torch
a = torch.det(torch.tensor([[1,2],[3,4]], dtype=torch.float))print(a)
Kết quả:
tensor(-2.0000)
03b. Tính định thức theo lô
import torch
v1 = torch.tensor([[[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],],[[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],],[[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],],[[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,22,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,2,3,4],[5,6,7,8],[9,10,11,12],[132,14,15,16]],[[1,22,3,34],[5,6,7,8],[9,10,11,12],[13,14,15,16]],],],], dtype=torch.float)
r = torch.det(v1)print(r)
Kết quả
tensor([[[-2.1863e-13, -2.1863e-13, -2.1863e-13, -2.1863e-13],
[-2.1863e-13, -2.1863e-13, -2.1863e-13, -2.1863e-13],
[-2.1863e-13, -2.1863e-13, -2.1863e-13, -2.1863e-13]],
04. Tích vô hướng của một số thực và một ma trận
import torch
m1 = torch.Tensor([1,2,3])
m3 =2* m1
print(m3)
Kết quả
tensor([2., 4., 6.])
05. Tích Hadamard 2 ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([4,5,6])
m3 = m1 * m2
print(m3)
Kết quả
tensor([ 4., 10., 18.])
06. Phép nhân 2 ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([4,5,6])
m3 = torch.mm(m1, m2)print(m3)
Kết quả: Báo lỗi, vì bạn đã nhân một ma trận cỡ 1 hàng x 3 cột, với một ma trận cỡ 1 hàng x 3 cột.
Làm lại:
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([[4],[5],[6]])
m3 = m1 @ m2
print(m3)
Kết quả:
tensor([32.])
Cách viết khác cho ra cùng kết quả
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([[4],[5],[6]])
m3 = torch.matmul(m1, m2)print(m3)
06b. Tích hai tensor
a là tensor có cỡ (2, 8, 3, 3), b là tensor có cỡ (2, 8, 3, 3). Tiến hành lấy tích có hướng của
import torch
a = torch.Tensor([[[[0.1273,0.4003,0.5100],[0.4068,0.6107,0.4355],[0.2000,0.7149,0.5809]],[[0.5049,0.8286,0.2377],[0.4336,0.2765,0.6109],[0.0359,0.1244,0.2754]],[[0.0340,0.5342,0.1360],[0.4816,0.4003,0.7100],[0.8665,0.1173,0.1755]],[[0.3083,0.7936,0.5195],[0.7717,0.4332,0.6206],[0.3938,0.8477,0.8037]],[[0.5840,0.2912,0.1452],[0.0955,0.0479,0.9875],[0.7230,0.6591,0.5492]],[[0.8198,0.3055,0.4255],[0.1708,0.3207,0.7335],[0.4327,0.2551,0.0708]],[[0.7762,0.6958,0.3824],[0.0487,0.2324,0.5327],[0.5467,0.0133,0.1373]],[[0.2700,0.0785,0.0835],[0.1330,0.3887,0.7558],[0.6703,0.7960,0.2316]]],[[[0.2350,0.6634,0.7015],[0.2464,0.5241,0.0652],[0.6536,0.8132,0.3734]],[[0.9519,0.0057,0.4618],[0.4059,0.0504,0.2812],[0.7715,0.2355,0.2237]],[[0.5579,0.1029,0.8661],[0.2733,0.7476,0.1380],[0.5006,0.2602,0.0637]],[[0.9428,0.4860,0.6974],[0.4670,0.6382,0.5778],[0.0380,0.2795,0.6046]],[[0.5932,0.0015,0.6910],[0.8167,0.6075,0.0331],[0.7983,0.6860,0.8179]],[[0.9906,0.2676,0.4514],[0.0903,0.7321,0.9494],[0.7395,0.5777,0.9800]],[[0.6021,0.3177,0.1025],[0.7278,0.1720,0.1299],[0.3512,0.5977,0.6376]],[[0.1890,0.6320,0.4504],[0.0765,0.5150,0.1238],[0.7199,0.0498,0.7593]]]])
b = torch.Tensor([[[[0.1273,0.4003,0.5100],[0.4068,0.6107,0.4355],[0.2000,0.7149,0.5809]],[[0.5049,0.8286,0.2377],[0.4336,0.2765,0.6109],[0.0359,0.1244,0.2754]],[[0.0340,0.5342,0.1360],[0.4816,0.4003,0.7100],[0.8665,0.1173,0.1755]],[[0.3083,0.7936,0.5195],[0.7717,0.4332,0.6206],[0.3938,0.8477,0.8037]],[[0.5840,0.2912,0.1452],[0.0955,0.0479,0.9875],[0.7230,0.6591,0.5492]],[[0.8198,0.3055,0.4255],[0.1708,0.3207,0.7335],[0.4327,0.2551,0.0708]],[[0.7762,0.6958,0.3824],[0.0487,0.2324,0.5327],[0.5467,0.0133,0.1373]],[[0.2700,0.0785,0.0835],[0.1330,0.3887,0.7558],[0.6703,0.7960,0.2316]]],[[[0.2350,0.6634,0.7015],[0.2464,0.5241,0.0652],[0.6536,0.8132,0.3734]],[[0.9519,0.0057,0.4618],[0.4059,0.0504,0.2812],[0.7715,0.2355,0.2237]],[[0.5579,0.1029,0.8661],[0.2733,0.7476,0.1380],[0.5006,0.2602,0.0637]],[[0.9428,0.4860,0.6974],[0.4670,0.6382,0.5778],[0.0380,0.2795,0.6046]],[[0.5932,0.0015,0.6910],[0.8167,0.6075,0.0331],[0.7983,0.6860,0.8179]],[[0.9906,0.2676,0.4514],[0.0903,0.7321,0.9494],[0.7395,0.5777,0.9800]],[[0.6021,0.3177,0.1025],[0.7278,0.1720,0.1299],[0.3512,0.5977,0.6376]],[[0.1890,0.6320,0.4504],[0.0765,0.5150,0.1238],[0.7199,0.0498,0.7593]]]])
c = a @ b
print(c)
Kết quả
tensor([[[[0.2810, 0.6600, 0.5355],
[0.3873, 0.8471, 0.7264],
[0.4325, 0.9319, 0.7508]],
[[0.6227, 0.6770, 0.6917],
[0.3607, 0.5117, 0.4402],
07. Tính hạng của ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([[4,5,6],[7,8,9]])
myrank = torch.matrix_rank(m2)print(myrank)
Kết quả:
tensor(2)
Lưu ý, với ma trận 1 hàng, hàm torch.matrix_rank(m1)
sẽ báo lỗi. Điều này trong lập trình với PyTorch là khác so với toán học.
08. Tìm ma trận nghịch đảo của một ma trận
import torch
m1 = torch.Tensor([1,2,3])
m2 = torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
m3 = torch.inverse(m2)print(m3)
Kết quả
tensor([[-2796203.0000,5592406.0000,-2796203.0000],[5592404.5000,-11184812.0000,5592406.5000],[-2796201.7500,5592406.0000,-2796203.2500]])
Kiểm tra việc sinh ma trận nghịch đảo, sau đó dùng ma trận ban đầu nhân với ma trận nghịch đảo để xem có ra ma trận đơn vị hay không?
import torch
m2 = torch.Tensor([[4,7],[2,6]])
m3 = torch.inverse(m2)
m4 = torch.Tensor([[0.6000,-0.7000],[-0.2000,0.4000]])
m5 = m2 @ m4
print(m5)
Kết quả:
tensor([[1.0000,0.0000],[0.0000,1.0000]])
09. Tính giá trị riêng của vector
Trong toán học, giá trị riêng của vector có một bộ các giá trị. Trong PyTorch, theo mặc định, chỉ một giá trị riêng vector được tính.
import torch
a = torch.diag(torch.tensor([1,2,3], dtype=torch.double))
e, v = torch.eig(a)# e, v = torch.eig(a, eigenvectors=True)
Kết quả
e
Out[3]:
tensor([[1., 0.],
[2., 0.],
[3., 0.]], dtype=torch.float64)
v
Out[4]: tensor([], dtype=torch.float64)
Tính eigenvector trả về Giá trị riêng có thực
import torch
a = torch.diag(torch.tensor([1,2,3], dtype=torch.double))# e, v = torch.eig(a)
e, v = torch.eig(a, eigenvectors=True)
Kết quả:
e
Out[3]:
tensor([[1., 0.],
[2., 0.],
[3., 0.]], dtype=torch.float64)
v
Out[4]:
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]], dtype=torch.float64)
10. Chéo hóa ma trận
import torch
a = torch.diag(torch.tensor([[1,2,0],[2,1,0],[0,0,3]], dtype=torch.double))
Kết quả
a
Out[3]: tensor([1., 1., 3.], dtype=torch.float64)
Source code: https://github.com/donhuvy/pytorch_linear.git
Nguồn: viblo.asia