线性代数

如果想看jupyter note效果的请点击​​github地址​

import torch 

x = 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.]))
x = torch.arange(4)
tensor([0, 1, 2, 3])
x[3]
tensor(3)
x[0]
tensor(0)
len(x)
4
x.shape
torch.Size([4])
A = torch.arange(20).reshape(5,4)
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]])
tensor([[1, 2, 3],
[2, 0, 4],
[3, 4, 5]])
B == B.T
tensor([[True, True, True],
[True, True, True],
[True, True, True]])
X = 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]]])
A = torch.arange(20,dtype=torch.float32).reshape(5,4)
B = A.clone()
A, A +
(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.,   4.,   9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]])
A
tensor([[ 0.,  1.,  2.,  3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
B
tensor([[ 0.,  1.,  2.,  3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
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(20*2).reshape(2,5,4)
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.shape
(tensor([[ 40,  45,  50,  55],
[140, 145, 150, 155]]),
torch.Size([2, 4]))
A.sum(axis=[0,1]).shape
torch.Size([4])
A=A.float()
A.mean(), A.sum() / A.numel()
(tensor(19.5000), tensor(19.5000))
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)
tensor([[[ 40.,  45.,  50.,  55.]],

[[140., 145., 150., 155.]]])
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]]])
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.cumsum(axis=0)
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.]]])

以上cumsum理解参考https://blog.csdn.net/banana1006034246/article/details/78841461

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.)

动手深度学习3月21日_线性代数

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

动手深度学习3月21日_.net_02

torch.abs(u).sum()
tensor(7.)

动手深度学习3月21日_深度学习_03

torch.norm(torch.ones(4,9))
tensor(6.)
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.]])

自动求导

import torch

x = torch.arange(4.0)
tensor([0., 1., 2., 3.])
x.requires_grad_(True)
x.grad
y = 2 * torch.dot(x,x)
tensor(28., grad_fn=<MulBackward0>)
y.backward()
x.grad
tensor([ 0.,  4.,  8., 12.])
x.grad == 4*x
tensor([True, True, True, True])
# 在默认情况下,Pytorch会积累梯度,我们需要清除之前的值
x.grad.zero_()
y = x.sum()
y.backward()
x.grad
tensor([1., 1., 1., 1.])
x.grad.zero_()
y = x*x
y.sum().backward()
x.grad
tensor([0., 2., 4., 6.])
x.grad.zero_()
y = x*x
u = y.detach()# u为常数
z = u*x

z.sum().backward()
x.grad ==
tensor([True, True, True, True])
x.grad.zero_()
y.sum().backward()
x.grad == 2*x
tensor([True, True, True, True])
def f(a):
b = a*2
while b.norm() < 1000:
b = b*2
if b.sum() >0:
c = b
else:
c = 100*b
return c
a = torch.randn(size=(),requires_grad=True)
d = f(a)
d.backward()

a.grad ==d/a
tensor(True)