我们讲的多维数组是一个计算机的概念,多维数组是一个纯计算机的语言,它和C++的数组一样。但线性代数,同样一个东西,但是它是在数学上的表达,所以它有数学上的意义,我们不需要太多的数学上的知识,我们这里还是稍微的讲一下,我们就简单的入门一下。
IN [1]: import torch
IN [2]: x = torch.tensor([3.0])
IN [3]: y = torch.tensor([2.0])
IN [4]: x+y,x*y,x/y,x**y
OUT [1]: (tensor([5.]), tensor([6.]), tensor([0.6667]), tensor([8.]))
IN [1]: import torch
IN [2]: x = torch.arange(4)
IN [3]: x
OUT [1]: tensor([0, 1, 2, 3])
IN [1]: import torch
IN [2]: a = torch.arange(4)
IN [3]: a[0],a[1],a[2],a[3]
OUT [1]: (tensor(0), tensor(1), tensor(2), tensor(3))
IN [1]: import torch
IN [2]: x = torch.arange(20).reshape(5,4) # 通过 reshape来生成一个 5行4列的矩阵
IN [3]: x
OUT [1]:tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
IN [4]:: x.T # x的转置矩阵
OUT [2]: tensor([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])
IN [1]: import torch
IN [2]: B = torch.tensor([[1,2,3],[2,0,4],[3,4,5]])
IN [3]: B == B.T
OUT [1]:
IN [1]: import torch
IN [2]: B = torch.arange(24).reshape(2,3,4)
OUT [1]:tensor([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])
IN [1]: import torch
IN [2]: A = torch.arange(20,dtype=torch.float32).reshape(5,4)
IN [3]: B = A.clone() # 通过分配新内存,将 A 的一个副本分配给 B
IN [4]: A,B,A+B
OUT [1]:(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.]]),
tensor([[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[16., 18., 20., 22.],
[24., 26., 28., 30.],
[32., 34., 36., 38.]]))
IN [1]: import torch
IN [2]: A = torch.arange(20,dtype=torch.float32).reshape(5,4)
IN [3]: B = A.clone() # 通过分配新内存,将 A 的一个副本分配给 B
IN [4]: A*B
OUT [1]:(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.]]),
tensor([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]]))
IN [1]: import torch
IN [2]: x = torch.arange(4,dtype=torch.float32)
IN [3]: x ,x.sum()
OUT [1]:(tensor([0., 1., 2., 3.]), tensor(6.))
IN [1]: import torch
IN [2]: A = torch.arange(40).reshape(2,5,4)
IN [3]: A.shape,A.sum(),A.mean()
OUT [1]:
IN [4]:A.mean(axis=0)
OUT [2]:(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]]]),
tensor(780))
IN [1]: import torch
IN [2]: A = torch.arange(40).reshape(2,5,4)
IN [3]: sum_A = A.sum(axis=1,keepdim=True)
IN [4]: sum_A,sum_A.shape
OUT [1]:(tensor([[[20, 22, 24, 26],
[28, 30, 32, 34],
[36, 38, 40, 42],
[44, 46, 48, 50],
[52, 54, 56, 58]]]),
torch.Size([1, 5, 4]))
IN [1]: import torch
IN [2]: A = torch.arange(40).reshape(2,5,4)
IN [3]: cumsum_A = A.cumsum(axis=0)
IN [4]: cumsum_A,cumsum_A.shape
OUT [1]:(tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]],
[[20, 22, 24, 26],
[28, 30, 32, 34],
[36, 38, 40, 42],
[44, 46, 48, 50],
[52, 54, 56, 58]]]),
torch.Size([2, 5, 4]))
IN [1]:import torch
IN [2]:x = torch.arange(4.0)
IN [3]:y = torch.ones(4,dtype=torch.float32)
IN [4]:z = torch.dot(x,y)
IN [5]:x,y,z
OUT [1]:(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))
IN [1]:import torch
IN [2]:x = torch.arange(20,dtype=torch.float32).reshape(5,4)
IN [3]:y = torch.ones(4)
IN [4]:z = torch.mv(x,y)
IN [5]:x,y,z
OUT [1]:(tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]]),
tensor([1., 1., 1., 1.]),
tensor([ 6., 22., 38., 54., 70.]))
IN [1]:import torch
IN [2]:m = torch.arange(20,dtype=torch.float32).reshape(5,4)
IN [3]:n = torch.ones(28,dtype=torch.float32).reshape(4,7)
IN [4]:l = torch.matmul(m,n)
IN [5]:l,l.shape
OUT [1]:(tensor([[ 6., 6., 6., 6., 6., 6., 6.],
[22., 22., 22., 22., 22., 22., 22.],
[38., 38., 38., 38., 38., 38., 38.],
[54., 54., 54., 54., 54., 54., 54.],
[70., 70., 70., 70., 70., 70., 70.]]),
torch.Size([5, 7]))
IN [1]: import torch
IN [2]: u = torch.tensor([3.0,-4.0])
IN [3]: torch.norm(u)
OUT [1]:tensor(5.)
IN [1]: import torch
IN [2]: u = torch.tensor([3.0,-4.0])
IN [3]: torch.abs(u).sum()
OUT [1]:tensor(7.)
IN [1]: import torch
IN [2]: torch.norm(torch.ones((4,9)))
OUT [1]:tensor(6.)
对于一个矩阵为 A = (2,5,4)维来说:
IN [1]:a= torch.ones((2,5,4))
IN [1]:a.shape,a.sum(axis=0).shape,a.sum(axis=1).shape,a.sum(axis=2).shape
OUT [1]:(torch.Size([2, 5, 4]),
torch.Size([5, 4]),
torch.Size([2, 4]),
torch.Size([2, 5])))
对于一个矩阵为 A = (2,5,4)维来说,要满足 keepdims = True来分析如下:
IN [1]:a= torch.ones((2,5,4))
IN[1]:a.shape,a.sum(axis=0,keepdims=True).shape,a.sum(axis=1,keepdims=True).shape,a.sum(axis=2,keepdims=True).shape
OUT [1]:(torch.Size([2, 5, 4]),
torch.Size([1, 5, 4]),
torch.Size([2, 1, 4]),
torch.Size([2, 5, 1]))