评价此页

torch.Tensor#

创建于:2016 年 12 月 23 日 | 最后更新于:2024 年 6 月 11 日

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 位整数 (无符号)

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 个有效位。在精度重要而范围不重要时很有用。

2

有时被称为 Brain Floating Point:使用 1 个符号位、8 个指数位和 7 个有效位。在范围重要时很有用,因为它与 float32 具有相同的指数位数。

3

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

4(1,2,3)

uint8 之外的无符号类型目前计划只在 eager 模式下提供有限支持(它们主要用于辅助 torch.compile 的使用);如果您需要 eager 支持且不需要额外的范围,我们建议使用它们的有符号变体。有关更多详细信息,请参阅 pytorch/pytorch#58734

5(1,2)

torch.float8_e4m3fntorch.float8_e5m2 实现了 https://arxiv.org/abs/2209.05433 中 8 位浮点类型的规范。操作支持非常有限。

为了向后兼容,我们支持以下这些数据类型的替代类名

数据类型

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

然而,为了构造张量,我们建议使用工厂函数,例如带有 dtype 参数的 torch.empty()torch.Tensor 构造函数是默认张量类型 (torch.FloatTensor) 的别名。

初始化和基本操作#

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

>>> 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() 总是复制 data。如果您有一个张量 data 并且只想更改其 requires_grad 标志,请使用 requires_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')

有关构建张量的更多信息,请参阅 创建操作

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

>>> 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() 从包含单个值的张量中获取 Python 数字

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

有关索引的更多信息,请参阅 索引、切片、连接、变异操作

可以创建 requires_grad=True 的张量,以便 torch.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]])

每个张量都有一个相关的 torch.Storage,它存储其数据。张量类还提供存储的多维、跨步视图,并定义其上的数值操作。

注意

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

注意

有关 torch.dtypetorch.devicetorch.layout 属性的更多信息,请参阅 torch.TensorTensor Attributes(张量属性)

注意

修改张量的方法标有下划线后缀。例如,torch.FloatTensor.abs_() 计算绝对值并返回修改后的张量,而 torch.FloatTensor.abs() 在新张量中计算结果。

注意

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

警告

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

张量类参考#

class torch.Tensor#

根据您的用例,有几种主要方法可以创建张量。

  • 要创建具有现有数据的张量,请使用 torch.tensor()

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

  • 要创建与另一个张量大小相同(和类型相似)的张量,请使用 torch.*_like 张量创建操作(请参阅 Creation Ops(创建操作))。

  • 要创建与另一个张量类型相似但大小不同的张量,请使用 tensor.new_* 创建操作。

  • 有一个旧的构造函数 torch.Tensor,不建议使用。请改用 torch.tensor()

Tensor.__init__(self, data)#

此构造函数已弃用,建议改用 torch.tensor()。此构造函数的功能取决于 data 的类型。

  • 如果 data 是一个 Tensor,则返回原始 Tensor 的别名。与 torch.tensor() 不同,这将跟踪自动求导并向原始 Tensor 传播梯度。device kwarg 不支持此 data 类型。

  • 如果 data 是一个序列或嵌套序列,则创建一个默认 dtype(通常是 torch.float32)的张量,其数据为序列中的值,必要时执行强制转换。值得注意的是,这与 torch.tensor() 不同,此构造函数将始终构造一个浮点张量,即使输入全部是整数。

  • 如果 datatorch.Size,则返回该大小的空张量。

此构造函数不支持显式指定返回张量的 dtypedevice。我们建议使用提供此功能的 torch.tensor()

参数 (Args)

data (array_like): 要构造的张量。

关键字参数
device (torch.device, 可选): 返回张量的所需设备。

默认值: 如果为 None,则与此张量具有相同的 torch.device

Tensor.T#

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

如果 nx 中的维度数,则 x.T 等效于 x.permute(n-1, n-2, ..., 0)

警告

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

Tensor.H#

返回矩阵(2D 张量)的共轭转置视图。

对于复数矩阵,x.H 等效于 x.transpose(0, 1).conj();对于实数矩阵,则等效于 x.transpose(0, 1)

另请参阅

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

Tensor.mT#

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

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

Tensor.mH#

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

Tensor.new_tensor

返回一个新 Tensor,其中 data 作为张量数据。

