快捷方式

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

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 支持并且不需要额外的范围,我们建议使用它们的带符号变体。有关更多详细信息,请参阅 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 位整数(有符号)

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

注意

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

注意

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

警告

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

张量类参考

class torch.Tensor

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

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

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

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

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

Tensor.T

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

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

警告

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

Tensor.H

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

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

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

Tensor.new_full

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

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

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

返回一个 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_

用指定的值填充 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 的元素累加到 self 张量中,按照 index 中给定的顺序添加索引。

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 中给出的顺序,将 source 中的元素累加到 self 张量中,使用 reduce 参数指定的约简操作。

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

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

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

定义如何将 other 转换到 self 中,在 load_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_

用从由 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,返回底层量化器的比例。

Tensor.q_zero_point

对于通过线性(仿射)量化量化的 Tensor,返回底层量化器的零点。

Tensor.q_per_channel_scales

对于通过线性(仿射)逐通道量化量化的 Tensor,返回底层量化器的比例 Tensor。

Tensor.q_per_channel_zero_points

对于通过线性(仿射)逐通道量化量化的 Tensor,返回底层量化器的零点 Tensor。

Tensor.q_per_channel_axis

对于通过线性(仿射)逐通道量化量化的 Tensor,返回应用逐通道量化的维度的索引。

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_

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

Tensor.scatter_add

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

Tensor.scatter_reduce_

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

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

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

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

返回原始张量的视图,其中包含来自 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 张量具有相同的数据,但具有不同的 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.zero_

用零填充 self 张量。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源