如何阅读PyTorch文档及常见PyTorch错误
PyTorch文档查看https://pytorch.org/docs/stable/
torch.nn -> 定义神经网络
torch.optim -> 优化算法
torch.utils.data -> 数据加载 dataset, dataloader类
阅读PyTorch文档示例
以torch.max
为例
有些函数对不同的输入有不同的行为
Parameters(位置参数):不需要指定参数的名称
Keyword Arguments(关键字参数):必须指定参数的名称
他们通过 *
隔开
带默认值的参数:有些参数有默认值(keepdim=False),所以传递这个参数的值是可选的
三种torch.max
的不同输入
- 返回整个张量的最大值(
torch.max(input) → Tensor
)
# 1. max of entire tensor (torch.max(input) → Tensor)
m = torch.max(x)
print(m)
-
沿一个维度的最大值 (
torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor)
)# 2. max along a dimension (torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor)) m, idx = torch.max(x