完全为了给自己地铁看书用 知识点就那点 也不商业 重复勿怪勿扰 谢谢..
1. 指定GPU编号
设置当前使用的GPU设备仅为0号设备,设备名称为 /gpu:0
:
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
设置当前使用的GPU设备为0, 1号两个设备,名称依次为 /gpu:0
、/gpu:1
: os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
,根据顺序表示优先使用0号设备,然后使用1号设备。
指定GPU的命令需要放在和神经网络相关的一系列操作的前面。
2. 查看模型每层输出详情
Keras有一个简洁的API来查看模型的每一层输出尺寸,这在调试网络时非常有用。现在在PyTorch中也可以实现这个功能。
使用很简单,如下用法:
input_size
是根据你自己的网络模型的输入尺寸进行设置。
https://github.com/sksq96/pytorch-summary
whaosoft aiot http://143ai.com
3. 梯度裁剪(Gradient Clipping)
nn.utils.clip_grad_norm_
的参数:
- parameters – 一个基于变量的迭代器,会进行梯度归一化
- max_norm – 梯度的最大范数
- norm_type – 规定范数的类型,默认为L2
知乎用户 @不椭的椭圆 提出:梯度裁剪在某些任务上会额外消耗大量的计算时间。
4. 扩展单张图片维度
因为在训练时的数据维度一般都是 (batch_size, c, h, w),而在测试时只输入一张图片,所以需要扩展维度,扩展维度有多个方法:
或
或(感谢知乎用户 @coldleaf 的补充)
tensor.unsqueeze(dim)
:扩展维度,dim指定扩展哪个维度。
tensor.squeeze(dim)
:去除dim指定的且size为1的维度,维度大于1时,squeeze()不起作用,不指定dim时,去除所有size为1的维度。
5. 独热编码
在PyTorch中使用交叉熵损失函数的时候会自动把label转化成onehot,所以不用手动转化,而使用MSE需要手动转化成onehot编码。
https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507/3
6. 防止验证模型时爆显存
验证模型时不需要求导,即不需要梯度计算,关闭autograd,可以提高速度,节约内存。如果不关闭可能会爆显存。
感谢别人提醒,我把 torch.cuda.empty_cache()
的使用原因更新一下。
这是原回答:
Pytorch 训练时无用的临时变量可能会越来越多,导致 out of memory ,可以使用下面语句来清理这些不需要的变量。
官网上的解释为:
Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU application and visible innvidia-smi.torch.cuda.empty_cache()
意思就是PyTorch的缓存分配器会事先分配一些固定的显存,即使实际上tensors并没有使用完这些显存,这些显存也不能被其他应用使用。这个分配过程由第一次CUDA内存访问触发的。
而 torch.cuda.empty_cache()
的作用就是释放缓存分配器当前持有的且未占用的缓存显存,以便这些显存可以被其他GPU应用程序中使用,并且通过 nvidia-smi
命令可见。注意使用此命令不会释放tensors占用的显存。
对于不用的数据变量,Pytorch 可以自动进行回收从而释放相应的显存。
更详细的优化可以看:
优化显存使用:
我的内容里有
显存利用问题:
https://oldpan.me/archives/pytorch-gpu-memory-usage-track
7. 学习率衰减
8. 冻结某些层的参数
参考:Pytorch 冻结预训练模型的某一层
https://www.zhihu.com/question/311095447/answer/589307812
在加载预训练模型的时候,我们有时想冻结前面几层,使其参数在训练过程中不发生变化。
我们需要先知道每一层的名字,通过如下代码打印:
假设前几层信息如下:
后面的True表示该层的参数可训练,然后我们定义一个要冻结的层的列表:
冻结方法如下:
冻结后我们再打印每层的信息:
可以看到前两层的weight和bias的requires_grad都为False,表示它们不可训练。
最后在定义优化器时,只对requires_grad为True的层的参数进行更新。
9. 对不同层使用不同学习率
我们对模型的不同层使用不同的学习率。
还是使用这个模型作为例子:
对 convolution1 和 convolution2 设置不同的学习率,首先将它们分开,即放到不同的列表里:
我们将模型划分为两部分,存放到一个列表里,每部分就对应上面的一个字典,在字典里设置不同的学习率。当这两部分有相同的其他参数时,就将该参数放到列表外面作为全局参数,如上面的`weight_decay`。
也可以在列表外设置一个全局学习率,当各部分字典里设置了局部学习率时,就使用该学习率,否则就使用列表外的全局学习率。