目录

torch.Tensor

一个torch.Tensor是一个多维矩阵,其中包含 单个数据类型。

数据类型

Torch 使用以下数据类型定义张量类型:

数据类型

DTYPE

32 位浮点

torch.float32torch.float

64 位浮点

torch.float64torch.double

16 位浮点 1

torch.float16torch.half

16 位浮点 2

torch.bfloat16

32 位复杂

torch.complex32torch.chalf

64 位复杂

torch.complex64torch.cfloat

128 位复杂

torch.complex128torch.cdouble

8 位整数 (无符号)

torch.uint8

16 位整数 (无符号)

torch.uint16(有限支持)4

32 位整数 (无符号)

torch.uint32(有限支持)4

64 位整数 (无符号)

torch.uint64(有限支持)4

8 位整数 (有符号)

torch.int8

16 位整数(有符号)

torch.int16torch.short

32 位整数(有符号)

torch.int32torch.int

64 位整数 (有符号)

torch.int64torch.long

布尔

torch.bool

量化 8 位整数 (unsigned)

torch.quint8

量化 8 位整数(有符号)

torch.qint8

量化 32 位整数(有符号)

torch.qint32

量化 4 位整数(无符号) 3

torch.quint4x2

8 位浮点,E4M3 5

torch.float8_e4m3fn(有限支持)

8 位浮点,e5m2 5

torch.float8_e5m2(有限支持)

1

有时称为 binary16:使用 1 个符号、5 个指数和 10 significand 位。当精度很重要但以牺牲范围为代价时很有用。

2

有时称为大脑浮点:使用 1 个符号、8 个指数和 7 significand 位。当 range 很重要时很有用,因为它具有相同的 指数位数为float32

3

量化的 4 位整数存储为 8 位有符号整数。目前,它仅在 EmbeddingBag 运算符中受支持。

41,2,3

除 之外的 unsigned types 目前计划只具有 在 EAGER 模式下的支持有限(它们主要用于帮助使用 torch.compile 的 Package);如果您需要热切的支持并且不需要额外的范围, 我们建议改用他们的 signed variants。有关详细信息,请参阅 https://github.com/pytorch/pytorch/issues/58734uint8

51,2

torch.float8_e4m3fn并实现 8 位的规范 https://arxiv.org/abs/2209.05433 浮点类型。op 支持 非常有限。torch.float8_e5m2

为了向后兼容,我们支持以下备用类名 对于这些数据类型:

数据类型

CPU 张量

GPU 张量

32 位浮点

torch.FloatTensor

torch.cuda.FloatTensor

64 位浮点

torch.DoubleTensor

torch.cuda.DoubleTensor

16 位浮点

torch.HalfTensor

torch.cuda.HalfTensor

16 位浮点

torch.BFloat16Tensor

torch.cuda.BFloat16Tensor

8 位整数 (无符号)

torch.ByteTensor

torch.cuda.ByteTensor

8 位整数 (有符号)

torch.CharTensor

torch.cuda.CharTensor

16 位整数(有符号)

torch.ShortTensor

torch.cuda.ShortTensor

32 位整数(有符号)

torch.IntTensor

torch.cuda.IntTensor

64 位整数 (有符号)

torch.LongTensor

torch.cuda.LongTensor

布尔

torch.BoolTensor

torch.cuda.BoolTensor

但是,要构造张量,我们建议使用工厂函数,例如torch.empty()替换为参数。这dtypetorch.Tensorconstructor 是默认张量类型的别名 ().torch.FloatTensor

初始化和基本作

可以从 Python 构造张量list或使用torch.tensor()构造 函数:

>>> torch.tensor([[1., -1.], [1., -1.]])
tensor([[ 1.0000, -1.0000],
        [ 1.0000, -1.0000]])
>>> torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])

警告

torch.tensor()始终复制 .如果你有一个 Tensor 并且只想更改它的标志,请使用datadatarequires_gradrequires_grad_()detach()以避免复制。 如果您有一个 numpy 数组并希望避免复制,请使用torch.as_tensor().

可以通过传递torch.dtype和/或torch.device更改为 构造函数或张量创建作:

>>> torch.zeros([2, 4], dtype=torch.int32)
tensor([[ 0,  0,  0,  0],
        [ 0,  0,  0,  0]], dtype=torch.int32)
>>> cuda0 = torch.device('cuda:0')
>>> torch.ones([2, 4], dtype=torch.float64, device=cuda0)
tensor([[ 1.0000,  1.0000,  1.0000,  1.0000],
        [ 1.0000,  1.0000,  1.0000,  1.0000]], dtype=torch.float64, device='cuda:0')

有关构建 Tensor 的更多信息,请参阅创建作

可以使用 Python 的索引来访问和修改张量的内容 和 slicing 表示法:

