08_14_线性代数实现

import torch
x = torch.arange(4)
x
tensor([0, 1, 2, 3])
x[3]
tensor(3)
len(x)
4
# 只有一个轴的张量,形状只有一个元素
x.shape
torch.Size([4])
A = torch.arange(20).reshape((4,5))
A
tensor([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]])
A.T
tensor([[ 0,  5, 10, 15],
        [ 1,  6, 11, 16],
        [ 2,  7, 12, 17],
        [ 3,  8, 13, 18],
        [ 4,  9, 14, 19]])
#对称矩阵的转置矩阵等于本身
B = torch.tensor([[1,2,3],[2,3,4],[3,4,5]])
B
tensor([[1, 2, 3],
        [2, 3, 4],
        [3, 4, 5]])
B == B.T
tensor([[True, True, True],
        [True, True, True],
        [True, True, True]])

A.reshape((2,3,4))#两层三行四列

X = torch.arange(24).reshape((2,3,4))#两层三行四列
X
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]]])
A = torch.arange(20,dtype = torch.float32).reshape(5,4)
A
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [12., 13., 14., 15.],
        [16., 17., 18., 19.]])
B = A.clone()#分配新内存,将A的一个副本分配给B
A,A + B
(tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]]),
 tensor([[ 0.,  2.,  4.,  6.],
         [ 8., 10., 12., 14.],
         [16., 18., 20., 22.],
         [24., 26., 28., 30.],
         [32., 34., 36., 38.]]))
A, B
(tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]]),
 tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]]))

两个矩阵的按元素乘法称为 哈达玛积(数学符号⊙)

A * B
tensor([[  0.,   1.,   4.,   9.],
        [ 16.,  25.,  36.,  49.],
        [ 64.,  81., 100., 121.],
        [144., 169., 196., 225.],
        [256., 289., 324., 361.]])
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]))
x = torch.arange(4,dtype = torch.float32)
x,x.sum()
(tensor([0., 1., 2., 3.]), tensor(6.))
A = torch.arange(40,dtype = torch.float32).reshape(2,5,4)
A
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.],
         [24., 25., 26., 27.],
         [28., 29., 30., 31.],
         [32., 33., 34., 35.],
         [36., 37., 38., 39.]]])
A.shape,A.sum()
(torch.Size([2, 5, 4]), tensor(780.))

三维:axis = 0(垂直求和),axis=1(纵向求和),axis=2(横向求和)

二维:axis = 0(纵向求和),axis=1(横向求和)

A_sum_axis0 = A.sum(axis=0)
A_sum_axis0,A_sum_axis0.shape
(tensor([[20., 22., 24., 26.],
         [28., 30., 32., 34.],
         [36., 38., 40., 42.],
         [44., 46., 48., 50.],
         [52., 54., 56., 58.]]),
 torch.Size([5, 4]))
A_sum_axis1 = A.sum(axis=1)
A_sum_axis1,A_sum_axis1.shape
(tensor([[ 40.,  45.,  50.,  55.],
         [140., 145., 150., 155.]]),
 torch.Size([2, 4]))
A_sum_axis2 = A.sum(axis=2)
A_sum_axis2,A_sum_axis2.shape
(tensor([[  6.,  22.,  38.,  54.,  70.],
         [ 86., 102., 118., 134., 150.]]),
 torch.Size([2, 5]))
A.mean(), A.sum() / A.numel()#A.numel(),求元素个数
(tensor(19.5000), tensor(19.5000))
A
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.],
         [24., 25., 26., 27.],
         [28., 29., 30., 31.],
         [32., 33., 34., 35.],
         [36., 37., 38., 39.]]])
A.mean(axis=0),  A.sum(axis=0) / A.shape[0]
(tensor([[10., 11., 12., 13.],
         [14., 15., 16., 17.],
         [18., 19., 20., 21.],
         [22., 23., 24., 25.],
         [26., 27., 28., 29.]]),
 tensor([[10., 11., 12., 13.],
         [14., 15., 16., 17.],
         [18., 19., 20., 21.],
         [22., 23., 24., 25.],
         [26., 27., 28., 29.]]))
sum_A = A.sum(axis=1,keepdims=True)
sum_A
tensor([[[ 40.,  45.,  50.,  55.]],

        [[140., 145., 150., 155.]]])
A / sum_A
tensor([[[0.0000, 0.0222, 0.0400, 0.0545],
         [0.1000, 0.1111, 0.1200, 0.1273],
         [0.2000, 0.2000, 0.2000, 0.2000],
         [0.3000, 0.2889, 0.2800, 0.2727],
         [0.4000, 0.3778, 0.3600, 0.3455]],

        [[0.1429, 0.1448, 0.1467, 0.1484],
         [0.1714, 0.1724, 0.1733, 0.1742],
         [0.2000, 0.2000, 0.2000, 0.2000],
         [0.2286, 0.2276, 0.2267, 0.2258],
         [0.2571, 0.2552, 0.2533, 0.2516]]])
y = torch.ones(4,dtype = torch.float32)
x,y,torch.dot(x,y)
#点积:相同位置元素乘积的和
(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))
torch.sum(x*y)
#也可对相同位置元素相乘后再求和
tensor(6.)
c = torch.arange(12,dtype = torch.float32).reshape(4,3)
d = torch.arange(3,dtype = torch.float32)
c,d
(tensor([[ 0.,  1.,  2.],
         [ 3.,  4.,  5.],
         [ 6.,  7.,  8.],
         [ 9., 10., 11.]]),
 tensor([0., 1., 2.]))
c.shape, d.shape
(torch.Size([4, 3]), torch.Size([3]))
torch.mv(c,d)
tensor([ 5., 14., 23., 32.])
e = torch.ones(3,3)
e
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
torch.mm(c,e)
tensor([[ 3.,  3.,  3.],
        [12., 12., 12.],
        [21., 21., 21.],
        [30., 30., 30.]])

L2范数是向量元素平方和的平方根

u = torch.tensor([3.0,-4.0])
torch.norm(u)
tensor(5.)

L1范数表示向量元素绝对值的和

torch.abs(u).sum()
tensor(7.)
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.]])

矩阵的 弗罗贝尼乌斯范数(F范数)是矩阵元素的平方和的平方根

torch.norm(torch.ones(4,9))
tensor(6.)
import torch

a = torch.ones((2,5,4))
a
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.],
         [1., 1., 1., 1.]]])
a.shape
torch.Size([2, 5, 4])
a.sum(axis=1).shape
torch.Size([2, 4])
a.sum(axis=[0,2]).shape
torch.Size([5])
a.sum(axis=1,keepdims=True).shape#纵向数变为1
torch.Size([2, 1, 4])
a.sum(axis=[0,2],keepdims=True).shape#垂直和横向变为1
torch.Size([1, 5, 1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值