pytorch api文档目录 作者:马育民 • 2026-01-09 20:45 • 阅读:10004 # 创建张量 - [pytorch api文档:torch.tensor()](https://www.malaoshi.top/show_1GW2ZgyWcwQn.html "pytorch api文档:torch.tensor()") - [pytorch api文档:torch.ones()创建全1张量](https://www.malaoshi.top/show_1GW2crxxmuQS.html "pytorch api文档:torch.ones()创建全1张量") - [pytorch api文档:torch.zeros()创建全零张量](https://www.malaoshi.top/show_1GW2bn8nGC4P.html "pytorch api文档:torch.zeros()创建全零张量") - [pytorch api文档:torch.empty()建一个未初始化的张量](https://www.malaoshi.top/show_1GW2ao0Cqais.html "pytorch api文档:torch.empty()建一个未初始化的张量") - [pytorch api文档:torch.empty() 和 torch.zeros()、torch.ones() 区别](https://www.malaoshi.top/show_1GW2anwCHnKf.html "pytorch api文档:torch.empty() 和 torch.zeros()、torch.ones() 区别") - [pytorch api文档:torch.ones_like()复用已有张量的形状、数据类型、设备等属性来创建全1张量](https://www.malaoshi.top/show_1GW2crxBuP4N.html "pytorch api文档:torch.ones_like()复用已有张量的形状、数据类型、设备等属性来创建全1张量") - [pytorch api文档:torch.arange()](https://www.malaoshi.top/show_1GW2YuyIR0Xi.html "pytorch api文档:torch.arange()") - [pytorch api文档:torch.linspace()函数-生成均匀分布张量](https://www.malaoshi.top/show_1GW2eRAXuyNH.html "pytorch api文档:torch.linspace()函数-生成均匀分布张量") ### 随机 - [pytorch api文档:torch.rand()生成均匀分布随机张量【0, 1) 区间](https://www.malaoshi.top/show_1GW2c5aFcZOW.html "pytorch api文档:torch.rand()生成均匀分布随机张量[0, 1) 区间") - [pytorch api文档:torch.randn()生成标准正态分布随机张量](https://www.malaoshi.top/show_1GW2c5isoUYi.html "pytorch api文档:torch.randn()生成标准正态分布随机张量") - [pytorch api文档:torch.rand()和torch.randn()区别](https://www.malaoshi.top/show_1GW2c5isoUYi.html "pytorch api文档:torch.rand()和torch.randn()区别") - [pytorch api文档:torch.randint()生成整数型随机张量](https://www.malaoshi.top/show_1GW2c5w3kyYY.html "pytorch api文档:torch.randint()生成整数型随机张量") - [pytorch api文档:torch.randperm()生成无重复随机整数的一维张量](https://www.malaoshi.top/show_1GW2c67TYa8B.html "pytorch api文档:torch.randperm()生成无重复随机整数的一维张量") - [pytorch api文档:torch.manual_seed()设置随机种子](https://www.malaoshi.top/show_1GW2b3yc5S6M.html "pytorch api文档:torch.manual_seed()设置随机种子") # 张量的方法 - [pytorch api文档:张量的.bool()方法-将张量中的元素转换为布尔类型](https://www.malaoshi.top/show_1GW2cqDSt928.html "pytorch api文档:张量的.bool()方法-将张量中的元素转换为布尔类型") - [pytorch api文档:张量的.masked_fill()方法-根据布尔类型的张量填充](https://www.malaoshi.top/show_1GW2csCL1NDn.html "pytorch api文档:张量的.masked_fill()方法-根据布尔类型的张量填充") - [pytorch api文档:张量的 .masked\_fill\_()方法-根据布尔类型的张量填充](https://www.malaoshi.top/show_1GW2dK2sR135.html "pytorch api文档:张量的 .masked_fill_()方法-根据布尔类型的张量填充") - [pytorch api文档:张量的 .view()形状变换方法](https://www.malaoshi.top/show_1GW2dKE8Ksnx.html "pytorch api文档:张量的 .view()形状变换方法") - [pytorch api文档:张量的.view() 和 torch.transpose() 函数的区别](https://www.malaoshi.top/show_1GW2dKLmsG0U.html "pytorch api文档:张量的.view() 和 torch.transpose() 函数的区别") - [pytorch api文档:张量的 mean()方法-平均值](https://www.malaoshi.top/show_1GW2e4lIV1G6.html "pytorch api文档:张量的 mean()方法-平均值") - [pytorch api文档:张量的 var()方法-计算张量的方差](https://www.malaoshi.top/show_1GW2e5wg9yvA.html "pytorch api文档:张量的 var()方法-计算张量的方差") - [pytorch api文档:张量的 backward()方法-反向传播自动计算梯度(求导)](https://www.malaoshi.top/show_1GW2ehYk3VIf.html "pytorch api文档:张量的 backward()方法-反向传播自动计算梯度(求导)") - [pytorch api文档:张量的 .numel()方法-统计元素总数](https://www.malaoshi.top/show_1GW2eoQiKsTv.html "pytorch api文档:张量的 .numel()方法-统计元素总数") - [pytorch api文档:张量的 unsqueeze()方法-增加一个维度](https://www.malaoshi.top/show_1GW2es6wneix.html "pytorch api文档:张量的 unsqueeze()方法-增加一个维度") - [pytorch api文档:张量的 squeeze()方法-删除维度](https://www.malaoshi.top/show_1GW2esF8xpDO.html "pytorch api文档:张量的 squeeze()方法-删除维度") ### 连续张量、不连续张量 - [pytorch api文档:连续张量、非连续张量](https://www.malaoshi.top/show_1GW2dN6bwpOg.html "pytorch api文档:连续张量、非连续张量") - [pytorch api文档:张量的 .stride() 方法-查看步长,判断是否连续](https://www.malaoshi.top/show_1GW2dNDAEfI1.html "pytorch api文档:张量的 .stride() 方法-查看步长,判断是否连续") - [pytorch api文档:张量的.is_contiguous()方法-判断张量内存是否连续](https://www.malaoshi.top/show_1GW2dNEzcQx9.html "pytorch api文档:张量的.is_contiguous()方法-判断张量内存是否连续") - [pytorch api文档:张量的 .contiguous()方法-返回内存连续的新张量](https://www.malaoshi.top/show_1GW2dNGybasI.html "pytorch api文档:张量的 .contiguous()方法-返回内存连续的新张量") # 张量运算 - [pytorch api文档:torch.sum()求和](https://www.malaoshi.top/show_1GW2bkUOHCyr.html "pytorch api文档:torch.sum()求和") - [pytorch api文档:torch.pow()函数-幂运算](https://www.malaoshi.top/show_1GW2eRN8CfdJ.html "pytorch api文档:torch.pow()函数-幂运算") - [pytorch api文档:torch.exp()自然指数运算](https://www.malaoshi.top/show_1GW2bkHgUvQR.html "pytorch api文档:torch.exp()自然指数运算") - [pytorch api文档:torch.dot()点积](https://www.malaoshi.top/show_1GW2ZgS2nNu4.html "pytorch api文档:torch.dot()点积") - [pytorch api文档:torch.matmul()矩阵乘法/张量乘法(等价@符号)](https://www.malaoshi.top/show_1GW2b5HTn2z0.html "pytorch api文档:torch.matmul()矩阵乘法/张量乘法(等价@符号)") - [pytorch api文档:torch.tril() 返回下三角矩阵](https://www.malaoshi.top/show_1GW2cdbycdtV.html "pytorch api文档:torch.tril() 返回下三角矩阵") - [pytorch api文档:torch.triu() 返回上三角矩阵](https://www.malaoshi.top/show_1GW2cdpYK3bj.html "pytorch api文档:torch.triu() 返回上三角矩阵") - [pytorch api文档:torch.sqrt()函数-平方根](https://www.malaoshi.top/show_1GW2e7wRErGM.html "pytorch api文档:torch.sqrt()函数-平方根") - [pytorch api文档:torch.log() 函数-自然对数](https://www.malaoshi.top/show_1GW2ftW2ppdv.html "pytorch api文档:torch.log() 函数-自然对数") # 张量操作 - [pytorch api文档:张量的 .T 属性(转置操作、行列互换)](https://www.malaoshi.top/show_1GW2bqHciyO0.html "pytorch api文档:张量的 .T 属性(转置操作、行列互换)") - [pytorch api文档:torch.transpose() 函数(转置操作、行列互换)](https://www.malaoshi.top/show_1GW2bqM8Ufqm.html "pytorch api文档:torch.transpose() 函数(转置操作、行列互换)") - [pytorch api文档:张量的.view() 和 torch.transpose() 函数的区别](https://www.malaoshi.top/show_1GW2dKLmsG0U.html "pytorch api文档:张量的.view() 和 torch.transpose() 函数的区别") - [pytorch api文档:torch.stack()堆叠多个形状相同的张量](https://www.malaoshi.top/show_1GW2d14EaVsN.html "pytorch api文档:torch.stack()堆叠多个形状相同的张量") - [pytorch api文档:torch.cat()在已有维度上拼接张量(维度数不变)](https://www.malaoshi.top/show_1GW2d1AO7xXt.html "pytorch api文档:torch.cat()在已有维度上拼接张量(维度数不变)") - [pytorch api文档:torch.argmax()返回张量最大值的索引](https://www.malaoshi.top/show_1GW2erSSlyj2.html "pytorch api文档:torch.argmax()返回张量最大值的索引") - [pytorch api文档:torch.max()返回张量最大值和索引](https://www.malaoshi.top/show_1GW2erd4cuaj.html "pytorch api文档:torch.max()返回张量最大值和索引") - [pytorch api文档:torch.max() vs torch.argmax() 对比](https://www.malaoshi.top/show_1GW2ercIbwXg.html "pytorch api文档:torch.max() vs torch.argmax() 对比") - [pytorch api文档:torch.flatten()函数-将张量展平成一维](https://www.malaoshi.top/show_1GW2ftlTVQM9.html "pytorch api文档:torch.flatten()函数-将张量展平成一维") # 神经网络 在 `torch.nn` 包中,`nn` 是 Neural Networks(神经网络) 的缩写 - [pytorch api文档:torch.nn.Parameter可训练参数的封装类](https://www.malaoshi.top/show_1GW2c5QMXmqt.html "pytorch api文档:torch.nn.Parameter可训练参数的封装类") - [pytorch api文档:torch.nn.Embedding()创建嵌入层](https://www.malaoshi.top/show_1GW2aoJ5nKFn.html "pytorch api文档:torch.nn.Embedding()创建嵌入层") - [pytorch api文档:nn.Linear类(线性层/全连接层)](https://www.malaoshi.top/show_1GW2cIbomFfg.html "pytorch api文档:nn.Linear类(线性层/全连接层)") - [pytorch api文档:nn.Linear类与nn.Parameter区别](https://www.malaoshi.top/show_1GW2cIsvevnb.html "pytorch api文档:nn.Linear类与nn.Parameter区别") - [pytorch api文档:torch.nn.Dropout](https://www.malaoshi.top/show_1GW2cw6OPL65.html "pytorch api文档:torch.nn.Dropout") - [pytorch api文档:torch.nn.Sequential()按顺序堆叠神经网络层](https://www.malaoshi.top/show_1GW2e2mYQo9O.html "pytorch api文档:torch.nn.Sequential()按顺序堆叠神经网络层") - [pytorch api文档:nn.ModuleList 与 nn.Sequential 比较](https://www.malaoshi.top/show_1GW2eVtDraxi.html "pytorch api文档:nn.ModuleList 与 nn.Sequential 比较") ### 激活函数类 - [pytorch api文档:torch.nn.ReLU()激活函数类](https://www.malaoshi.top/show_1GW2eQVDPLEU.html "pytorch api文档:torch.nn.ReLU()激活函数类") - [pytorch api文档:torch.nn.GELU()激活函数](https://www.malaoshi.top/show_1GW2eQ49XIbw.html "pytorch api文档:torch.nn.GELU()激活函数") ### torch.nn.Module - [pytorch api文档:torch.nn.Module神经网络基类](https://www.malaoshi.top/show_1GW2d1fZRbo1.html "pytorch api文档:torch.nn.Module神经网络基类") - [pytorch api文档:nn.Module的register_buffer()方法将张量注册为模型的缓冲区(buffer)](https://www.malaoshi.top/show_1GW2d1KcAdvn.html "pytorch api文档:nn.Module的register_buffer()方法将张量注册为模型的缓冲区(buffer)") - [pytorch api文档:nn.Module的named_parameters()方法-返回模型中所有可训练的参数](https://www.malaoshi.top/show_1GW2egn5bB7P.html "pytorch api文档:nn.Module的named_parameters()方法-返回模型中所有可训练的参数") - [pytorch api文档:nn.Module类的.eval()方法-将模型切换到评估/推理模式](https://www.malaoshi.top/show_1GW2ft0JVX7M.html "pytorch api文档:nn.Module类的.eval()方法-将模型切换到评估/推理模式") - [pytorch api文档:nn.Module类的.train()方法-将模型切换为训练模式](https://www.malaoshi.top/show_1GW2gYFSVMb0.html "pytorch api文档:nn.Module类的.train()方法-将模型切换为训练模式") - [pytorch api文档:nn.Module类的.train() 与 .eval() 对比](https://www.malaoshi.top/show_1GW2gYBNiwQo.html "pytorch api文档:nn.Module类的.train() 与 .eval() 对比") - [pytorch api文档:nn.Module的.state_dict()-提取模型中可学习的参数和持久化的缓冲区](https://www.malaoshi.top/show_1GW2gek3MZKA.html "pytorch api文档:nn.Module的.state_dict()-提取模型中可学习的参数和持久化的缓冲区") - [pytorch api文档:nn.Module的.load_state_dict()-加载模型权重](https://www.malaoshi.top/show_1GW2gfHOO6Yb.html "pytorch api文档:nn.Module的.load_state_dict()-加载模型权重") ### ModuleList - [pytorch api文档:nn.ModuleList() 存储多个nn.Module的有序列表容器](https://www.malaoshi.top/show_1GW2dCKk4ltl.html "pytorch api文档:nn.ModuleList() 存储多个nn.Module的有序列表容器") - [pytorch api文档:nn.ModuleList 与 nn.Sequential 比较](https://www.malaoshi.top/show_1GW2eVtDraxi.html "pytorch api文档:nn.ModuleList 与 nn.Sequential 比较") ### 损失函数 - [pytorch api文档:nn.MSELoss()均方差损失](https://www.malaoshi.top/show_1GW2eh5RSApR.html "pytorch api文档:nn.MSELoss()均方差损失") - [pytorch api文档:nn.MSELoss()的backward()方法-反向传播自动计算梯度(求导)](https://www.malaoshi.top/show_1GW2ehiQEndH.html "pytorch api文档:nn.MSELoss()的backward()方法-反向传播自动计算梯度(求导)") - [pytorch api文档:nn.functional.cross_entropy()交叉熵损失函数](https://www.malaoshi.top/show_1GW2fy7aYCus.html "pytorch api文档:nn.functional.cross_entropy()交叉熵损失函数") # 加载数据 - [pytorch api文档:DataLoader()](https://www.malaoshi.top/show_1GW2YvI5QpvU.html "pytorch api文档:DataLoader()") # [激活函数](https://www.malaoshi.top/show_1GW2eL49vPk4.html "激活函数") - [pytorch api文档:torch.softmax()](https://www.malaoshi.top/show_1GW2blPk6St3.html "pytorch api文档:torch.softmax()") - [pytorch api文档:torch.tanh()激活函数](https://www.malaoshi.top/show_1GW2ePq2AnR9.html "pytorch api文档:torch.tanh()激活函数") # 其他 - [pytorch api文档:torch.set_printoptions()定义张量打印格式](https://www.malaoshi.top/show_1GW2eJREe61Z.html "pytorch api文档:torch.set_printoptions()定义张量打印格式") - [pytorch api文档:torch.no_grad()函数-关闭梯度计算](https://www.malaoshi.top/show_1GW2ftCYYlFM.html "pytorch api文档:torch.no_grad()函数-关闭梯度计算") - [pytorch api文档:torch.optim.AdamW()-AdamW优化器](https://www.malaoshi.top/show_1GW2gabxtURm.html "pytorch api文档:torch.optim.AdamW()-AdamW优化器") - [pytorch api文档:torch.save()函数-保存整个模型、模型状态字典(可训练参数)](https://www.malaoshi.top/show_1GW2gdkWbINz.html "pytorch api文档:torch.save()函数-保存整个模型、模型状态字典(可训练参数)") - [pytorch api文档:torch.load()函数-加载整个模型、模型状态字典(可训练参数)](https://www.malaoshi.top/show_1GW2gdrxR5YJ.html "pytorch api文档:torch.load()函数-加载整个模型、模型状态字典(可训练参数)") 原文出处:http://malaoshi.top/show_1GW2YvJg8Dmq.html