Tensor.new_full

返回一个大小为 size,并用 fill_value 填充的 Tensor。

Tensor.new_empty

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

Tensor.new_ones

返回一个大小为 size,并用 1 填充的 Tensor。

Tensor.new_zeros

返回一个大小为 size,并用 0 填充的 Tensor。

Tensor.is_cuda

如果张量存储在 GPU 上,则为 True,否则为 False

Tensor.is_quantized

如果张量已量化,则为 True,否则为 False

Tensor.is_meta

如果张量是元张量,则为 True,否则为 False

Tensor.device

这是此张量所在的 torch.device

Tensor.grad

此属性默认为 None,并在第一次调用 backward() 计算 self 的梯度时变为 Tensor。

Tensor.ndim

dim() 的别名

Tensor.real

对于复值输入张量,返回一个包含 self 张量实值的新张量。

Tensor.imag

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

Tensor.nbytes

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

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

将标量或张量添加到 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_

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

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_(other) -> Tensor

Tensor.all

参阅 torch.all()

Tensor.any

参阅 torch.any()

Tensor.backward

计算当前张量相对于图叶的梯度。

Tensor.baddbmm

参阅 torch.baddbmm()

Tensor.baddbmm_

baddbmm() 的原地版本

Tensor.bernoulli

返回一个结果张量,其中每个 result[i]\texttt{result[i]} 独立地从 Bernoulli(self[i])\text{Bernoulli}(\texttt{self[i]}) 中采样。

Tensor.bernoulli_

用从 Bernoulli(p)\text{Bernoulli}(\texttt{p}) 中独立采样的样本填充 self 的每个位置。

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_

用从柯西分布中提取的数字填充张量

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

返回一个内存中连续的张量,包含与 self 张量相同的数据。

Tensor.copy_

src 中的元素复制到 self 张量中,并返回 self

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

Tensor.arccosh_

acosh_() -> Tensor

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

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

Tensor.deg2rad

参阅 torch.deg2rad()

Tensor.dequantize

给定一个量化张量,对其进行去量化并返回去量化的浮点张量。

Tensor.det

参阅 torch.det()

Tensor.dense_dim

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

Tensor.detach

返回一个从当前图分离的新张量。

Tensor.detach_

将张量从创建它的图中分离,使其成为叶子。

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

返回 self 张量的维度数。

Tensor.dim_order

返回描述 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_

用指定值填充 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_

通过将 alpha 乘以 source 的元素,并按照 index 中给定的顺序添加到索引处,将它们累加到 self 张量中。

Tensor.index_add

torch.Tensor.index_add_() 的非原地版本。

Tensor.index_copy_

通过按照 index 中给定的顺序选择索引,将 tensor 的元素复制到 self 张量中。

Tensor.index_copy

torch.Tensor.index_copy_() 的非原地版本。

Tensor.index_fill_

通过按照 index 中给定的顺序选择索引,用值 value 填充 self 张量的元素。

Tensor.index_fill

torch.Tensor.index_fill_() 的非原地版本。

Tensor.index_put_

使用 indices(一个张量元组)中指定的索引,将张量 values 中的值放入张量 self 中。

Tensor.index_put

index_put_() 的非原地版本。

Tensor.index_reduce_

通过按照 index 中给定的顺序,使用 reduce 参数给出的规约,将 source 的元素累加到 self 张量中。

Tensor.index_reduce

Tensor.index_select

参阅 torch.index_select()

Tensor.indices

返回 稀疏 COO 张量 的索引张量。

Tensor.inner

参阅 torch.inner()

Tensor.int

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

Tensor.int_repr

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

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

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

Tensor.is_complex

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

Tensor.is_conj

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

Tensor.is_floating_point

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

Tensor.is_inference

参阅 torch.is_inference()

Tensor.is_leaf

所有 requires_gradFalse 的 Tensor 按照惯例都将是叶子 Tensor。

Tensor.is_pinned

如果此张量位于固定内存中,则返回 true。

Tensor.is_set_to

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

Tensor.is_shared

检查张量是否在共享内存中。

Tensor.is_signed

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

Tensor.is_sparse

如果张量使用稀疏 COO 存储布局,则为 True,否则为 False

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_

