
pytorch
文章平均质量分 55
pytorch模型书写常用操作集
皮皮AI记
硕士,AI算法工程师。曾获华为杯中国研究生数学建模竞赛一等奖,省优秀毕业生等。写过几篇中文核心,发过几篇国际会议,还有几个授权专利。喜欢搞搞代码,写写博客,没事还会搞搞LaTeX。愿景:用数学和代码构建智能系统,用逻辑创造价值。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【深度学习】学习率预热和学习率衰减 (learning rate warmup & decay)
当然,这种使用warmup和decay的learning rate schedule大多是在bert这种预训练的大模型的微调应用中遇见的。如果是做自然语言处理相关任务的,transformers已经封装了好几个带有warmup 和 decay的lr schedule。如果不是做研究的话,这些已经封装的lr schedule直接拿来用即可。当然也可以使用pytorch中的相关模块自定义。原创 2022-12-10 09:54:02 · 6645 阅读 · 0 评论 -
【Pytorch】加载模型避坑坑load_state_dict中的strict使用与加载多GPU训练的模型
加载模型的小知识, 使用多GPU训练的模型并保存到ckpt中后,使用torch.load_state_dict加载模型的时候将会报错,但是如果将其中的参数设置为True的时候就可以加载,但是当使用加载后的模型去预测数据时,结果错的离谱。 相关内容可以看看这篇博文:关于Pytorch加载模型参数的避坑指南.那么对应的解决方案是,在使用多GPU训练保存模型的时候,保存的模型应该是model.module,并不是直接保存model。(假设使用的模型是model)原创 2022-12-04 21:49:52 · 1826 阅读 · 0 评论 -
【Pytorch】模型的可复现性
在做研究的时候,通常我们希望同样的样本,同样的代码能够得到同样的实验效果,但由于代码中存在一些随机性,导致虽然是同样的样本和程序,但是得到的结果不一致。在pytorch的官方文档中为此提供了一些建议,原文档:REPRODUCIBILITY。下面我们来看看看具体的内容。pytorch在一些操作具有随机性,如:,我们可以使用设置随机数种子来使得所有的设备(CPU和GPU)的随机性一致(本质来说现有的随机函数都是伪随机,都是通过随机数种子确定)。如:python中的随机性当然有时候我们的程序中可能还会使用py原创 2022-12-03 22:55:51 · 1896 阅读 · 1 评论 -
【Pytorch】取反操作符~在Pytorch和python中使用的比较
python中的取反操作在python中取反操作符~是对数字的二进制位进行取反,然后返回取反结果对应的十进制数字。这个可以参考:Python3运算符使用案例如下:num = 60# 查看二进制print(num, bin(num)) # 60 0b111100num_ = ~num # -(x + 1) print(num_, bin(num_)) # -61 -0b111101细心的会发现,经过~处理后的结果并不是对应位置为相反关系。这就需要了解计算机是如何存储负数的了,负数通常是原创 2022-05-03 15:51:09 · 5033 阅读 · 0 评论 -
【Pytorch】torch.Tensor.masked_fill()
官方文档:https://pytorch.org/docs/stable/generated/torch.Tensor.masked_fill.html?highlight=torch%20masked_fill#torch.Tensor.masked_fill该张量方法使用方法与torch.Tensor.masked_fill_()类似,其官方文档:https://pytorch.org/docs/stable/generated/torch.Tensor.masked_fill_.html#torch原创 2022-05-03 14:09:26 · 1526 阅读 · 0 评论 -
【Pytorch】torch.nn.Conv1d()理解与使用
官方文档:https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html?highlight=nn%20conv1d#torch.nn.Conv1d作用:对由多个输入平面组成的输入信号应用一维卷积。在最简单的情况下,输入大小为 (N,Cin,L)(N, C_{\text{in}}, L)(N,Cin,L) 和输出$ (N, C_{\text{out}}, L_{\text{out}})$可以精确地描述为:out(Ni,Coutj)=bia原创 2022-04-30 18:07:30 · 2682 阅读 · 0 评论 -
【Pytorch】torch.permute()使用
官方文档:https://pytorch.org/docs/stable/generated/torch.permute.html?highlight=torch%20permute#torch.permute形式:torch.permute(input, dims) → Tensor作用:返回原始张量输入的视图,其尺寸已置换。参数:input (Tensor) – the input tensor.dims (tuple of python:ints) – The desired orderi原创 2022-04-29 19:17:00 · 1332 阅读 · 0 评论 -
【Pytorch】torch.cat()使用
官方文档:https://pytorch.org/docs/stable/generated/torch.cat.html?highlight=torch%20cat#torch.cat形式:``torch.cat(tensors, dim=0, *, out=None) → Tensor`作用:连接给定维度中给定的 seq 张量序列。所有张量必须具有相同的形状(连接维度除外)或为空。torch.cat() 可以看作是 torch.split() 和 torch.chunk() 的逆运算。参数:原创 2022-04-29 19:10:37 · 606 阅读 · 0 评论 -
【Pytorch】torch.Tensor.view()使用
官方文档:https://pytorch.org/docs/stable/generated/torch.Tensor.view.html?highlight=torch%20view#torch.Tensor.view形式:Tensor.view(*shape) → Tensor作用:返回一个新的张量,其数据与自张量相同,但形状不同。说明:返回的张量共享相同的数据,并且必须具有相同数量的元素,但可能具有不同的大小。当不清楚是否可以执行 view() 时,建议使用 reshape(),如果形状兼容则原创 2022-04-27 23:51:46 · 1816 阅读 · 0 评论 -
【Pytorch】torch.Tensor.expand_as()与torch.Tensor.expand()使用与比较
torch.Tensor.expand_as官方文档地址:https://pytorch.org/docs/stable/generated/torch.Tensor.expand_as.html?highlight=expand_as#torch.Tensor.expand_astorch.Tensor.expand()官方文档地址:https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html#torch.Tensor.expand原创 2022-04-24 23:43:26 · 555 阅读 · 0 评论 -
【Pytorch】torch.index_select()方法使用
官方文档地址:https://pytorch.org/docs/stable/generated/torch.index_select.html?highlight=torch%20index_select#torch.index_select形式:torch.index_select(input, dim, index, *, out=None) → Tensor作用:返回一个新张量,该张量使用索引中的条目沿维度 dim 索引( LongTensor)输入张量。返回的张量与原始张量(输入)具有相同的原创 2022-04-23 21:47:11 · 2567 阅读 · 0 评论 -
【Pytorch】torch.sort()方法使用
官方文档地址:https://pytorch.org/docs/stable/generated/torch.sort.html?highlight=sort#torch.sort形式:torch.sort(input, dim=- 1, descending=False, stable=False, *, out=None)作用:沿给定维度按值升序对输入张量的元素进行排序。如果没有给出 dim,则选择输入的最后一个维度。如果descending 为True,则元素按值降序排序。如果 stable 为原创 2022-04-23 21:33:23 · 1973 阅读 · 0 评论 -
【Pytorch】Tensor.contiguous()使用与理解
官方文档地址:https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html?highlight=torch%20contiguous#torch.Tensor.contiguous形式:Tensor.contiguous(memory_format=torch.contiguous_format) → Tensor作用:返回一个连续的内存张量,其中包含与自张量相同的数据。如果 self tensor 已经是指定的内存格式,则原创 2022-04-19 07:53:59 · 1684 阅读 · 0 评论 -
【Pytorch】torch.bmm()方法使用
官方文档地址:https://pytorch.org/docs/stable/generated/torch.bmm.html?highlight=bmm#torch.bmm形式:torch.bmm(input, mat2, *, out=None) → Tensor作用:矩阵的批量相乘,支持TensorFloat32数据的操作。要求:input 和 mat2 必须是 3-D 张量,每个张量都包含相同数量的矩阵。如果input的维度是(b×n×m)(b\times n\times m)(b×n×m),原创 2022-04-18 23:45:39 · 1705 阅读 · 0 评论