>>> x = torch.tensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
tensor(6)
>>> x[0][1] = 8
>>> print(x)
tensor([[ 1,  8,  3],
        [ 4,  5,  6]])

torch.Tensor.item()从包含 单个值:

>>> x = torch.tensor([[1]])
>>> x
tensor([[ 1]])
>>> x.item()
1
>>> x = torch.tensor(2.5)
>>> x
tensor(2.5000)
>>> x.item()
2.5

有关索引的更多信息,请参阅索引、切片、联接、更改运算

可以使用 创建 Tensor,以便requires_grad=Truetorch.autograd记录对它们的作以进行自动区分。

>>> x = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> out = x.pow(2).sum()
>>> out.backward()
>>> x.grad
tensor([[ 2.0000, -2.0000],
        [ 2.0000,  2.0000]])

每个张量都有一个关联的 ,用于保存其数据。 tensor 类还提供存储的多维跨步视图,并定义对其的数值运算。torch.Storage

注意

有关张量视图的更多信息,请参阅张量视图

注意

有关torch.dtype,torch.devicetorch.layout的属性torch.Tensor,请参阅 Tensor Attributes

注意

改变张量的方法标有下划线后缀。 例如,计算 absolute 值 就地返回修改后的张量,同时在新张量中计算结果。torch.FloatTensor.abs_()torch.FloatTensor.abs()

注意

要更改现有张量的torch.device和/或torch.dtype,请考虑使用to()方法。

警告

当前torch.Tensor引入了内存开销, 因此,这可能会导致具有许多微小张量的应用程序中出现意外的高内存使用率。 如果这是您的情况,请考虑使用一个大型结构。

Tensor 类参考

Torch 的 Torch 类张肌

创建张量有几种主要方法,具体取决于您的使用案例。

  • 要使用预先存在的数据创建张量,请使用torch.tensor().

  • 要创建具有特定大小的张量,请使用 Tensor creation ops (请参阅 创建作)。torch.*

  • 要创建与另一个张量大小相同(和类似类型)的张量,请执行以下作: 使用 Tensor Creation Ops (参见 Creation Ops)。torch.*_like

  • 要创建与另一个张量类型相似但大小不同的张量,请执行以下作: 使用 Creation Ops。tensor.new_*

张肌。

返回此张量的视图,其维度反转。

如果 是 中的维度数,则 等效于 。nxx.Tx.permute(n-1, n-2, ..., 0)

警告

的使用Tensor.T()在 2 维以外的张量上反转其形状 已弃用,它将在未来发行版中引发错误。考虑mT转置矩阵批次,或 x.permute(*torch.arange(x.ndim - 1, -1, -1)) 反向 张量的维度。

张肌。H

返回共轭和转置的矩阵(二维张量)的视图。

x.H等效于 对于复矩阵和对于实矩阵。x.transpose(0, 1).conj()x.transpose(0, 1)

另请参阅

mH:也适用于矩阵批次的属性。

张肌。兆吨

返回此张量的视图,其中最后两个维度已转置。

x.mT等效于 。x.transpose(-2, -1)

张肌。mH

访问此属性等效于调用adjoint().

Tensor.new_tensor

返回一个新的 Tensor 作为张量数据。data

Tensor.new_full

返回一个大小为 的 Tensor,其中填充了 。sizefill_value

Tensor.new_empty

返回一个大小为填充了未初始化数据的 Tensor。size

Tensor.new_ones

返回一个大小为 的 Tensor,其中填充了 。size1

Tensor.new_zeros

返回一个大小为 的 Tensor,其中填充了 。size0

Tensor.is_cuda

如果 Tensor 存储在 GPU 上,否则。TrueFalse

Tensor.is_quantized

如果 Tensor 被量化,否则。TrueFalse

Tensor.is_meta

如果 Tensor 是元张量,否则。TrueFalse

Tensor.device

torch.device其中 Tensor 所在的位置。

Tensor.grad

默认情况下,此属性在第一次调用计算 的梯度时变为 Tensor。Nonebackward()self

Tensor.ndim

别名dim()

Tensor.real

返回一个新张量,其中包含复值输入张量的张量的实数值。self

Tensor.imag

返回包含张量的虚数值的新张量。self

Tensor.nbytes

如果 Tensor 不使用稀疏存储布局,则返回 Tensor 元素的“视图”消耗的字节数。

Tensor.itemsize

别名element_size()

Tensor.abs

torch.abs()

Tensor.abs_

的就地版本abs()

Tensor.absolute

别名abs()

Tensor.absolute_

的就地版本absolute()别名abs_()

Tensor.acos

torch.acos()

Tensor.acos_

的就地版本acos()

Tensor.arccos

torch.arccos()

Tensor.arccos_

的就地版本arccos()

Tensor.add

将标量或张量添加到 tensor。self

Tensor.add_

的就地版本add()

Tensor.addbmm

torch.addbmm()

Tensor.addbmm_

