import torch
x=torch.tensor([3.0])
y=torch.tensor([2.0])
x+y,x-y,x*y,x/y,x**y
(tensor([5.]), tensor([1.]), tensor([6.]), tensor([1.5000]), tensor([9.]))
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(5,4)
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, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]]
b=torch.tensor([[1,2,3],[2,0,4],[3,4,5]])
b
tensor([[1, 2, 3],
[2, 0, 4],
[3, 4, 5]])
b.T
tensor([[1, 2, 3],
[2, 0, 4],
[3, 4, 5]])
b==b.T
tensor([[True, True, True],
[True, True, True],
[True, True, True]])
c=torch.arange(24).reshape(2,3,4)
c
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
id(B)==id(A)
True
B=A.clone()
id(B)==id(A)
False
B
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)
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+x
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]]])
(a*x).shape
torch.Size([2, 3, 4])
x=torch.arange(24).reshape(2,3,4)
x.sum()
tensor(276)
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.))
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.shap
(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_sum_axis01=A.sum(axis=[0,1])
A_sum_axis01,A_sum_axis01.shape
(tensor([180., 190., 200., 210.]), torch.Size([4])
A.mean(),A.sum()/A.numel()
(tensor(19.5000), tensor(19.5000)
# 在某一个维度上求均值
A.mean(axis=1),A.sum(axis=1)/A.shape[1]
(tensor([[ 8., 9., 10., 11.],
[28., 29., 30., 31.]]),
tensor([[ 8., 9., 10., 11.],
[28., 29., 30., 31.]]))
sum_A=A.sum(axis=1,keepdims=True)
sum_A
tensor([[[ 40., 45., 50., 55.]],
[[140., 145., 150., 155.]]])
A.cumsum(axis=1)
tensor([[[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[ 12., 15., 18., 21.],
[ 24., 28., 32., 36.],
[ 40., 45., 50., 55.]],
[[ 20., 21., 22., 23.],
[ 44., 46., 48., 50.],
[ 72., 75., 78., 81.],
[104., 108., 112., 116.],
[140., 145., 150., 155.]]])
# 点积
y=torch.ones(4,dtype=torch.float32)
x=torch.ones(4,dtype=torch.float32)
x,y
(tensor([1., 1., 1., 1.]), tensor([1., 1., 1., 1.]))
torch.dot(x,y)
tensor(4.)
torch.sum(x*y)
tensor(4.)
# 点积是一维的dot
# mv是向量与矩阵的乘法
B=A.reshape(10,4)
B.shape
torch.Size([10, 4])
x.shape
torch.Size([4])
torch.mv(B,x)
tensor([ 6., 22., 38., 54., 70., 86., 102., 118., 134., 150.])
C=torch.ones(4,5)
C
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
torch.mm(B,C)
tensor([[ 6., 6., 6., 6., 6.],
[ 22., 22., 22., 22., 22.],
[ 38., 38., 38., 38., 38.],
[ 54., 54., 54., 54., 54.],
[ 70., 70., 70., 70., 70.],
[ 86., 86., 86., 86., 86.],
[102., 102., 102., 102., 102.],
[118., 118., 118., 118., 118.],
[134., 134., 134., 134., 134.],
[150., 150., 150., 150., 150.]])
# L2范数
A=torch.tensor([3.0,-4.0])
torch.norm(A)
tensor(5.)
# L1范数
torch.abs(A).sum()
tensor(7.)
# 矩阵的范数
torch.norm(torch.ones(4,9))
tensor(6.)