使用由给定均值 μ\mu 和标准差 σ\sigma 参数化的对数正态分布采样的数字填充 self 张量。

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

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

Tensor.map_

callable 应用于 self 张量和给定 tensor 中的每个元素,并将结果存储在 self 张量中。

Tensor.masked_scatter_

source 中的元素复制到 self 张量中,位置由 mask 为 True 的地方指定。

Tensor.masked_scatter

torch.Tensor.masked_scatter_() 的非原地版本

Tensor.masked_fill_

mask 为 True 的地方,用 value 填充 self 张量的元素。

Tensor.masked_fill

torch.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

定义在 load_state_dict() 中将 other 加载到 self 时如何转换 other

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_

使用由 meanstd 参数化的正态分布采样的元素填充 self 张量。

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_

source 中的元素复制到 index 指定的位置。

Tensor.qr

参见 torch.qr()

Tensor.qscheme

返回给定 QTensor 的量化方案。

Tensor.quantile

参见 torch.quantile()

Tensor.nanquantile

参见 torch.nanquantile()

Tensor.q_scale

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

Tensor.q_zero_point

给定一个通过线性(仿射)量化量化的张量,返回底层量化器() 的零点。

Tensor.q_per_channel_scales

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

Tensor.q_per_channel_zero_points

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

Tensor.q_per_channel_axis

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

Tensor.rad2deg

参见 torch.rad2deg()

Tensor.random_

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

Tensor.ravel

参见 torch.ravel()

Tensor.reciprocal

参见 torch.reciprocal()

Tensor.reciprocal_

reciprocal() 的原地版本

Tensor.record_stream

标记张量已被此流使用。

Tensor.register_hook

注册一个反向钩子。

Tensor.register_post_accumulate_grad_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

如果需要为此张量计算梯度,则为 True,否则为 False

Tensor.requires_grad_

更改自动梯度是否应记录此张量上的操作:就地设置此张量的 requires_grad 属性。

Tensor.reshape

返回一个与 self 具有相同数据和元素数量但具有指定形状的新张量。

Tensor.reshape_as

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

Tensor.resize_

self 张量的大小调整为指定大小。

Tensor.resize_as_

self 张量的大小调整为与指定的 tensor 相同的大小。

Tensor.retain_grad

使此张量在 backward() 期间填充其 grad

Tensor.retains_grad

如果此张量是非叶张量并且其 gradbackward() 期间被允许填充,则为 True,否则为 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

torch.Tensor.scatter_() 的非原地版本

Tensor.scatter_

将张量 src 中的所有值写入 self 中,索引由 index 张量指定。

Tensor.scatter_add_

以类似于 scatter_() 的方式,将张量 src 中的所有值添加到 self 中,索引由 index 张量指定。

Tensor.scatter_add

torch.Tensor.scatter_add_() 的非原地版本

Tensor.scatter_reduce_

使用 reduce 参数("sum""prod""mean""amax""amin")定义的归约方法,将 src 张量中的所有值归约到 self 张量中 index 张量指定的索引处。

Tensor.scatter_reduce

torch.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

返回一个新的 稀疏张量,其值来自一个步幅张量 self,并由稀疏张量 mask 的索引过滤。

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

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

Tensor.storage_type

返回底层存储的类型。

Tensor.stride

返回 self 张量的步幅。

Tensor.sub

参见 torch.sub()

Tensor.sub_

sub() 的原地版本

Tensor.subtract

参见 torch.subtract()

Tensor.subtract_

subtract() 的原地版本。

Tensor.sum

参见 torch.sum()

Tensor.sum_to_size

this 张量求和到 size

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.to_mkldnn

返回张量在 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

如果 self 不是步幅张量,则创建 self 的步幅副本,否则返回 self

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

返回原始张量的一个视图,该视图包含维度 dimension 中大小为 sizeself 张量的所有切片。

Tensor.uniform_

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

Tensor.unique

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

Tensor.unique_consecutive

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

Tensor.unsqueeze

参见 torch.unsqueeze()

Tensor.unsqueeze_

unsqueeze() 的原地版本

Tensor.values

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

Tensor.var

参见 torch.var()

Tensor.vdot

参见 torch.vdot()

Tensor.view

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

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.xpu

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

Tensor.zero_

用零填充 self 张量。