的就地版本addbmm()

Tensor.addcdiv

torch.addcdiv()

Tensor.addcdiv_

的就地版本addcdiv()

Tensor.addcmul

torch.addcmul()

Tensor.addcmul_

的就地版本addcmul()

Tensor.addmm

torch.addmm()

Tensor.addmm_

的就地版本addmm()

Tensor.sspaddmm

torch.sspaddmm()

Tensor.addmv

torch.addmv()

Tensor.addmv_

的就地版本addmv()

Tensor.addr

torch.addr()

Tensor.addr_

的就地版本addr()

Tensor.adjoint

别名adjoint()

Tensor.allclose

torch.allclose()

Tensor.amax

torch.amax()

Tensor.amin

torch.amin()

Tensor.aminmax

torch.aminmax()

Tensor.angle

torch.angle()

Tensor.apply_

将函数应用于张量中的每个元素,将每个元素替换为 返回的值。callablecallable

Tensor.argmax

torch.argmax()

Tensor.argmin

torch.argmin()

Tensor.argsort

torch.argsort()

Tensor.argwhere

torch.argwhere()

Tensor.asin

torch.asin()

Tensor.asin_

的就地版本asin()

Tensor.arcsin

torch.arcsin()

Tensor.arcsin_

的就地版本arcsin()

Tensor.as_strided

torch.as_strided()

Tensor.atan

torch.atan()

Tensor.atan_

的就地版本atan()

Tensor.arctan

torch.arctan()

Tensor.arctan_

的就地版本arctan()

Tensor.atan2

torch.atan2()

Tensor.atan2_

的就地版本atan2()

Tensor.arctan2

torch.arctan2()

Tensor.arctan2_

atan2_(其他) -> 张量

Tensor.all

torch.all()

Tensor.any

torch.any()

Tensor.backward

计算当前张量 wrt 图叶子的梯度。

Tensor.baddbmm

torch.baddbmm()

Tensor.baddbmm_

的就地版本baddbmm()

Tensor.bernoulli

返回一个结果张量,其中每个结果[i]\texttt{result[i]}独立采样自伯努利(自我[i])\text{Bernoulli}(\texttt{self[i]}).

Tensor.bernoulli_

用独立的样品填充 的每个位置self伯努利(p)\text{Bernoulli}(\texttt{p}).

Tensor.bfloat16

self.bfloat16()等效于 。self.to(torch.bfloat16)

Tensor.bincount

torch.bincount()

Tensor.bitwise_not

torch.bitwise_not()

Tensor.bitwise_not_

的就地版本bitwise_not()

Tensor.bitwise_and

torch.bitwise_and()

Tensor.bitwise_and_

的就地版本bitwise_and()

Tensor.bitwise_or

torch.bitwise_or()

Tensor.bitwise_or_

的就地版本bitwise_or()

Tensor.bitwise_xor

torch.bitwise_xor()

Tensor.bitwise_xor_

的就地版本bitwise_xor()

Tensor.bitwise_left_shift

torch.bitwise_left_shift()

Tensor.bitwise_left_shift_

的就地版本bitwise_left_shift()

Tensor.bitwise_right_shift

torch.bitwise_right_shift()

Tensor.bitwise_right_shift_

的就地版本bitwise_right_shift()

Tensor.bmm

torch.bmm()

Tensor.bool

self.bool()等效于 。self.to(torch.bool)

Tensor.byte

self.byte()等效于 。self.to(torch.uint8)

Tensor.broadcast_to

torch.broadcast_to().

Tensor.cauchy_

用从 Cauchy 分布中提取的数字填充张量:

Tensor.ceil

torch.ceil()

Tensor.ceil_

的就地版本ceil()

Tensor.char

self.char()等效于 。self.to(torch.int8)

Tensor.cholesky

torch.cholesky()

Tensor.cholesky_inverse

torch.cholesky_inverse()

Tensor.cholesky_solve

torch.cholesky_solve()

Tensor.chunk

torch.chunk()

Tensor.clamp

torch.clamp()

Tensor.clamp_

的就地版本clamp()

Tensor.clip

别名clamp().

Tensor.clip_

别名clamp_().

Tensor.clone

torch.clone()

Tensor.contiguous

返回包含与 tensor 相同数据的 contiguous in memory tensor。self

Tensor.copy_

将元素从 复制到 tensor 中并返回 。srcselfself

Tensor.conj

torch.conj()

Tensor.conj_physical

torch.conj_physical()

Tensor.conj_physical_

的就地版本conj_physical()

Tensor.resolve_conj

torch.resolve_conj()

Tensor.resolve_neg

torch.resolve_neg()

Tensor.copysign

torch.copysign()

Tensor.copysign_

的就地版本copysign()

Tensor.cos

torch.cos()

Tensor.cos_

的就地版本cos()

Tensor.cosh

torch.cosh()

