本文最后更新于 2025-11-28T10:27:30+08:00
深度学习学习记录 RethinkFun深度学习教程
Tensor张量 是一种多为数组 可用torch或者numpy创建,Tensor属性类似numpy数组 Tensor数据类型:整形,浮点型,布尔型
1 2 torch.tensor(data) np.ndarray(data)
Tensor的统计操作和切片索引:
1 2 3 torch.tensor([[1 , 2 , 3 ],[1 , 2 , 3 ]],dtype=torch.float32)print (t1.mean(dim=0 ))print (t1[1 ,0 ])
Tensor利用GPU加速计算
1 2 t1 = torch.randn((3 ,4 ),device="cuda" ) t1 = t1.to("cpu" )
pytorch梯度计算与线性回归
1 2 3 4 5 6 7 8 9 10 11 12 import torch x = torch.tensor(1.0 , requires_grad=True ) y = torch.tensor(1.0 , requires_grad=True ) v = 3 *x+4 *y u = torch.square(v) z = torch.log(u) z.backward() print ("x grad:" , x.grad)print ("y grad:" , y.grad)
输出结果为 x grad: tensor(0.8571) y grad: tensor(1.1429)
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 40 import torch device = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) inputs = torch.rand(100 , 3 ) weights = torch.tensor([[1.1 ], [2.2 ], [3.3 ]]) bias = torch.tensor(4.4 ) targets = inputs @ weights + bias + 0.1 *torch.randn(100 , 1 ) w = torch.rand((3 , 1 ), requires_grad=True , device=device) b = torch.rand((1 ,), requires_grad=True , device=device) inputs = inputs.to(device) targets = targets.to(device) epoch = 10000 lr = 0.003 for i in range (epoch): outputs = inputs @ w + b loss = torch.mean(torch.square(outputs - targets)) print ("loss:" , loss.item()) loss.backward() with torch.no_grad(): w -= lr * w.grad b -= lr * b.grad w.grad.zero_() b.grad.zero_()print ("训练后的权重 w:" , w)print ("训练后的偏置 b:" , b)
输出结果: 训练后的权重 w: tensor([[1.1682], [2.2172], [3.3188]], device=’cuda:0’, requires_grad=True) 训练后的偏置 b: tensor([4.3380], device=’cuda:0’, requires_grad=True)
tensorboard绘制loss曲线 首先安装tensorboard库 运行代码 命令行输入: tensorboard –logdir=保存的地址,之后打开localhost即可查看loss曲线
归一化 不同特征的取值范围不同,对loss函数的影响不同(梯度的影响不同),在共用学习率lr的情况下,会使得loss函数无法接近理想值。 归一化常用特征值/特征最大值,这样所有值都分布在[0,1]之间了(同样的也可标准化处理,即特征值-平均值/标准差,使数据分布在[-1,1]之间)
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 import torch device = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) inputs = torch.tensor([[2 , 1000 ], [3 , 2000 ], [2 , 500 ], [1 , 800 ], [4 , 3000 ]], dtype=torch.float , device=device) labels = torch.tensor([[19 ], [31 ], [14 ], [15 ], [43 ]], dtype=torch.float , device=device) inputs = inputs / torch.tensor([4 , 3000 ], device=device) w = torch.ones(2 , 1 , requires_grad=True , device=device) b = torch.ones(1 , requires_grad=True , device=device) epoch = 1000 lr = 0.5 for i in range (epoch): outputs = inputs @ w + b loss = torch.mean(torch.square(outputs - labels)) print ("loss" , loss.item()) loss.backward() print ("w.grad" , w.grad.tolist()) with torch.no_grad(): w -= w.grad * lr b -= b.grad * lr w.grad.zero_() b.grad.zero_()
输出结果可以发现loss函数很接近0,符合预设的函数特性