Pytorch torch 参考手册
PyTorch 软件包包含了用于多维张量的数据结构,并定义了在这些张量上执行的数学运算。此外,它还提供了许多实用工具,用于高效地序列化张量和任意类型的数据,以及其他有用的工具。
它还有一个 CUDA 版本,可以让你在计算能力 >= 3.0 的 NVIDIA GPU 上运行张量计算。
PyTorch torch API 手册
Tensors 类型判断
| 函数 | 描述 |
|---|---|
[torch.is_tensor(obj)](https://www.runoob.com/pytorch/pytorch-torch-is_tensor.html) | 检查 obj 是否为 PyTorch 张量。 |
[torch.is_storage(obj)](https://www.runoob.com/pytorch/pytorch-torch-is_storage.html) | 检查 obj 是否为 PyTorch 存储对象。 |
[torch.is_complex(input)](https://www.runoob.com/pytorch/pytorch-torch-is_complex.html) | 检查 input 数据类型是否为复数数据类型。 |
[torch.is_conj(input)](https://www.runoob.com/pytorch/pytorch-torch-is_conj.html) | 检查 input 是否为共轭张量。 |
[torch.is_floating_point(input)](https://www.runoob.com/pytorch/pytorch-torch-is_floating_point.html) | 检查 input 数据类型是否为浮点数据类型。 |
[torch.is_nonzero(input)](https://www.runoob.com/pytorch/pytorch-torch-is_nonzero.html) | 检查 input 是否为非零单一元素张量。 |
[torch.set_default_dtype(d)](https://www.runoob.com/pytorch/pytorch-torch-set_default_dtype.html) | 设置默认浮点数据类型为 d。 |
[torch.get_default_dtype()](https://www.runoob.com/pytorch/pytorch-torch-get_default_dtype.html) | 获取当前默认浮点 torch.dtype。 |
[torch.set_default_device(device)](https://www.runoob.com/pytorch/pytorch-torch-set_default_device.html) | 设置默认 torch.Tensor 分配的设备为 device。 |
[torch.get_default_device()](https://www.runoob.com/pytorch/pytorch-torch-get_default_device.html) | 获取默认 torch.Tensor 分配的设备。 |
[torch.set_default_tensor_type(tensor_type)](https://www.runoob.com/pytorch/pytorch-torch-set_default_tensor_type.html) | 设置默认张量类型为 tensor_type。 |
[torch.numel(input)](https://www.runoob.com/pytorch/pytorch-torch-numel.html) | 返回 input 张量中的元素总数。 |
[torch.set_printoptions(...)](https://www.runoob.com/pytorch/pytorch-torch-set_printoptions.html) | 设置张量打印选项。 |
Tensor 创建
| 函数 | 描述 |
|---|---|
[torch.tensor(data, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-tensor.html) | 从数据创建张量,复制数据,无自动梯度历史。 |
[torch.as_tensor(data, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-as_tensor.html) | 将数据转换为张量,共享数据并保留自动梯度历史。 |
[torch.asarray(data, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-asarray.html) | 将数据转换为张量数组。 |
[torch.from_numpy(ndarray)](https://www.runoob.com/pytorch/pytorch-torch-from_numpy.html) | 从 NumPy 数组创建张量(共享内存)。 |
[torch.from_dlpack(ext_tensor)](https://www.runoob.com/pytorch/pytorch-torch-from_dlpack.html) | 从 dlpack 张量创建 PyTorch 张量。 |
[torch.frombuffer(buffer, dtype, count, offset)](https://www.runoob.com/pytorch/pytorch-torch-frombuffer.html) | 从 buffer 创建一维张量。 |
[torch.zeros(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-zeros.html) | 创建全零张量。 |
[torch.zeros_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-zeros_like.html) | 创建与输入相同形状的全零张量。 |
[torch.ones(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-ones.html) | 创建全一张量。 |
[torch.ones_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-ones_like.html) | 创建与输入相同形状的全一张量。 |
[torch.empty(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-empty.html) | 创建未初始化的张量。 |
[torch.empty_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-empty_like.html) | 创建与输入相同形状的未初始化张量。 |
[torch.empty_strided(size, stride, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-empty_strided.html) | 创建具有指定步幅的未初始化张量。 |
[torch.arange(start, end, step, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-arange.html) | 创建等差序列张量。 |
[torch.range(start, end, step, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-range.html) | 创建包含 end 值的等差序列张量。 |
[torch.linspace(start, end, steps, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-linspace.html) | 创建等间隔序列张量。 |
[torch.logspace(start, end, steps, base, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-logspace.html) | 创建对数间隔序列张量。 |
[torch.eye(n, m, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-eye.html) | 创建单位矩阵。 |
[torch.full(size, fill_value, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-full.html) | 创建填充指定值的张量。 |
[torch.full_like(input, fill_value, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-full_like.html) | 创建与输入相同形状的填充张量。 |
[torch.rand(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-rand.html) | 创建均匀分布随机张量(范围 [0, 1))。 |
[torch.rand_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-rand_like.html) | 创建与输入相同形状的均匀分布随机张量。 |
[torch.randn(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randn.html) | 创建标准正态分布随机张量。 |
[torch.randn_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randn_like.html) | 创建与输入相同形状的标准正态分布随机张量。 |
[torch.randint(low, high, size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randint.html) | 创建整数随机张量。 |
[torch.randint_like(input, low, high, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randint_like.html) | 创建与输入相同形状的整数随机张量。 |
[torch.randperm(n, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randperm.html) | 创建 0 到 n-1 的随机排列。 |
[torch.sparse_coo_tensor(indices, values, size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-sparse_coo_tensor.html) | 在指定的 indices 处构造稀疏 COO 张量。 |
[torch.sparse_csr_tensor(crow_indices, col_indices, values, size, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-sparse_csr_tensor.html) | 构造稀疏 CSR 张量。 |
[torch.sparse_csc_tensor(ccol_indices, row_indices, values, size, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-sparse_csc_tensor.html) | 构造稀疏 CSC 张量。 |
[torch.quantize_per_tensor(input, scale, zero_point, dtype)](https://www.runoob.com/pytorch/pytorch-torch-quantize_per_tensor.html) | 创建量化张量(per-tensor)。 |
[torch.quantize_per_channel(input, scales, zero_points, axis, dtype)](https://www.runoob.com/pytorch/pytorch-torch-quantize_per_channel.html) | 创建量化张量(per-channel)。 |
[torch.dequantize(input)](https://www.runoob.com/pytorch/pytorch-torch-dequantize.html) | 反量化张量。 |
[torch.complex(real, imag)](https://www.runoob.com/pytorch/pytorch-torch-complex.html) | 从实部和虚部创建复数张量。 |
[torch.polar(abs, angle)](https://www.runoob.com/pytorch/pytorch-torch-polar.html) | 从极坐标创建复数张量。 |
[torch.heaviside(input, values)](https://www.runoob.com/pytorch/pytorch-torch-heaviside.html) | 计算 Heaviside 阶跃函数。 |
索引、切片、连接、变异操作
| 函数 | 描述 |
|---|---|
[torch.cat(tensors, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cat.html) | 沿指定维度连接张量。 |
[torch.concat(tensors, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-concat.html) | 沿指定维度连接张量(同 cat)。 |
[torch.concatenate(tensors, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-concatenate.html) | 沿指定维度连接张量(同 cat)。 |
[torch.stack(tensors, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-stack.html) | 沿新维度堆叠张量。 |
[torch.split(tensor, split_size, dim)](https://www.runoob.com/pytorch/pytorch-torch-split.html) | 将张量沿指定维度分割。 |
[torch.chunk(tensor, chunks, dim)](https://www.runoob.com/pytorch/pytorch-torch-chunk.html) | 将张量沿指定维度分块。 |
[torch.reshape(input, shape)](https://www.runoob.com/pytorch/pytorch-torch-reshape.html) | 改变张量的形状。 |
[torch.transpose(input, dim0, dim1)](https://www.runoob.com/pytorch/pytorch-torch-transpose.html) | 交换张量的两个维度。 |
[torch.t(input)](https://www.runoob.com/pytorch/pytorch-torch-t.html) | 转置二维张量。 |
[torch.squeeze(input, dim)](https://www.runoob.com/pytorch/pytorch-torch-squeeze.html) | 移除大小为 1 的维度。 |
[torch.unsqueeze(input, dim)](https://www.runoob.com/pytorch/pytorch-torch-unsqueeze.html) | 在指定位置插入大小为 1 的维度。 |
[torch.permute(input, dims)](https://www.runoob.com/pytorch/pytorch-torch-permute.html) | 重新排列张量的维度。 |
[torch.movedim(input, source, destination)](https://www.runoob.com/pytorch/pytorch-torch-movedim.html) | 移动张量的维度到新位置。 |
[torch.moveaxis(input, source, destination)](https://www.runoob.com/pytorch/pytorch-torch-moveaxis.html) | 移动张量的轴到新位置。 |
[torch.narrow(input, dim, start, length)](https://www.runoob.com/pytorch/pytorch-torch-narrow.html) | 返回张量的切片。 |
[torch.narrow_copy(input, dim, start, length)](https://www.runoob.com/pytorch/pytorch-torch-narrow_copy.html) | 返回张量的切片副本。 |
[torch.select(input, dim, index)](https://www.runoob.com/pytorch/pytorch-torch-select.html) | 沿指定维度选择索引对应的切片。 |
[torch.slice_scatter(input, src, dim, start, end)](https://www.runoob.com/pytorch/pytorch-torch-slice_scatter.html) | 将 src 散布到 input 的切片中。 |
[torch.select_scatter(input, src, dim, index)](https://www.runoob.com/pytorch/pytorch-torch-select_scatter.html) | 将 src 散布到指定索引位置。 |
[torch.diagonal_scatter(input, src, offset, dim1, dim2)](https://www.runoob.com/pytorch/pytorch-torch-diagonal_scatter.html) | 将值散布到对角线位置。 |
[torch.expand(input, size)](https://www.runoob.com/pytorch/pytorch-torch-expand.html) | 扩展张量的尺寸(复制视图)。 |
[torch.expand_as(input, other)](https://www.runoob.com/pytorch/pytorch-torch-expand_as.html) | 将张量扩展到与 other 相同的尺寸。 |
[torch.masked_select(input, mask)](https://www.runoob.com/pytorch/pytorch-torch-masked_select.html) | 根据布尔掩码选择元素。 |
[torch.index_select(input, dim, index)](https://www.runoob.com/pytorch/pytorch-torch-index_select.html) | 沿指定维度选择索引对应的元素。 |
[torch.gather(input, dim, index, sparse_grad)](https://www.runoob.com/pytorch/pytorch-torch-gather.html) | 沿指定维度收集指定索引的元素。 |
[torch.scatter(input, dim, index, src, reduce)](https://www.runoob.com/pytorch/pytorch-torch-scatter.html) | 将 src 的值散布到 input 的指定位置。 |
[torch.scatter_add(input, dim, index, src)](https://www.runoob.com/pytorch/pytorch-torch-scatter_add.html) | 将 src 的值加到指定位置。 |
[torch.scatter_reduce(input, dim, index, src, reduce, include_self)](https://www.runoob.com/pytorch/pytorch-torch-scatter_reduce.html) | 将 src 的值按指定方式聚合到指定位置。 |
[torch.index_add(input, dim, index, source, alpha)](https://www.runoob.com/pytorch/pytorch-torch-index_add.html) | 将 source 加到 index 指定的位置。 |
[torch.index_copy(input, dim, index, source)](https://www.runoob.com/pytorch/pytorch-torch-index_copy.html) | 将 source 复制到 index 指定的位置。 |
[torch.index_reduce(input, dim, index, source, reduce, include_self)](https://www.runoob.com/pytorch/pytorch-torch-index_reduce.html) | 将 source 按指定方式聚合到 index 指定的位置。 |
[torch.take(input, index)](https://www.runoob.com/pytorch/pytorch-torch-take.html) | 获取给定索引位置的元素。 |
[torch.take_along_dim(input, indices, dim)](https://www.runoob.com/pytorch/pytorch-torch-take_along_dim.html) | 沿指定维度获取索引位置的元素。 |
[torch.nonzero(input)](https://www.runoob.com/pytorch/pytorch-torch-nonzero.html) | 返回非零元素的索引。 |
[torch.argwhere(input)](https://www.runoob.com/pytorch/pytorch-torch-argwhere.html) | 返回满足条件的元素索引。 |
[torch.where(condition, input, other)](https://www.runoob.com/pytorch/pytorch-torch-where.html) | 根据条件返回元素。 |
[torch.unbind(tensor, dim)](https://www.runoob.com/pytorch/pytorch-torch-unbind.html) | 沿指定维度分割为元组。 |
[torch.split_with_sizes(tensor, split_sizes, dim)](https://www.runoob.com/pytorch/pytorch-torch-split_with_sizes.html) | 按大小分割张量。 |
[torch.tensor_split(tensor, indices_or_sections, dim)](https://www.runoob.com/pytorch/pytorch-torch-tensor_split.html) | 按索引或段数分割张量。 |
[torch.hsplit(tensor, indices_or_sections)](https://www.runoob.com/pytorch/pytorch-torch-hsplit.html) | 水平分割张量。 |
[torch.vsplit(tensor, indices_or_sections)](https://www.runoob.com/pytorch/pytorch-torch-vsplit.html) | 垂直分割张量。 |
[torch.dsplit(tensor, indices_or_sections)](https://www.runoob.com/pytorch/pytorch-torch-dsplit.html) | 深度分割张量。 |
[torch.hstack(tensors, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-hstack.html) | 水平堆叠张量。 |
[torch.vstack(tensors, out)](https://www.runoob.com/pytorch/pytorch-torch-vstack.html) | 垂直堆叠张量。 |
[torch.dstack(tensors, out)](https://www.runoob.com/pytorch/pytorch-torch-dstack.html) | 深度堆叠张量。 |
[torch.column_stack(tensors, out)](https://www.runoob.com/pytorch/pytorch-torch-column_stack.html) | 列堆叠张量。 |
[torch.row_stack(tensors, out)](https://www.runoob.com/pytorch/pytorch-torch-row_stack.html) | 行堆叠张量(同 vstack)。 |
[torch.tile(input, dims)](https://www.runoob.com/pytorch/pytorch-torch-tile.html) | 重复张量多次。 |
[torch.repeat_interleave(input, repeats, dim)](https://www.runoob.com/pytorch/pytorch-torch-repeat_interleave.html) | 沿指定维度重复元素。 |
[torch.flip(input, dims)](https://www.runoob.com/pytorch/pytorch-torch-flip.html) | 沿指定维度翻转张量。 |
[torch.fliplr(input)](https://www.runoob.com/pytorch/pytorch-torch-fliplr.html) | 左右翻转张量。 |
[torch.flipud(input)](https://www.runoob.com/pytorch/pytorch-torch-flipud.html) | 上下翻转张量。 |
[torch.rot90(input, k, dims)](https://www.runoob.com/pytorch/pytorch-torch-rot90.html) | 旋转张量 90 度。 |
[torch.linalg.matrix_transpose(input)](https://www.runoob.com/pytorch/pytorch-torch-linalg-matrix_transpose.html) | 矩阵转置。 |
[torch.adjoint(input)](https://www.runoob.com/pytorch/pytorch-torch-adjoint.html) | 返回张量的伴随矩阵。 |
[torch.resolve_conj(input)](https://www.runoob.com/pytorch/pytorch-torch-resolve_conj.html) | 解析共轭张量。 |
[torch.resolve_neg(input)](https://www.runoob.com/pytorch/pytorch-torch-resolve_neg.html) | 解析负张量。 |
[torch.view_as_real(input)](https://www.runoob.com/pytorch/pytorch-torch-view_as_real.html) | 将复数张量视为实数张量。 |
[torch.view_as_complex(input)](https://www.runoob.com/pytorch/pytorch-torch-view_as_complex.html) | 将实数张量视为复数张量。 |
[torch.unravel_index(indices, shape)](https://www.runoob.com/pytorch/pytorch-torch-unravel_index.html) | 将展平索引转换为多维索引。 |
随机数生成
| 函数 | 描述 |
|---|---|
[torch.manual_seed(seed)](https://www.runoob.com/pytorch/pytorch-torch-manual_seed.html) | 设置随机种子(CPU)。 |
[torch.seed()](https://www.runoob.com/pytorch/pytorch-torch-seed.html) | 设置随机种子并返回新的种子值。 |
[torch.initial_seed()](https://www.runoob.com/pytorch/pytorch-torch-initial_seed.html) | 返回当前随机种子。 |
[torch.get_rng_state()](https://www.runoob.com/pytorch/pytorch-torch-get_rng_state.html) | 返回随机数生成器状态。 |
[torch.set_rng_state(state)](https://www.runoob.com/pytorch/pytorch-torch-set_rng_state.html) | 设置随机数生成器状态。 |
[torch.rand(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-rand.html) | 创建均匀分布随机张量(范围 [0, 1))。 |
[torch.rand_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-rand_like.html) | 创建与输入相同形状的均匀分布随机张量。 |
[torch.randn(*size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randn.html) | 创建标准正态分布随机张量。 |
[torch.randn_like(input, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randn_like.html) | 创建与输入相同形状的标准正态分布随机张量。 |
[torch.randint(low, high, size, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randint.html) | 创建整数随机张量。 |
[torch.randint_like(input, low, high, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randint_like.html) | 创建与输入相同形状的整数随机张量。 |
[torch.randperm(n, dtype, device, requires_grad)](https://www.runoob.com/pytorch/pytorch-torch-randperm.html) | 创建 0 到 n-1 的随机排列。 |
[torch.bernoulli(input, *, generator)](https://www.runoob.com/pytorch/pytorch-torch-bernoulli.html) | 从伯努利分布生成随机数。 |
[torch.multinomial(input, num_samples, replacement, generator)](https://www.runoob.com/pytorch/pytorch-torch-multinomial.html) | 多项式采样。 |
[torch.normal(mean, std, out)](https://www.runoob.com/pytorch/pytorch-torch-normal.html) | 从正态分布生成随机数。 |
[torch.poisson(input, generator)](https://www.runoob.com/pytorch/pytorch-torch-poisson.html) | 从泊松分布生成随机数。 |
序列化
| 函数 | 描述 |
|---|---|
[torch.save(obj, f, pickle_module, pickle_protocol)](https://www.runoob.com/pytorch/pytorch-torch-save.html) | 保存对象到文件。 |
[torch.load(f, map_location, pickle_module, weights_only)](https://www.runoob.com/pytorch/pytorch-torch-load.html) | 从文件加载对象。 |
梯度控制
| 函数 | 描述 |
|---|---|
[torch.no_grad()](https://www.runoob.com/pytorch/pytorch-torch-no_grad.html) | 上下文管理器,禁用梯度计算。 |
[torch.enable_grad()](https://www.runoob.com/pytorch/pytorch-torch-enable_grad.html) | 上下文管理器,启用梯度计算。 |
[torch.set_grad_enabled(grad)](https://www.runoob.com/pytorch/pytorch-torch-set_grad_enabled.html) | 设置是否启用梯度计算。 |
[torch.is_grad_enabled()](https://www.runoob.com/pytorch/pytorch-torch-is_grad_enabled.html) | 检查是否启用梯度计算。 |
[torch.inference_mode()](https://www.runoob.com/pytorch/pytorch-torch-inference_mode.html) | 上下文管理器,推理模式(禁用梯度和 autograd)。 |
[torch.is_inference_mode_enabled()](https://www.runoob.com/pytorch/pytorch-torch-is_inference_mode_enabled.html) | 检查是否启用推理模式。 |
数学运算 - 点操作
| 函数 | 描述 |
|---|---|
[torch.abs(input, out)](https://www.runoob.com/pytorch/pytorch-torch-abs.html) | 逐元素绝对值。 |
[torch.absolute(input, out)](https://www.runoob.com/pytorch/pytorch-torch-absolute.html) | 逐元素绝对值(同 abs)。 |
[torch.acos(input, out)](https://www.runoob.com/pytorch/pytorch-torch-acos.html) | 逐元素反余弦。 |
[torch.arccos(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arccos.html) | 逐元素反余弦(同 acos)。 |
[torch.acosh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-acosh.html) | 逐元素反双曲余弦。 |
[torch.arccosh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arccosh.html) | 逐元素反双曲余弦(同 acosh)。 |
[torch.add(input, other, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-add.html) | 逐元素加法(可指定 alpha 缩放)。 |
[torch.addcdiv(input, tensor1, tensor2, value, out)](https://www.runoob.com/pytorch/pytorch-torch-addcdiv.html) | 执行 input + value * (tensor1 / tensor2)。 |
[torch.addcmul(input, tensor1, tensor2, value, out)](https://www.runoob.com/pytorch/pytorch-torch-addcmul.html) | 执行 input + value * (tensor1 * tensor2)。 |
[torch.angle(input, out)](https://www.runoob.com/pytorch/pytorch-torch-angle.html) | 返回复数张量的相位角。 |
[torch.asin(input, out)](https://www.runoob.com/pytorch/pytorch-torch-asin.html) | 逐元素反正弦。 |
[torch.arcsin(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arcsin.html) | 逐元素反正弦(同 asin)。 |
[torch.asinh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-asinh.html) | 逐元素反双曲正弦。 |
[torch.arcsinh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arcsinh.html) | 逐元素反双曲正弦(同 asinh)。 |
[torch.atan(input, out)](https://www.runoob.com/pytorch/pytorch-torch-atan.html) | 逐元素反正切。 |
[torch.arctan(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arctan.html) | 逐元素反正切(同 atan)。 |
[torch.atan2(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-atan2.html) | 逐元素二维反正切。 |
[torch.arctan2(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-arctan2.html) | 逐元素二维反正切(同 atan2)。 |
[torch.atanh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-atanh.html) | 逐元素反双曲正切。 |
[torch.arctanh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-arctanh.html) | 逐元素反双曲正切(同 atanh)。 |
[torch.bitwise_not(input, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_not.html) | 逐元素按位取反。 |
[torch.bitwise_and(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_and.html) | 逐元素按位与。 |
[torch.bitwise_or(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_or.html) | 逐元素按位或。 |
[torch.bitwise_xor(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_xor.html) | 逐元素按位异或。 |
[torch.bitwise_left_shift(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_left_shift.html) | 逐元素左移位。 |
[torch.bitwise_right_shift(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-bitwise_right_shift.html) | 逐元素右移位。 |
[torch.ceil(input, out)](https://www.runoob.com/pytorch/pytorch-torch-ceil.html) | 逐元素向上取整。 |
[torch.clamp(input, min, max, out)](https://www.runoob.com/pytorch/pytorch-torch-clamp.html) | 将张量值限制在指定范围内。 |
[torch.clip(input, min, max, out)](https://www.runoob.com/pytorch/pytorch-torch-clip.html) | 将张量值限制在指定范围内(同 clamp)。 |
[torch.conj_physical(input, out)](https://www.runoob.com/pytorch/pytorch-torch-conj_physical.html) | 逐元素计算物理共轭。 |
[torch.copysign(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-copysign.html) | 逐元素复制符号。 |
[torch.cos(input, out)](https://www.runoob.com/pytorch/pytorch-torch-cos.html) | 逐元素余弦。 |
[torch.cosh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-cosh.html) | 逐元素双曲余弦。 |
[torch.deg2rad(input, out)](https://www.runoob.com/pytorch/pytorch-torch-deg2rad.html) | 将角度转换为弧度。 |
[torch.div(input, other, rounding_mode, out)](https://www.runoob.com/pytorch/pytorch-torch-div.html) | 逐元素除法。 |
[torch.divide(input, other, rounding_mode, out)](https://www.runoob.com/pytorch/pytorch-torch-divide.html) | 逐元素除法(同 div)。 |
[torch.digamma(input, out)](https://www.runoob.com/pytorch/pytorch-torch-digamma.html) | 逐元素计算 psi 函数(对数导数)。 |
[torch.erf(input, out)](https://www.runoob.com/pytorch/pytorch-torch-erf.html) | 逐元素误差函数。 |
[torch.erfc(input, out)](https://www.runoob.com/pytorch/pytorch-torch-erfc.html) | 逐元素互补误差函数。 |
[torch.erfinv(input, out)](https://www.runoob.com/pytorch/pytorch-torch-erfinv.html) | 逐元素误差函数逆。 |
[torch.exp(input, out)](https://www.runoob.com/pytorch/pytorch-torch-exp.html) | 逐元素指数函数。 |
[torch.exp2(input, out)](https://www.runoob.com/pytorch/pytorch-torch-exp2.html) | 逐元素 2 的幂。 |
[torch.expm1(input, out)](https://www.runoob.com/pytorch/pytorch-torch-expm1.html) | 逐元素 exp(x) - 1。 |
[torch.fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max)](https://www.runoob.com/pytorch/pytorch-torch-fake_quantize_per_channel_affine.html) | 模拟每通道量化。 |
[torch.fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_max)](https://www.runoob.com/pytorch/pytorch-torch-fake_quantize_per_tensor_affine.html) | 模拟每张量量化。 |
[torch.fix(input, out)](https://www.runoob.com/pytorch/pytorch-torch-fix.html) | 逐元素取整数部分(向零取整)。 |
[torch.float_power(input, exponent, out)](https://www.runoob.com/pytorch/pytorch-torch-float_power.html) | 逐元素浮点幂运算。 |
[torch.floor(input, out)](https://www.runoob.com/pytorch/pytorch-torch-floor.html) | 逐元素向下取整。 |
[torch.floor_divide(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-floor_divide.html) | 逐元素整除。 |
[torch.fmod(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-fmod.html) | 逐元素取模(余数)。 |
[torch.frac(input, out)](https://www.runoob.com/pytorch/pytorch-torch-frac.html) | 逐元素取小数部分。 |
[torch.frexp(input, out)](https://www.runoob.com/pytorch/pytorch-torch-frexp.html) | 将浮点数分解为尾数和指数。 |
[torch.gradient(input, dim, spacing, edge_order)](https://www.runoob.com/pytorch/pytorch-torch-gradient.html) | 计算张量的梯度。 |
[torch.imag(input, out)](https://www.runoob.com/pytorch/pytorch-torch-imag.html) | 返回复数张量的虚部。 |
[torch.ldexp(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-ldexp.html) | 逐元素计算 input * 2**other。 |
[torch.lerp(input, end, weight, out)](https://www.runoob.com/pytorch/pytorch-torch-lerp.html) | 逐元素线性插值。 |
[torch.lgamma(input, out)](https://www.runoob.com/pytorch/pytorch-torch-lgamma.html) | 逐元素 gamma 函数的对数。 |
[torch.log(input, out)](https://www.runoob.com/pytorch/pytorch-torch-log.html) | 逐元素自然对数。 |
[torch.log10(input, out)](https://www.runoob.com/pytorch/pytorch-torch-log10.html) | 逐元素以 10 为底的对数。 |
[torch.log1p(input, out)](https://www.runoob.com/pytorch/pytorch-torch-log1p.html) | 逐元素 log(1 + x)。 |
[torch.log2(input, out)](https://www.runoob.com/pytorch/pytorch-torch-log2.html) | 逐元素以 2 为底的对数。 |
[torch.logaddexp(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-logaddexp.html) | 逐元素 log(exp(input) + exp(other))。 |
[torch.logaddexp2(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-logaddexp2.html) | 逐元素 log2(2**input + 2**other)。 |
[torch.logical_and(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-logical_and.html) | 逐元素逻辑与。 |
[torch.logical_not(input, out)](https://www.runoob.com/pytorch/pytorch-torch-logical_not.html) | 逐元素逻辑非。 |
[torch.logical_or(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-logical_or.html) | 逐元素逻辑或。 |
[torch.logical_xor(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-logical_xor.html) | 逐元素逻辑异或。 |
[torch.logit(input, eps, out)](https://www.runoob.com/pytorch/pytorch-torch-logit.html) | 逐元素 logit 函数。 |
[torch.hypot(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-hypot.html) | 逐元素 hypot 函数 sqrt(input^2 + other^2)。 |
[torch.i0(input, out)](https://www.runoob.com/pytorch/pytorch-torch-i0.html) | 逐元素修正贝塞尔函数(第一类,0 阶)。 |
[torch.igamma(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-igamma.html) | 逐元素不完全 gamma 函数。 |
[torch.igammac(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-igammac.html) | 逐元素互补不完全 gamma 函数。 |
[torch.mul(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-mul.html) | 逐元素乘法。 |
[torch.multiply(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-multiply.html) | 逐元素乘法(同 mul)。 |
[torch.mvlgamma(input, p, out)](https://www.runoob.com/pytorch/pytorch-torch-mvlgamma.html) | 逐元素多元 gamma 函数的对数。 |
[torch.nan_to_num(input, nan, posinf, neginf, out)](https://www.runoob.com/pytorch/pytorch-torch-nan_to_num.html) | 将 NaN 替换为指定值。 |
[torch.neg(input, out)](https://www.runoob.com/pytorch/pytorch-torch-neg.html) | 逐元素取负。 |
[torch.negative(input, out)](https://www.runoob.com/pytorch/pytorch-torch-negative.html) | 逐元素取负(同 neg)。 |
[torch.nextafter(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-nextafter.html) | 逐元素返回下一个可表示的浮点数。 |
[torch.polygamma(input, n, out)](https://www.runoob.com/pytorch/pytorch-torch-polygamma.html) | 逐元素 polygamma 函数。 |
[torch.positive(input, out)](https://www.runoob.com/pytorch/pytorch-torch-positive.html) | 逐元素取正。 |
[torch.pow(input, exponent, out)](https://www.runoob.com/pytorch/pytorch-torch-pow.html) | 逐元素幂运算。 |
[torch.quantized_batch_norm(input, weight, bias, mean, var, eps, output_scale, output_zero_point)](https://www.runoob.com/pytorch/pytorch-torch-quantized_batch_norm.html) | 量化批归一化。 |
[torch.quantized_max_pool1d(input, kernel_size, stride, padding, dilation, ceil_mode)](https://www.runoob.com/pytorch/pytorch-torch-quantized_max_pool1d.html) | 量化最大池化(1D)。 |
[torch.quantized_max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)](https://www.runoob.com/pytorch/pytorch-torch-quantized_max_pool2d.html) | 量化最大池化(2D)。 |
[torch.rad2deg(input, out)](https://www.runoob.com/pytorch/pytorch-torch-rad2deg.html) | 将弧度转换为角度。 |
[torch.real(input, out)](https://www.runoob.com/pytorch/pytorch-torch-real.html) | 返回复数张量的实部。 |
[torch.reciprocal(input, out)](https://www.runoob.com/pytorch/pytorch-torch-reciprocal.html) | 逐元素倒数。 |
[torch.remainder(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-remainder.html) | 逐元素取余。 |
[torch.round(input, decimals, out)](https://www.runoob.com/pytorch/pytorch-torch-round.html) | 逐元素四舍五入。 |
[torch.rsqrt(input, out)](https://www.runoob.com/pytorch/pytorch-torch-rsqrt.html) | 逐元素平方根倒数。 |
[torch.sigmoid(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sigmoid.html) | 逐元素 sigmoid 函数。 |
[torch.sign(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sign.html) | 逐元素返回符号(-1, 0, 1)。 |
[torch.sgn(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sgn.html) | 逐元素返回符号向量。 |
[torch.signbit(input, out)](https://www.runoob.com/pytorch/pytorch-torch-signbit.html) | 逐元素检查符号位。 |
[torch.sin(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sin.html) | 逐元素正弦。 |
[torch.sinc(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sinc.html) | 逐元素 sinc 函数 sin(pi*x)/(pi*x)。 |
[torch.sinh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sinh.html) | 逐元素双曲正弦。 |
[torch.softmax(input, dim, dtype)](https://www.runoob.com/pytorch/pytorch-torch-softmax.html) | 逐元素 softmax 函数。 |
[torch.sqrt(input, out)](https://www.runoob.com/pytorch/pytorch-torch-sqrt.html) | 逐元素平方根。 |
[torch.square(input, out)](https://www.runoob.com/pytorch/pytorch-torch-square.html) | 逐元素平方。 |
[torch.sub(input, other, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-sub.html) | 逐元素减法。 |
[torch.subtract(input, other, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-subtract.html) | 逐元素减法(同 sub)。 |
[torch.tan(input, out)](https://www.runoob.com/pytorch/pytorch-torch-tan.html) | 逐元素正切。 |
[torch.tanh(input, out)](https://www.runoob.com/pytorch/pytorch-torch-tanh.html) | 逐元素双曲正切。 |
[torch.true_divide(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-true_divide.html) | 逐元素真除法。 |
[torch.trunc(input, out)](https://www.runoob.com/pytorch/pytorch-torch-trunc.html) | 逐元素截断(取整数部分)。 |
[torch.xlogy(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-xlogy.html) | 逐元素计算 input * log(other)。 |
数学运算 - 归约操作
| 函数 | 描述 |
|---|---|
[torch.argmax(input, dim, keepdim)](https://www.runoob.com/pytorch/pytorch-torch-argmax.html) | 返回沿维度最大值的索引。 |
[torch.argmin(input, dim, keepdim)](https://www.runoob.com/pytorch/pytorch-torch-argmin.html) | 返回沿维度最小值的索引。 |
[torch.amax(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-amax.html) | 返回沿维度的最大值。 |
[torch.amin(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-amin.html) | 返回沿维度的最小值。 |
[torch.aminmax(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-aminmax.html) | 返回沿维度的最小值和最大值。 |
[torch.all(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-all.html) | 判断是否所有元素都为 True。 |
[torch.any(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-any.html) | 判断是否有元素为 True。 |
[torch.max(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-max.html) | 沿指定维度求最大值。 |
[torch.min(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-min.html) | 沿指定维度求最小值。 |
[torch.dist(input, other, p)](https://www.runoob.com/pytorch/pytorch-torch-dist.html) | 计算两个张量之间的 p 范数距离。 |
[torch.logsumexp(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-logsumexp.html) | 计算 log-sum-exp。 |
[torch.mean(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-mean.html) | 沿指定维度求均值。 |
[torch.nanmean(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-nanmean.html) | 沿指定维度求均值(忽略 NaN)。 |
[torch.median(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-median.html) | 沿指定维度求中位数。 |
[torch.nanmedian(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-nanmedian.html) | 沿指定维度求中位数(忽略 NaN)。 |
[torch.mode(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-mode.html) | 沿指定维度求众数。 |
[torch.norm(input, p, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-norm.html) | 计算 p 范数。 |
[torch.nansum(input, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-nansum.html) | 沿指定维度求和(忽略 NaN)。 |
[torch.prod(input, dim, keepdim, dtype, out)](https://www.runoob.com/pytorch/pytorch-torch-prod.html) | 沿指定维度求积。 |
[torch.quantile(input, q, dim, keepdim, out, method)](https://www.runoob.com/pytorch/pytorch-torch-quantile.html) | 计算分位数。 |
[torch.nanquantile(input, q, dim, keepdim, out, method)](https://www.runoob.com/pytorch/pytorch-torch-nanquantile.html) | 计算分位数(忽略 NaN)。 |
[torch.std(input, dim, unbiased, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-std.html) | 计算标准差。 |
[torch.std_mean(input, dim, unbiased, keepdim)](https://www.runoob.com/pytorch/pytorch-torch-std_mean.html) | 计算标准差和均值。 |
[torch.sum(input, dim, keepdim, dtype, out)](https://www.runoob.com/pytorch/pytorch-torch-sum.html) | 沿指定维度求和。 |
[torch.unique(input, sorted, return_inverse, return_counts, dim)](https://www.runoob.com/pytorch/pytorch-torch-unique.html) | 返回唯一元素。 |
[torch.unique_consecutive(input, sorted, return_inverse, return_counts, dim)](https://www.runoob.com/pytorch/pytorch-torch-unique_consecutive.html) | 返回连续唯一元素。 |
[torch.var(input, dim, unbiased, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-var.html) | 计算方差。 |
[torch.var_mean(input, dim, unbiased, keepdim)](https://www.runoob.com/pytorch/pytorch-torch-var_mean.html) | 计算方差和均值。 |
[torch.count_nonzero(input, dim)](https://www.runoob.com/pytorch/pytorch-torch-count_nonzero.html) | 统计非零元素数量。 |
[torch.hash_tensor(input)](https://www.runoob.com/pytorch/pytorch-torch-hash_tensor.html) | 计算张量的哈希值。 |
数学运算 - 比较操作
| 函数 | 描述 |
|---|---|
[torch.allclose(input, other, rtol, atol, equal_nan)](https://www.runoob.com/pytorch/pytorch-torch-allclose.html) | 检查两个张量是否接近(所有元素)。 |
[torch.argsort(input, dim, descending, stable)](https://www.runoob.com/pytorch/pytorch-torch-argsort.html) | 返回排序后的索引。 |
[torch.eq(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-eq.html) | 逐元素相等比较。 |
[torch.equal(input, other)](https://www.runoob.com/pytorch/pytorch-torch-equal.html) | 检查两个张量是否完全相等。 |
[torch.ge(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-ge.html) | 逐元素大于等于比较。 |
[torch.greater_equal(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-greater_equal.html) | 逐元素大于等于比较(同 ge)。 |
[torch.gt(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-gt.html) | 逐元素大于比较。 |
[torch.greater(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-greater.html) | 逐元素大于比较(同 gt)。 |
[torch.isclose(input, other, rtol, atol, equal_nan)](https://www.runoob.com/pytorch/pytorch-torch-isclose.html) | 检查两个张量是否接近(逐元素)。 |
[torch.isfinite(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isfinite.html) | 检查是否为有限值。 |
[torch.isin(elements, test_elements, assume_unique, invert)](https://www.runoob.com/pytorch/pytorch-torch-isin.html) | 检查元素是否在集合中。 |
[torch.isinf(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isinf.html) | 检查是否为无穷值。 |
[torch.isposinf(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isposinf.html) | 检查是否为正无穷。 |
[torch.isneginf(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isneginf.html) | 检查是否为负无穷。 |
[torch.isnan(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isnan.html) | 检查是否为 NaN。 |
[torch.isreal(input, out)](https://www.runoob.com/pytorch/pytorch-torch-isreal.html) | 检查是否为实数。 |
[torch.kthvalue(input, k, dim, keepdim, out)](https://www.runoob.com/pytorch/pytorch-torch-kthvalue.html) | 返回第 k 小的元素和索引。 |
[torch.le(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-le.html) | 逐元素小于等于比较。 |
[torch.less_equal(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-less_equal.html) | 逐元素小于等于比较(同 le)。 |
[torch.lt(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-lt.html) | 逐元素小于比较。 |
[torch.less(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-less.html) | 逐元素小于比较(同 lt)。 |
[torch.maximum(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-maximum.html) | 逐元素取最大值。 |
[torch.minimum(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-minimum.html) | 逐元素取最小值。 |
[torch.fmax(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-fmax.html) | 逐元素取最大值(忽略 NaN)。 |
[torch.fmin(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-fmin.html) | 逐元素取最小值(忽略 NaN)。 |
[torch.ne(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-ne.html) | 逐元素不等比较。 |
[torch.not_equal(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-not_equal.html) | 逐元素不等比较(同 ne)。 |
[torch.sort(input, dim, descending, stable, out)](https://www.runoob.com/pytorch/pytorch-torch-sort.html) | 沿指定维度排序。 |
[torch.topk(input, k, dim, largest, sorted, out)](https://www.runoob.com/pytorch/pytorch-torch-topk.html) | 返回最大的 k 个元素和索引。 |
[torch.msort(input, out)](https://www.runoob.com/pytorch/pytorch-torch-msort.html) | 沿最后一个维度排序(返回排序后的张量)。 |
数学运算 - 谱操作
| 函数 | 描述 |
|---|---|
[torch.stft(input, n_fft, hop_length, win_length, window, center, normalized, onesided, return_complex)](https://www.runoob.com/pytorch/pytorch-torch-stft.html) | 短时傅里叶变换。 |
[torch.istft(input, n_fft, hop_length, win_length, window, center, normalized, onesided, length, return_complex)](https://www.runoob.com/pytorch/pytorch-torch-istft.html) | 短时傅里叶变换逆。 |
[torch.bartlett_window(window_length, periodic, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-bartlett_window.html) | Bartlett 窗口。 |
[torch.blackman_window(window_length, periodic, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-blackman_window.html) | Blackman 窗口。 |
[torch.hamming_window(window_length, periodic, alpha, beta, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-hamming_window.html) | Hamming 窗口。 |
[torch.hann_window(window_length, periodic, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-hann_window.html) | Hann 窗口。 |
[torch.kaiser_window(window_length, periodic, beta, dtype, device)](https://www.runoob.com/pytorch/pytorch-torch-kaiser_window.html) | Kaiser 窗口。 |
数学运算 - 其他操作
| 函数 | 描述 |
|---|---|
[torch.atleast_1d(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-atleast_1d.html) | 将输入转换为至少 1 维的张量。 |
[torch.atleast_2d(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-atleast_2d.html) | 将输入转换为至少 2 维的张量。 |
[torch.atleast_3d(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-atleast_3d.html) | 将输入转换为至少 3 维的张量。 |
[torch.bincount(input, weights, minlength)](https://www.runoob.com/pytorch/pytorch-torch-bincount.html) | 计算非负整数的出现次数。 |
[torch.block_diag(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-block_diag.html) | 从输入张量构建块对角矩阵。 |
[torch.broadcast_tensors(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-broadcast_tensors.html) | 将输入广播到相同形状。 |
[torch.broadcast_to(input, shape)](https://www.runoob.com/pytorch/pytorch-torch-broadcast_to.html) | 将张量广播到指定形状。 |
[torch.broadcast_shapes(*shapes)](https://www.runoob.com/pytorch/pytorch-torch-broadcast_shapes.html) | 广播形状以兼容操作。 |
[torch.bucketize(input, boundaries, right)](https://www.runoob.com/pytorch/pytorch-torch-bucketize.html) | 将输入映射到桶索引。 |
[torch.cartesian_prod(*tensors)](https://www.runoob.com/pytorch/pytorch-torch-cartesian_prod.html) | 计算笛卡尔积。 |
[torch.cdist(x1, x2, p, compute_mode)](https://www.runoob.com/pytorch/pytorch-torch-cdist.html) | 计算成对距离。 |
[torch.clone(input, memory_format)](https://www.runoob.com/pytorch/pytorch-torch-clone.html) | 返回张量的副本。 |
[torch.combinations(input, r, with_replacement)](https://www.runoob.com/pytorch/pytorch-torch-combinations.html) | 计算组合。 |
[torch.corrcoef(input)](https://www.runoob.com/pytorch/pytorch-torch-corrcoef.html) | 计算相关系数矩阵。 |
[torch.cov(input, correction, fweights, aweights)](https://www.runoob.com/pytorch/pytorch-torch-cov.html) | 计算协方差矩阵。 |
[torch.cross(input, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cross.html) | 计算叉积。 |
[torch.cummax(input, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cummax.html) | 沿维度累积最大值。 |
[torch.cummin(input, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cummin.html) | 沿维度累积最小值。 |
[torch.cumprod(input, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cumprod.html) | 沿维度累积乘积。 |
[torch.cumsum(input, dim, out, dtype)](https://www.runoob.com/pytorch/pytorch-torch-cumsum.html) | 沿维度累积和。 |
[torch.diag(input, diagonal, out)](https://www.runoob.com/pytorch/pytorch-torch-diag.html) | 创建对角矩阵或提取对角线。 |
[torch.diag_embed(input, offset, dim1, dim2, out)](https://www.runoob.com/pytorch/pytorch-torch-diag_embed.html) | 将输入作为对角线嵌入。 |
[torch.diagflat(input, offset, out)](https://www.runoob.com/pytorch/pytorch-torch-diagflat.html) | 创建对角矩阵(扁平输入)。 |
[torch.diagonal(input, offset, dim1, dim2, out)](https://www.runoob.com/pytorch/pytorch-torch-diagonal.html) | 提取对角线元素。 |
[torch.diff(input, n, dim, prepend, append, out)](https://www.runoob.com/pytorch/pytorch-torch-diff.html) | 计算差分。 |
[torch.einsum(equation, *operands)](https://www.runoob.com/pytorch/pytorch-torch-einsum.html) | 爱因斯坦求和约定。 |
[torch.flatten(input, start_dim, end_dim, out)](https://www.runoob.com/pytorch/pytorch-torch-flatten.html) | 展平张量。 |
[torch.ravel(input, out)](https://www.runoob.com/pytorch/pytorch-torch-ravel.html) | 展平为一维张量。 |
[torch.kron(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-kron.html) | 计算 Kronecker 积。 |
[torch.meshgrid(*tensors, indexing)](https://www.runoob.com/pytorch/pytorch-torch-meshgrid.html) | 创建网格。 |
[torch.lcm(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-lcm.html) | 逐元素最小公倍数。 |
[torch.logcumsumexp(input, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-logcumsumexp.html) | 沿维度累积 log-sum-exp。 |
[torch.renorm(input, p, dim, maxnorm, out)](https://www.runoob.com/pytorch/pytorch-torch-renorm.html) | 重归一化到指定范数。 |
[torch.roll(input, shifts, dims)](https://www.runoob.com/pytorch/pytorch-torch-roll.html) | 滚动张量元素。 |
[torch.searchsorted(sorted_sequence, values, side, sorter)](https://www.runoob.com/pytorch/pytorch-torch-searchsorted.html) | 在排序序列中搜索位置。 |
[torch.tensordot(a, b, dims)](https://www.runoob.com/pytorch/pytorch-torch-tensordot.html) | 计算张量点积。 |
[torch.trace(input, out)](https://www.runoob.com/pytorch/pytorch-torch-trace.html) | 计算矩阵迹。 |
[torch.tril(input, diagonal, out)](https://www.runoob.com/pytorch/pytorch-torch-tril.html) | 提取下三角矩阵。 |
[torch.tril_indices(row, column, offset, dtype, device, layout)](https://www.runoob.com/pytorch/pytorch-torch-tril_indices.html) | 生成下三角索引。 |
[torch.triu(input, diagonal, out)](https://www.runoob.com/pytorch/pytorch-torch-triu.html) | 提取上三角矩阵。 |
[torch.triu_indices(row, column, offset, dtype, device, layout)](https://www.runoob.com/pytorch/pytorch-torch-triu_indices.html) | 生成上三角索引。 |
[torch.unflatten(input, dim, sizes)](https://www.runoob.com/pytorch/pytorch-torch-unflatten.html) | 展开张量。 |
[torch.vander(x, N, increasing, out)](https://www.runoob.com/pytorch/pytorch-torch-vander.html) | 创建 Vandermonde 矩阵。 |
线性代数 (BLAS 和 LAPACK)
| 函数 | 描述 |
|---|---|
[torch.addbmm(input, batch1, batch2, beta, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-addbmm.html) | 批量矩阵乘加。 |
[torch.addmm(input, mat1, mat2, beta, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-addmm.html) | 矩阵乘加。 |
[torch.addmv(input, mat, vec, beta, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-addmv.html) | 矩阵向量乘加。 |
[torch.addr(input, vec1, vec2, beta, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-addr.html) | 向量外积加。 |
[torch.baddbmm(input, batch1, batch2, beta, alpha, out)](https://www.runoob.com/pytorch/pytorch-torch-baddbmm.html) | 批量矩阵乘加(bmm + add)。 |
[torch.bmm(input, mat2, out)](https://www.runoob.com/pytorch/pytorch-torch-bmm.html) | 批量矩阵乘法。 |
[torch.chain_matmul(*matrices)](https://www.runoob.com/pytorch/pytorch-torch-chain_matmul.html) | 链式矩阵乘法。 |
[torch.cholesky(input, upper, out)](https://www.runoob.com/pytorch/pytorch-torch-cholesky.html) | Cholesky 分解。 |
[torch.cholesky_inverse(input, upper, out)](https://www.runoob.com/pytorch/pytorch-torch-cholesky_inverse.html) | Cholesky 分解求逆。 |
[torch.cholesky_solve(input, input2, upper, out)](https://www.runoob.com/pytorch/pytorch-torch-cholesky_solve.html) | Cholesky 分解求解线性方程。 |
[torch.dot(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-dot.html) | 计算两个向量的点积。 |
[torch.geqrf(input, out)](https://www.runoob.com/pytorch/pytorch-torch-geqrf.html) | QR 分解(geqrf)。 |
[torch.ger(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-ger.html) | 计算向量外积。 |
[torch.inner(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-inner.html) | 计算内积。 |
[torch.inverse(input, out)](https://www.runoob.com/pytorch/pytorch-torch-inverse.html) | 计算矩阵的逆。 |
[torch.det(input, out)](https://www.runoob.com/pytorch/pytorch-torch-det.html) | 计算矩阵的行列式。 |
[torch.logdet(input, out)](https://www.runoob.com/pytorch/pytorch-torch-logdet.html) | 计算行列式的对数。 |
[torch.slogdet(input, out)](https://www.runoob.com/pytorch/pytorch-torch-slogdet.html) | 计算行列式的符号和对数绝对值。 |
[torch.lu(input, pivot, get_infos, out)](https://www.runoob.com/pytorch/pytorch-torch-lu.html) | LU 分解。 |
[torch.lu_solve(input, LU_data, LU_pivots, out)](https://www.runoob.com/pytorch/pytorch-torch-lu_solve.html) | LU 分解求解。 |
[torch.lu_unpack(LU_data, LU_pivots, unpack_data, unpack_pivots)](https://www.runoob.com/pytorch/pytorch-torch-lu_unpack.html) | 解包 LU 分解结果。 |
[torch.matmul(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-matmul.html) | 矩阵乘法(支持不同维度)。 |
[torch.matrix_power(input, n, out)](https://www.runoob.com/pytorch/pytorch-torch-matrix_power.html) | 矩阵幂运算。 |
[torch.matrix_exp(input, out)](https://www.runoob.com/pytorch/pytorch-torch-matrix_exp.html) | 矩阵指数。 |
[torch.mm(input, mat2, out)](https://www.runoob.com/pytorch/pytorch-torch-mm.html) | 矩阵乘法(二维)。 |
[torch.mv(input, vec, out)](https://www.runoob.com/pytorch/pytorch-torch-mv.html) | 矩阵向量乘法。 |
[torch.orgqr(input, q, out)](https://www.runoob.com/pytorch/pytorch-torch-orgqr.html) | 从 QR 分解重构 Q。 |
[torch.ormqr(input, mat, vec, left, transpose, out)](https://www.runoob.com/pytorch/pytorch-torch-ormqr.html) | ormqr 操作。 |
[torch.outer(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-outer.html) | 计算向量外积。 |
[torch.pinverse(input, rcond, out)](https://www.runoob.com/pytorch/pytorch-torch-pinverse.html) | 计算Moore-Penrose 伪逆。 |
[torch.qr(input, out)](https://www.runoob.com/pytorch/pytorch-torch-qr.html) | QR 分解。 |
[torch.svd(input, some, compute_uv, out)](https://www.runoob.com/pytorch/pytorch-torch-svd.html) | 奇异值分解。 |
[torch.svd_lowrank(input, q, niter, M)](https://www.runoob.com/pytorch/pytorch-torch-svd_lowrank.html) | 低秩 SVD 近似。 |
[torch.pca_lowrank(input, q, center, niter)](https://www.runoob.com/pytorch/pytorch-torch-pca_lowrank.html) | 低秩 PCA 近似。 |
[torch.lobpcg(input, K, B, X, M, P, max_iter, tol, debug, ortho_iparams, fpfloor)](https://www.runoob.com/pytorch/pytorch-torch-lobpcg.html) | LOBPCG 特征值求解器。 |
[torch.trapz(y, x, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-trapz.html) | 梯形积分(已废弃,使用 trapezoid)。 |
[torch.trapezoid(y, x, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-trapezoid.html) | 梯形积分。 |
[torch.cumulative_trapezoid(y, x, dim, out)](https://www.runoob.com/pytorch/pytorch-torch-cumulative_trapezoid.html) | 累积梯形积分。 |
[torch.triangular_solve(input, A, upper, transpose, unitriangular, out)](https://www.runoob.com/pytorch/pytorch-torch-triangular_solve.html) | 三角矩阵求解。 |
[torch.vdot(input, other, out)](https://www.runoob.com/pytorch/pytorch-torch-vdot.html) | 计算向量点积(复数感知)。 |
设备管理
| 函数 | 描述 |
|---|---|
[torch.cuda.is_available()](https://www.runoob.com/pytorch/pytorch-torch-cuda-is_available.html) | 检查 CUDA 是否可用。 |
[torch.cuda.device_count()](https://www.runoob.com/pytorch/pytorch-torch-cuda-device_count.html) | 返回 CUDA 设备数量。 |
[torch.cuda.current_device()](https://www.runoob.com/pytorch/pytorch-torch-cuda-current_device.html) | 返回当前 CUDA 设备索引。 |
[torch.cuda.device(name)](https://www.runoob.com/pytorch/pytorch-torch-cuda-device.html) | 创建一个设备对象。 |
[torch.cuda.device_context(device)](https://www.runoob.com/pytorch/pytorch-torch-cuda-device_context.html) | 创建设备上下文。 |
[torch.device(device)](https://www.runoob.com/pytorch/pytorch-torch-device.html) | 创建一个设备对象(如 'cpu' 或 'cuda:0')。 |
[torch.Tensor.to(device)](https://www.runoob.com/pytorch/pytorch-torch-Tensor-to.html) | 将张量移动到指定设备。 |
[torch.get_device_module(device_type)](https://www.runoob.com/pytorch/pytorch-torch-get_device_module.html) | 获取设备模块(如 cuda, mps)。 |
并行计算
| 函数 | 描述 |
|---|---|
[torch.get_num_threads()](https://www.runoob.com/pytorch/pytorch-torch-get_num_threads.html) | 获取用于 CPU 操作的总线程数。 |
[torch.set_num_threads(int)](https://www.runoob.com/pytorch/pytorch-torch-set_num_threads.html) | 设置用于 CPU 操作的线程数。 |
[torch.get_num_interop_threads()](https://www.runoob.com/pytorch/pytorch-torch-get_num_interop_threads.html) | 获取 inter-op 并行线程数。 |
[torch.set_num_interop_threads(int)](https://www.runoob.com/pytorch/pytorch-torch-set_num_interop_threads.html) | 设置 inter-op 并行线程数。 |
工具函数
| 函数 | 描述 |
|---|---|
[torch.compiled_with_cxx11_abi()](https://www.runoob.com/pytorch/pytorch-torch-compiled_with_cxx11_abi.html) | 检查是否使用 C++11 ABI 编译。 |
[torch.result_type(tensor, other)](https://www.runoob.com/pytorch/pytorch-torch-result_type.html) | 返回操作结果的 dtype。 |
[torch.can_cast(from_dtype, to_dtype)](https://www.runoob.com/pytorch/pytorch-torch-can_cast.html) | 检查是否可以转换数据类型。 |
[torch.promote_types(type1, type2)](https://www.runoob.com/pytorch/pytorch-torch-promote_types.html) | 返回提升后的数据类型。 |
[torch.use_deterministic_algorithms(mode, warn_only)](https://www.runoob.com/pytorch/pytorch-torch-use_deterministic_algorithms.html) | 启用/禁用确定性算法。 |
[torch.are_deterministic_algorithms_enabled()](https://www.runoob.com/pytorch/pytorch-torch-are_deterministic_algorithms_enabled.html) | 检查是否启用确定性算法。 |
[torch.is_deterministic_algorithms_warn_only_enabled()](https://www.runoob.com/pytorch/pytorch-torch-is_deterministic_algorithms_warn_only_enabled.html) | 检查确定性算法是否为警告模式。 |
[torch.set_deterministic_debug_mode(debug_mode)](https://www.runoob.com/pytorch/pytorch-torch-set_deterministic_debug_mode.html) | 设置确定性调试模式。 |
[torch.get_deterministic_debug_mode()](https://www.runoob.com/pytorch/pytorch-torch-get_deterministic_debug_mode.html) | 获取确定性调试模式。 |
[torch.set_float32_matmul_precision(precision)](https://www.runoob.com/pytorch/pytorch-torch-set_float32_matmul_precision.html) | 设置 float32 矩阵乘法的精度。 |
[torch.get_float32_matmul_precision()](https://www.runoob.com/pytorch/pytorch-torch-get_float32_matmul_precision.html) | 获取 float32 矩阵乘法的精度。 |
[torch.set_warn_always(enabled)](https://www.runoob.com/pytorch/pytorch-torch-set_warn_always.html) | 设置是否始终显示警告。 |
[torch.is_warn_always_enabled()](https://www.runoob.com/pytorch/pytorch-torch-is_warn_always_enabled.html) | 检查是否始终显示警告。 |
[torch.vmap(fn, in_dims, out_dims, randomness, chunk_size)](https://www.runoob.com/pytorch/pytorch-torch-vmap.html) | 向量化映射。 |
[torch._assert(condition, message)](https://www.runoob.com/pytorch/pytorch-torch-_assert.html) | 断言检查(内部使用)。 |
[torch.typename(t)](https://www.runoob.com/pytorch/pytorch-torch-typename.html) | 返回类型的字符串表示。 |
编译优化
| 函数 | 描述 |
|---|---|
[torch.compile(model, backend, options, dynamic)](https://www.runoob.com/pytorch/pytorch-torch-compile.html) | 编译 PyTorch 模型进行优化。 |
实例
实例
python
import torch
# 创建张量
x = torch.tensor([1, 2, 3])
y = torch.zeros(2, 3)
# 数学运算
z = torch.add(x, 1) # 逐元素加 1
print(z)
# 索引和切片
mask = x > 1
selected = torch.masked_select(x, mask)
print(selected)
# 设备管理
if torch.cuda.is_available():
device = torch.device('cuda')
x = x.to(device)
print(x.device)
# 矩阵运算
a = torch.randn(3, 4)
b = torch.randn(4, 5)
c = torch.matmul(a, b)
print(c.shape)
# 梯度计算
x = torch.tensor([1., 2., 3.], requires_grad=True)
y = x.sum()
y.backward()
print(x.grad)输出结果:
python
tensor([2, 3, 4])
tensor([2, 3])如果需要更详细的信息,可以参考 PyTorch 官方文档。
AI 思考中...