Tensor.cosh_

的就地版本cosh()

Tensor.corrcoef

torch.corrcoef()

Tensor.count_nonzero

torch.count_nonzero()

Tensor.cov

torch.cov()

Tensor.acosh

torch.acosh()

Tensor.acosh_

的就地版本acosh()

Tensor.arccosh

acosh() -> 张量

Tensor.arccosh_

acosh_() -> 张量

Tensor.cpu

返回 CPU 内存中此对象的副本。

Tensor.cross

torch.cross()

Tensor.cuda

返回 CUDA 内存中此对象的副本。

Tensor.logcumsumexp

torch.logcumsumexp()

Tensor.cummax

torch.cummax()

Tensor.cummin

torch.cummin()

Tensor.cumprod

torch.cumprod()

Tensor.cumprod_

的就地版本cumprod()

Tensor.cumsum

torch.cumsum()

Tensor.cumsum_

的就地版本cumsum()

Tensor.chalf

self.chalf()等效于 。self.to(torch.complex32)

Tensor.cfloat

self.cfloat()等效于 。self.to(torch.complex64)

Tensor.cdouble

self.cdouble()等效于 。self.to(torch.complex128)

Tensor.data_ptr

返回 tensor 的第一个元素的地址。self

Tensor.deg2rad

torch.deg2rad()

Tensor.dequantize

给定一个量化的 Tensor,对其进行反量化并返回反量化的 float Tensor。

Tensor.det

torch.det()

Tensor.dense_dim

返回 sparse tensor 中的密集维度数。self

Tensor.detach

返回与当前图形分离的新 Tensor。

Tensor.detach_

将 Tensor 与创建它的图形分离,使其成为叶子。

Tensor.diag

torch.diag()

Tensor.diag_embed

torch.diag_embed()

Tensor.diagflat

torch.diagflat()

Tensor.diagonal

torch.diagonal()

Tensor.diagonal_scatter

torch.diagonal_scatter()

Tensor.fill_diagonal_

填充至少具有 2 维的张量的主对角线。

Tensor.fmax

torch.fmax()

Tensor.fmin

torch.fmin()

Tensor.diff

torch.diff()

Tensor.digamma

torch.digamma()

Tensor.digamma_

的就地版本digamma()

Tensor.dim

返回 tensor 的维数。self

Tensor.dim_order

返回一个 int 元组,描述 的暗序或物理布局。self

Tensor.dist

torch.dist()

Tensor.div

torch.div()

Tensor.div_

的就地版本div()

Tensor.divide

torch.divide()

Tensor.divide_

的就地版本divide()

Tensor.dot

torch.dot()

Tensor.double

self.double()等效于 。self.to(torch.float64)

Tensor.dsplit

torch.dsplit()

Tensor.element_size

返回单个元素的大小(以字节为单位)。

Tensor.eq

torch.eq()

Tensor.eq_

的就地版本eq()

Tensor.equal

torch.equal()

Tensor.erf

torch.erf()

Tensor.erf_

的就地版本erf()

Tensor.erfc

torch.erfc()

Tensor.erfc_

的就地版本erfc()

Tensor.erfinv

torch.erfinv()

Tensor.erfinv_

的就地版本erfinv()

Tensor.exp

torch.exp()

Tensor.exp_

的就地版本exp()

Tensor.expm1

torch.expm1()

Tensor.expm1_

的就地版本expm1()

Tensor.expand

返回张量的新视图,其中单例维度扩展为更大的大小。self

Tensor.expand_as

将此张量扩展为与 相同的大小。other

Tensor.exponential_

用从 PDF 中提取的元素填充张量(概率密度函数):self

Tensor.fix

torch.fix().

Tensor.fix_

的就地版本fix()

Tensor.fill_

用指定的值填充 tensor。self

Tensor.flatten

torch.flatten()

Tensor.flip

torch.flip()

Tensor.fliplr

torch.fliplr()

Tensor.flipud

torch.flipud()

Tensor.float

self.float()等效于 。self.to(torch.float32)

Tensor.float_power

torch.float_power()

Tensor.float_power_

的就地版本float_power()

Tensor.floor

torch.floor()

Tensor.floor_

的就地版本floor()

Tensor.floor_divide

torch.floor_divide()

Tensor.floor_divide_

的就地版本floor_divide()

Tensor.fmod

torch.fmod()

Tensor.fmod_

的就地版本fmod()

Tensor.frac

torch.frac()

Tensor.frac_

的就地版本frac()

Tensor.frexp

torch.frexp()

Tensor.gather

torch.gather()

Tensor.gcd

torch.gcd()

Tensor.gcd_

的就地版本gcd()

Tensor.ge

torch.ge().

Tensor.ge_

的就地版本ge().

Tensor.greater_equal

torch.greater_equal().

Tensor.greater_equal_

的就地版本greater_equal().

Tensor.geometric_

