如何阅读PyTorch文档及常见PyTorch错误

如何阅读PyTorch文档及常见PyTorch错误

文章目录

PyTorch文档查看https://pytorch.org/docs/stable/

python 复制代码
torch.nn -> 定义神经网络
torch.optim -> 优化算法
torch.utils.data -> 数据加载 dataset, dataloader类

阅读PyTorch文档示例

torch.max为例

有些函数对不同的输入有不同的行为

Parameters(位置参数):不需要指定参数的名称

Keyword Arguments(关键字参数):必须指定参数的名称

他们通过 * 隔开

带默认值的参数:有些参数有默认值(keepdim=False),所以传递这个参数的值是可选的

三种torch.max的不同输入

  1. 返回整个张量的最大值(torch.max(input) → Tensor)
python 复制代码
# 1. max of entire tensor (torch.max(input) → Tensor)
m = torch.max(x)
print(m)
  1. 沿一个维度的最大值 (torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor))

    python 复制代码
    # 2. max along a dimension (torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor))
    m, idx = torch.max(x,0)
    print(m)
    print(idx)

    位置参数可以不指定参数的名字,关键字参数必须指定参数名字。以 * 隔开,(位置参数 * 关键字参数)

    python 复制代码
    # 2-2 位置参数可以不指定参数的名字,关键字参数必须指定参数名字。以 * 隔开,(位置参数 * 关键字参数)
    m, idx = torch.max(input=x,dim=0)
    print(m)
    print(idx)

    python 复制代码
    # 2-3
    m, idx = torch.max(x,0,False)
    print(m)
    print(idx)
    
    # 2-4
    m, idx = torch.max(x,dim=0,keepdim=True)
    print(m)
    print(idx)
    
    # 2-5
    p = (m,idx)
    torch.max(x,0,False,out=p)
    print(p[0])
    print(p[1])

    位置参数可以不指定参数的名字,关键字参数必须指定参数名字。

  2. 两个张量上的选择最大的(torch.max(input, other, *, out=None) → Tensor)

    python 复制代码
    # 3. max(choose max) operators on two tensors (torch.max(input, other, *, out=None) → Tensor)
    t = torch.max(x,y)
    print(t)

常见Pytorch错误

Tensor在不同设备上

报错信息:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper_mm)

解决方案:将张量移动到GPU

python 复制代码
# 1. different device error (fixed)
x = torch.randn(5).to("cuda:0")
y = model(x)
print(y.shape)

维度不匹配

报错信息:RuntimeError: The size of tensor a (5) must match the size of tensor b (4) at non-singleton dimension 1

解决办法:张量的形状不正确,使用transpose,squeeze, unsqueeze来对齐尺寸

python 复制代码
# 2. mismatched dimensions error 1 (fixed by transpose)
y = y.transpose(0,1)
z = x + y
print(z.shape)

cuda内存不足

报错信息:RuntimeError: CUDA out of memory. Tried to allocate 7.27 GiB (GPU 0; 4.00 GiB total capacity; 8.67 GiB already allocated; 0 bytes free; 8.69 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

解决方法:数据的批量大小太大,无法装入GPU。减小批量大小。如果对数据进行迭代(batch size = 1),问题就会得到解决。你也可以使用DataLoader

python 复制代码
# 3. cuda out of memory error (fixed, but it might take some time to execute)
for d in data:
	out = resnet18(d.to("cuda:0").unsqueeze(0))
print(out.shape)

张量类型不匹配

报错信息:RuntimeError: expected scalar type Long but found Float

解决方法:标签张量类型必须是Long,将其转换为"long"以解决此问题

python 复制代码
# 4. mismatched tensor type (fixed)
labels = labels.long()
lossval = L(outs,labels)
print(lossval)

参考

torch.max --- PyTorch 2.4 documentation

Hongyi_Lee_dl_homeworks/Warmup/Pytorch_Tutorial_2.pdf at master · huaiyuechusan/Hongyi_Lee_dl_homeworks (github.com)

orial_2.pdf at master · huaiyuechusan/Hongyi_Lee_dl_homeworks (github.com)](https://github.com/huaiyuechusan/Hongyi_Lee_dl_homeworks/blob/master/Warmup/Pytorch_Tutorial_2.pdf)

相关推荐
大江东去浪淘尽千古风流人物2 分钟前
【cuVSLAM】GPU 加速、多相机、实时视觉/视觉惯性 SLAM设计优势
c++·人工智能·数码相机·ubuntu·计算机视觉·augmented reality
weixin_458580127 分钟前
如何在 Go 中直接将 AST 编译为可执行二进制文件?
jvm·数据库·python
Elastic 中国社区官方博客5 小时前
Elasticsearch:使用 Agent Builder 的 A2A 实现 - 开发者的圣诞颂歌
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
2301_816660215 小时前
PHP怎么处理Eloquent Attribute Inference属性推断_Laravel从数据自动推导类型【操作】
jvm·数据库·python
第一程序员5 小时前
数据工程 pipelines 实践
python·github
chools6 小时前
【AI超级智能体】快速搞懂工具调用Tool Calling 和 MCP协议
java·人工智能·学习·ai
知行合一。。。6 小时前
Python--05--面向对象(属性,方法)
android·开发语言·python
郝学胜-神的一滴6 小时前
深度学习必学:PyTorch 神经网络参数初始化全攻略(原理 + 代码 + 选择指南)
人工智能·pytorch·python·深度学习·神经网络·机器学习
leobertlan6 小时前
好玩系列:用20元实现快乐保存器
android·人工智能·算法
笨笨饿6 小时前
#58_万能函数的构造方法:ReLU函数
数据结构·人工智能·stm32·单片机·硬件工程·学习方法