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位 signed 整数

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

有时也称为大脑浮点数:它使用1位符号位、8位指数位和7位有效数字位。当范围很重要时,这种格式非常有用,因为它与float32具有相同的指数位数量。

3

量化后的4位整数以8位有符号整数的形式存储。目前仅支持于EmbeddingBag操作符中。

4(1,2,3)

除了uint8之外的无符号类型目前仅在即时模式下提供有限支持(主要用于与torch.compile一起使用);如果你需要即时模式支持且不需要额外的取值范围,建议使用相应的有符号类型。更多详情请参见https://github.com/pytorch/pytorch/issues/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位 signed 整数

torch.IntTensor

torch.cuda.IntTensor

64位带符号的整数

torch.LongTensor

torch.cuda.LongTensor

布尔类型

torch.BoolTensor

torch.cuda.BoolTensor

为了构建张量,我们建议使用工厂函数(如torch.empty()),并传入dtype参数。此外,torch.Tensor构造函数是默认张量类型(torch.FloatTensor)的别名。

初始化与基本操作

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

>>> 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。如果你有一个 Tensor 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 属性的更多信息,请参见张量属性

注意

修改张 tensor 的方法都会在名称后面加上下划线后缀。例如,torch.FloatTensor.abs_() 会就地计算绝对值并返回修改后的 tensor,而 torch.FloatTensor.abs() 则会在新的 tensor 中返回结果。

注意

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

警告

当前 torch.Tensor 的实现引入了内存开销,在使用许多小张量的应用中可能会导致意外的高内存 usage。如果你遇到这种情况,可以考虑使用一个大的结构体。

注:这里的"usage"可能是原文中的拼写错误或专有名词,未做修改。

张量类参考

torch.Tensor

根据不同的使用场景,有几种主要的方式可以创建张量。

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

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

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

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

  • 有一个已不再推荐使用的构造函数 torch.Tensor。建议使用 torch.tensor() 替代。

Tensor.__init__(self, data)

此构造函数已被弃用,建议使用torch.tensor()代替。其行为取决于data的类型。

  • 如果 data 是一个张量,返回该张量的别名。与torch.tensor()不同,这个函数会跟踪自动微分,并将梯度传播回原始张量。device 参数不适用于此 data 类型。

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

  • 如果 data 是一个 torch.Size,则返回相应大小的空张量。

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

参数:

data (array_like): 构造张量所使用的数据。

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

默认:如果为 None,则使用与该张量相同的 torch.device

张量T

返回该张量维度反转后的视图。

如果nx的维度数量,那么x.T等同于x.permute(n-1, n-2, ..., 0)

警告

在二维张量之外的其他维度上使用Tensor.T() 来反转形状是不推荐的,并且在未来版本中会抛出错误。建议使用 mT 来转置矩阵批次,或者使用 x.permute(*torch.arange(x.ndim - 1, -1, -1)) 来反转张量的维度。

张量 H

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

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

参见

mH: 一个也可以应用于矩阵批次的属性。

张量mT

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

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

张量。mH

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

Tensor.new_tensor

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

Tensor.new_full

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

Tensor.new_empty

返回一个大小为 size 的张量,其中的数据尚未初始化。

Tensor.new_ones

返回一个大小为 size 且所有元素均为 1 的张量。

Tensor.new_zeros

返回一个大小为 size 且所有元素均为 0 的张量。

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

“dim()”的别名

Tensor.real

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

Tensor.imag

返回一个新张量,其中包含 self 张量的虚部值。

Tensor.nbytes

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

Tensor.itemsize

别名 for element_size()

Tensor.abs

参见 torch.abs()

Tensor.abs_

abs() 的原地版本

Tensor.absolute

别名 для abs()

根据上下文和一致性要求,最终答案应为:

别名 for abs()

Tensor.absolute_

这是 absolute() 函数的原地版本,相当于 abs_() 的别名。

Tensor.acos

参见 torch.acos()

Tensor.acos_

acos() 的 inplace 版本

Tensor.arccos

参见 torch.arccos()

Tensor.arccos_

arccos() 的 inplace 版本

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,并将每个元素替换成该函数返回的值。

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() 的 inplace 版本

Tensor.as_strided

参见 torch.as_strided()

Tensor.atan

参见 torch.atan()

Tensor.atan_

atan() 的就地版本

Tensor.arctan

参见 torch.arctan()

Tensor.arctan_

arctan() 的 inplace 版本

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

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

Tensor.bernoulli_

self 的每个位置用来自 $\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() 的 inplace 版本

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() 的 inplace 版本

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_

的原地版本

Tensor.corrcoef

参见 torch.corrcoef()

Tensor.count_nonzero

参见 torch.count_nonzero()

Tensor.cov

参见 torch.cov()

Tensor.acosh

参见 torch.acosh()

Tensor.acosh_

acosh() 的 inplace 版本

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_

的就地版本

Tensor.cumsum

参见 torch.cumsum()

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

填写一个至少二维的张量的主要对角线。

Tensor.fmax

参见 torch.fmax()

Tensor.fmin

参见 torch.fmin()

Tensor.diff

参见torch.diff()

Tensor.digamma

参见 torch.digamma()

Tensor.digamma_

digamma() 的 inplace 版本

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_() 的独立版本。

或者更自然的表达方式:

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_() 的 out-of-place 版本。

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() 会返回一个具有 uint8_t 数据类型的 CPU 张量,这个张量包含了该量化张量内部的所有 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.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

Tensor.less_

就地版本的 less()

Tensor.lu

参见 torch.lu()

Tensor.lu_solve

参见 torch.lu_solve()

Tensor.as_subclass

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

Tensor.map_

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

Tensor.masked_scatter_

source中的元素复制到self张量中,在mask为True的位置。

Tensor.masked_scatter

torch.Tensor.masked_scatter_() 的独立版本

Tensor.masked_fill_

self张量中mask为True的元素填充为value

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() 的 inplace 版本

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

如果此张量是非叶子张量,并且其grad在调用backward()时被启用以进行填充,则返回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_()类似的方式,在index张量中指定的索引处,将src张量中的所有值添加到self

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_() 的 out-of-place 版本

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() 的 inplace 版本

Tensor.arcsinh

参见 torch.arcsinh()

Tensor.arcsinh_

arcsinh() 的 inplace 版本

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() 的 inplace 版本

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_() 的 inplace 版本

Tensor.trunc

参见 torch.trunc()

Tensor.trunc_

trunc() 的原地版本

Tensor.type

如果没有提供dtype,则返回类型;否则,将此对象转换为指定的类型。

Tensor.type_as

将此张量转换为指定类型并返回。

Tensor.unbind

参见 torch.unbind()

Tensor.unflatten

参见 torch.unflatten()

Tensor.unfold

返回原始张量的一个视图,包含从 self 张量的 dimension 维度中提取的所有大小为 size 的切片。

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 张量相同,但形状不同。

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张量用零填充。

本页目录