用从几何分布中提取的元素填充张量:self

Tensor.geqrf

torch.geqrf()

Tensor.ger

torch.ger()

Tensor.get_device

对于 CUDA 张量,此函数返回张量所在的 GPU 的设备序号。

Tensor.gt

torch.gt().

Tensor.gt_

的就地版本gt().

Tensor.greater

torch.greater().

Tensor.greater_

的就地版本greater().

Tensor.half

self.half()等效于 。self.to(torch.float16)

Tensor.hardshrink

torch.nn.functional.hardshrink()

Tensor.heaviside

torch.heaviside()

Tensor.histc

torch.histc()

Tensor.histogram

torch.histogram()

Tensor.hsplit

torch.hsplit()

Tensor.hypot

torch.hypot()

Tensor.hypot_

的就地版本hypot()

Tensor.i0

torch.i0()

Tensor.i0_

的就地版本i0()

Tensor.igamma

torch.igamma()

Tensor.igamma_

的就地版本igamma()

Tensor.igammac

torch.igammac()

Tensor.igammac_

的就地版本igammac()

Tensor.index_add_

按照 中给出的顺序添加到索引中,将 times 的元素累积到张量中。alphasourceselfindex

Tensor.index_add

的 Out-of-place versiontorch.Tensor.index_add_().

Tensor.index_copy_

复制 的元素tensor通过按照 中给定的顺序选择索引,将其放入张量中。selfindex

Tensor.index_copy

的 Out-of-place versiontorch.Tensor.index_copy_().

Tensor.index_fill_

通过按照 中给出的顺序选择索引,用 value 填充张量的元素。selfvalueindex

Tensor.index_fill

的 Out-of-place versiontorch.Tensor.index_fill_().

Tensor.index_put_

使用 中指定的索引(这是一个 Tensor 元组)将 tensor 中的值放入 tensor 中。valuesselfindices

Tensor.index_put

的 Out-place 版本index_put_().

Tensor.index_reduce_

通过使用参数给出的 reduction 给出的顺序累积到索引,将 的元素累积到张量中。sourceselfindexreduce

Tensor.index_reduce

Tensor.index_select

torch.index_select()

Tensor.indices

返回稀疏 COO 张量的 indices 张量。

Tensor.inner

torch.inner().

Tensor.int

self.int()等效于 。self.to(torch.int32)

Tensor.int_repr

给定一个量化的 Tensor,返回一个数据类型为 uint8_t 的 CPU Tensor,用于存储给定 Tensor 的基础 uint8_t 值。self.int_repr()

Tensor.inverse

torch.inverse()

Tensor.isclose

torch.isclose()

Tensor.isfinite

torch.isfinite()

Tensor.isinf

torch.isinf()

Tensor.isposinf

torch.isposinf()

Tensor.isneginf

torch.isneginf()

Tensor.isnan

torch.isnan()

Tensor.is_contiguous

如果 tensor 在内存中按内存格式指定的顺序连续,则返回 True。self

Tensor.is_complex

如果 的数据类型是复杂数据类型,则返回 True。self

Tensor.is_conj

如果共轭位 设置为 true,则返回 True。self

Tensor.is_floating_point

如果 的数据类型是浮点数据类型,则返回 True。self

Tensor.is_inference

torch.is_inference()

Tensor.is_leaf

按照约定,所有具有 which is 的 Tensor 都将是叶 Tensor。requires_gradFalse

Tensor.is_pinned

如果此张量驻留在固定内存中,则返回 true。

Tensor.is_set_to

如果两个张量都指向完全相同的内存(相同的存储、偏移量、大小和步幅),则返回 True。

Tensor.is_shared

检查 tensor 是否在共享内存中。

Tensor.is_signed

如果 的数据类型是有符号数据类型,则返回 True。self

Tensor.is_sparse

如果 Tensor 使用稀疏 COO 存储布局,否则。TrueFalse

Tensor.istft

torch.istft()

Tensor.isreal

torch.isreal()

Tensor.item

将此张量的值作为标准 Python 数字返回。

Tensor.kthvalue

torch.kthvalue()

Tensor.lcm

torch.lcm()

Tensor.lcm_

的就地版本lcm()

Tensor.ldexp

torch.ldexp()

Tensor.ldexp_

的就地版本ldexp()

Tensor.le

torch.le().

Tensor.le_

的就地版本le().

Tensor.less_equal

torch.less_equal().

Tensor.less_equal_

的就地版本less_equal().

Tensor.lerp

torch.lerp()

Tensor.lerp_

的就地版本lerp()

Tensor.lgamma

torch.lgamma()

Tensor.lgamma_

的就地版本lgamma()

Tensor.log

torch.log()

Tensor.log_

的就地版本log()

Tensor.logdet

torch.logdet()

Tensor.log10

torch.log10()

Tensor.log10_

的就地版本log10()

Tensor.log1p

torch.log1p()

Tensor.log1p_

的就地版本log1p()

Tensor.log2

torch.log2()

Tensor.log2_

的就地版本log2()

Tensor.log_normal_

用来自对数正态分布的数字样本填充张量,该分布由给定均值参数化selfμ\mu和标准差σ\sigma.

Tensor.logaddexp

torch.logaddexp()

Tensor.logaddexp2

torch.logaddexp2()

Tensor.logsumexp

torch.logsumexp()

Tensor.logical_and

torch.logical_and()

Tensor.logical_and_

的就地版本logical_and()

Tensor.logical_not

torch.logical_not()

Tensor.logical_not_

的就地版本logical_not()

Tensor.logical_or

torch.logical_or()

Tensor.logical_or_

的就地版本logical_or()

Tensor.logical_xor

torch.logical_xor()

Tensor.logical_xor_

的就地版本logical_xor()

Tensor.logit

torch.logit()

Tensor.logit_

的就地版本logit()

Tensor.long

self.long()等效于 。self.to(torch.int64)

Tensor.lt

torch.lt().

Tensor.lt_

的就地版本lt().

Tensor.less

lt(other) -> 张量

Tensor.less_

的就地版本less().

Tensor.lu

torch.lu()

Tensor.lu_solve

torch.lu_solve()

Tensor.as_subclass

创建具有与 相同的数据指针的实例。clsself

Tensor.map_

适用于 tensor 中的每个元素和给定的callableselftensor并将结果存储在 Tensor 中。self

Tensor.masked_scatter_

将元素从 复制到 tensor 中 为 True 的位置。sourceselfmask

Tensor.masked_scatter

的 Out-of-place versiontorch.Tensor.masked_scatter_()

Tensor.masked_fill_

用 where 为 True 填充 tensor 的元素。selfvaluemask

Tensor.masked_fill

的 Out-of-place versiontorch.Tensor.masked_fill_()

Tensor.masked_select

torch.masked_select()

Tensor.matmul

torch.matmul()

Tensor.matrix_power

注意

matrix_power()已弃用,请使用torch.linalg.matrix_power()相反。

Tensor.matrix_exp

torch.matrix_exp()

Tensor.max

torch.max()

Tensor.maximum

torch.maximum()

Tensor.mean

torch.mean()

Tensor.module_load

定义在将其加载到otherselfload_state_dict().

Tensor.nanmean

torch.nanmean()

Tensor.median

torch.median()

Tensor.nanmedian

torch.nanmedian()

Tensor.min

torch.min()

Tensor.minimum

torch.minimum()

Tensor.mm

torch.mm()

Tensor.smm

torch.smm()

Tensor.mode

torch.mode()

Tensor.movedim

torch.movedim()

Tensor.moveaxis

torch.moveaxis()

Tensor.msort

torch.msort()

Tensor.mul

torch.mul().

Tensor.mul_

的就地版本mul().

Tensor.multiply

torch.multiply().

Tensor.multiply_

的就地版本multiply().

Tensor.multinomial

torch.multinomial()

Tensor.mv

torch.mv()

Tensor.mvlgamma

torch.mvlgamma()

Tensor.mvlgamma_

的就地版本mvlgamma()

Tensor.nansum

torch.nansum()

Tensor.narrow

torch.narrow().

Tensor.narrow_copy

torch.narrow_copy().

Tensor.ndimension

别名dim()

Tensor.nan_to_num

torch.nan_to_num().

Tensor.nan_to_num_

的就地版本nan_to_num().

Tensor.ne

torch.ne().

Tensor.ne_

的就地版本ne().

Tensor.not_equal

torch.not_equal().

Tensor.not_equal_

的就地版本not_equal().

Tensor.neg

torch.neg()

Tensor.neg_

的就地版本neg()

Tensor.negative

torch.negative()

Tensor.negative_

的就地版本negative()

Tensor.nelement

别名numel()

Tensor.nextafter

torch.nextafter()

Tensor.nextafter_

的就地版本nextafter()

Tensor.nonzero

torch.nonzero()

Tensor.norm

torch.norm()

Tensor.normal_

用来自正态分布的元素样本填充张量,参数化为selfmeanstd.

Tensor.numel

torch.numel()

Tensor.numpy

将张量作为 NumPy 返回。ndarray

Tensor.orgqr

torch.orgqr()

Tensor.ormqr

torch.ormqr()

Tensor.outer

torch.outer().

Tensor.permute

torch.permute()

Tensor.pin_memory

将张量复制到固定内存(如果尚未固定)。

Tensor.pinverse

torch.pinverse()

Tensor.polygamma

torch.polygamma()

Tensor.polygamma_

的就地版本polygamma()

Tensor.positive

torch.positive()

Tensor.pow

torch.pow()

Tensor.pow_

的就地版本pow()

Tensor.prod

torch.prod()

Tensor.put_

将元素从 复制到 中指定的位置。sourceindex

Tensor.qr

torch.qr()

Tensor.qscheme

返回给定 QTensor 的量化方案。

Tensor.quantile

torch.quantile()

Tensor.nanquantile

torch.nanquantile()

Tensor.q_scale

给定一个通过线性(仿射)量化量化的 Tensor,返回底层 quantizer() 的 scale 。

Tensor.q_zero_point

给定一个通过线性(仿射)量化量化的 Tensor,返回底层 quantizer() 的 zero_point。

Tensor.q_per_channel_scales

给定一个通过线性(仿射)每通道量化量化的 Tensor,返回底层量化器的尺度的 Tensor。

Tensor.q_per_channel_zero_points

给定一个通过线性(仿射)每通道量化量化的 Tensor,返回底层量化器的 zero_points 张量。

Tensor.q_per_channel_axis

给定一个通过线性(仿射)每通道量化量化的 Tensor,返回应用每通道量化的维度索引。

Tensor.rad2deg

torch.rad2deg()

Tensor.random_

用从离散均匀分布中采样的数字填充张量。self[from, to - 1]

Tensor.ravel

torch.ravel()

Tensor.reciprocal

torch.reciprocal()

Tensor.reciprocal_

的就地版本reciprocal()

Tensor.record_stream

将张量标记为此流已使用。

Tensor.register_hook

注册一个向后钩子。

Tensor.register_post_accumulate_grad_hook

注册一个在 grad 累积后运行的 backward hook。

Tensor.remainder

torch.remainder()

Tensor.remainder_

的就地版本remainder()

Tensor.renorm

torch.renorm()

Tensor.renorm_

的就地版本renorm()

Tensor.repeat

沿指定维度重复此张量。

Tensor.repeat_interleave

torch.repeat_interleave().

Tensor.requires_grad

是如果需要为此 Tensor 计算梯度,否则。TrueFalse

Tensor.requires_grad_

更改 autograd 是否应该记录对此张量的作:就地设置此张量的属性。requires_grad

Tensor.reshape

返回一个张量,其数据和元素数与指定形状相同。self

Tensor.reshape_as

返回与 相同的形状的此张量。other

Tensor.resize_

将 tensor 大小调整为指定大小。self

Tensor.resize_as_

将张量大小调整为与指定大小相同的selftensor.

Tensor.retain_grad

允许此 Tensor 在 期间填充其 。gradbackward()

Tensor.retains_grad

如果此 Tensor 是非叶子的,并且允许在 期间填充它,否则。Truegradbackward()False

Tensor.roll

torch.roll()

Tensor.rot90

torch.rot90()

Tensor.round

torch.round()

Tensor.round_

的就地版本round()

Tensor.rsqrt

torch.rsqrt()

Tensor.rsqrt_

的就地版本rsqrt()

Tensor.scatter

的 Out-of-place versiontorch.Tensor.scatter_()

Tensor.scatter_

将张量中的所有值写入张量中指定的索引处。srcselfindex

Tensor.scatter_add_

将张量中的所有值添加到张量中指定的索引处,其方式与srcselfindexscatter_().

Tensor.scatter_add

的 Out-of-place versiontorch.Tensor.scatter_add_()

Tensor.scatter_reduce_

使用通过参数 (、) 定义的应用缩减,将张量中的所有值减少到张量中张量中指定的索引。srcindexselfreduce"sum""prod""mean""amax""amin"

Tensor.scatter_reduce

的 Out-of-place versiontorch.Tensor.scatter_reduce_()

Tensor.select

torch.select()

Tensor.select_scatter

torch.select_scatter()

Tensor.set_

设置底层存储、大小和步幅。

Tensor.share_memory_

将底层存储移动到共享内存。

Tensor.short

self.short()等效于 。self.to(torch.int16)

Tensor.sigmoid

torch.sigmoid()

Tensor.sigmoid_

的就地版本sigmoid()

Tensor.sign

torch.sign()

Tensor.sign_

的就地版本sign()

Tensor.signbit

torch.signbit()

Tensor.sgn

torch.sgn()

Tensor.sgn_

的就地版本sgn()

Tensor.sin

torch.sin()

Tensor.sin_

的就地版本sin()

Tensor.sinc

torch.sinc()

Tensor.sinc_

的就地版本sinc()

Tensor.sinh

torch.sinh()

Tensor.sinh_

的就地版本sinh()

Tensor.asinh

torch.asinh()

Tensor.asinh_

的就地版本asinh()

Tensor.arcsinh

torch.arcsinh()

Tensor.arcsinh_

的就地版本arcsinh()

Tensor.shape

返回张量的大小。self

Tensor.size

返回张量的大小。self

Tensor.slogdet

torch.slogdet()

Tensor.slice_scatter

torch.slice_scatter()

Tensor.softmax

别名torch.nn.functional.softmax().

Tensor.sort

torch.sort()

Tensor.split

torch.split()

Tensor.sparse_mask

返回一个新的稀疏张量,其中包含由 sparse 张量的索引过滤的跨步张量中的值。selfmask

Tensor.sparse_dim

返回稀疏张量中的稀疏维度数。self

Tensor.sqrt

torch.sqrt()

Tensor.sqrt_

的就地版本sqrt()

Tensor.square

torch.square()

Tensor.square_

的就地版本square()

Tensor.squeeze

torch.squeeze()

Tensor.squeeze_

的就地版本squeeze()

Tensor.std

torch.std()

Tensor.stft

torch.stft()

Tensor.storage

返回底层TypedStorage.

Tensor.untyped_storage

返回底层UntypedStorage.

Tensor.storage_offset

返回 tensor 在底层存储中的偏移量,以存储元素(而不是字节)的数量表示。self

Tensor.storage_type

返回底层存储的类型。

Tensor.stride

返回 tensor 的步幅。self

Tensor.sub

torch.sub().

Tensor.sub_

的就地版本sub()

Tensor.subtract

torch.subtract().

Tensor.subtract_

的就地版本subtract().

Tensor.sum

torch.sum()

Tensor.sum_to_size

将张量求和 为 。thissize

Tensor.svd

torch.svd()

Tensor.swapaxes

torch.swapaxes()

Tensor.swapdims

torch.swapdims()

Tensor.t

torch.t()

Tensor.t_

的就地版本t()

Tensor.tensor_split

torch.tensor_split()

Tensor.tile

torch.tile()

Tensor.to

执行 Tensor dtype 和/或 device 转换。

Tensor.to_mkldnn

返回 layout 中张量的副本。torch.mkldnn

Tensor.take

torch.take()

Tensor.take_along_dim

torch.take_along_dim()

Tensor.tan

torch.tan()

Tensor.tan_

的就地版本tan()

Tensor.tanh

torch.tanh()

Tensor.tanh_

的就地版本tanh()

Tensor.atanh

torch.atanh()

Tensor.atanh_

的就地版本atanh()

Tensor.arctanh

torch.arctanh()

Tensor.arctanh_

的就地版本arctanh()

Tensor.tolist

以 (嵌套) 列表的形式返回张量。

Tensor.topk

torch.topk()

Tensor.to_dense

创建 if 不是 stribed 张量的 stribed 副本,否则返回 。selfselfself

Tensor.to_sparse

返回张量的稀疏副本。

Tensor.to_sparse_csr

将张量转换为压缩的行存储格式 (CSR)。

Tensor.to_sparse_csc

将张量转换为压缩列存储 (CSC) 格式。

Tensor.to_sparse_bsr

将张量转换为给定块大小的块稀疏行 (BSR) 存储格式。

Tensor.to_sparse_bsc

将张量转换为给定区块大小的区块稀疏列 (BSC) 存储格式。

Tensor.trace

torch.trace()

Tensor.transpose

torch.transpose()

Tensor.transpose_

的就地版本transpose()

Tensor.triangular_solve

torch.triangular_solve()

Tensor.tril

torch.tril()

Tensor.tril_

的就地版本tril()

Tensor.triu

torch.triu()

Tensor.triu_

的就地版本triu()

Tensor.true_divide

torch.true_divide()

Tensor.true_divide_

的就地版本true_divide_()

Tensor.trunc

torch.trunc()

Tensor.trunc_

的就地版本trunc()

Tensor.type

如果未提供 dtype,则返回类型,否则将此对象强制转换为指定类型。

Tensor.type_as

将此张量强制转换为给定张量的类型。

Tensor.unbind

torch.unbind()

Tensor.unflatten

torch.unflatten().

Tensor.unfold

返回原始张量的视图,其中包含维度中 tensor 的所有大小切片。sizeselfdimension

Tensor.uniform_

用从连续均匀分布中采样的数字填充张量:self

Tensor.unique

返回输入张量的唯一元素。

Tensor.unique_consecutive

从每组连续的等效元素中消除除第一个元素之外的所有元素。

Tensor.unsqueeze

torch.unsqueeze()

Tensor.unsqueeze_

的就地版本unsqueeze()

Tensor.values

返回稀疏 COO 张量的 values tensor。

Tensor.var

torch.var()

Tensor.vdot

torch.vdot()

Tensor.view

返回一个新张量,其数据与张量相同,但具有不同的 。selfshape

Tensor.view_as

将此张量视为与 相同的大小。other

Tensor.vsplit

torch.vsplit()

Tensor.where

self.where(condition, y)等效于 。torch.where(condition, self, y)

Tensor.xlogy

torch.xlogy()

Tensor.xlogy_

的就地版本xlogy()

Tensor.zero_

用零填充 tensor。self

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源