捷徑

torch.Tensor

torch.Tensor 是一個多維矩陣,包含單一資料類型的元素。

資料類型

Torch 使用下列資料類型定義 tensor 類型

資料類型

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.Tensor 的屬性的更多資訊,請參閱 張量屬性

注意

會改變張量的方法會標記一個底線後綴。例如,torch.FloatTensor.abs_() 會原地計算絕對值並返回修改後的張量,而 torch.FloatTensor.abs() 會在一個新的張量中計算結果。

注意

要更改現有張量的 torch.device 和/或 torch.dtype,請考慮使用張量上的 to() 方法。

警告

目前 torch.Tensor 的實現引入了記憶體開銷,因此在具有許多微小張量的應用程式中,可能會導致出乎意料的高記憶體使用量。如果這是您的情況,請考慮使用一個大型結構。

張量類別參考

class torch.Tensor

根據您的用例,有幾種建立張量的主要方法。

  • 要使用預先存在的資料建立張量,請使用 torch.tensor()

  • 要建立具有特定大小的張量,請使用 torch.* 張量建立操作(請參閱 建立操作)。

  • 要建立一個與另一個張量具有相同大小(和類似類型)的張量,請使用 torch.*_like 張量建立操作(請參閱 建立操作)。

  • 要建立一個與另一個張量具有相似類型但不同大小的張量,請使用 tensor.new_* 建立操作。

  • 有一個已過時的建構子 torch.Tensor,不建議使用。請改用 torch.tensor()

Tensor.__init__(self, data)

此建構子已棄用,我們建議改用 torch.tensor()。這個建構子的作用取決於 data 的類型。

  • 如果 data 是一個 Tensor,則返回原始 Tensor 的別名。與 torch.tensor() 不同,這會追蹤 autograd 並將梯度傳播到原始 Tensor。此 data 類型不支援 device kwarg。

  • 如果 data 是一個序列或嵌套序列,則建立一個預設 dtype 的張量(通常是 torch.float32),其資料是序列中的值,並在必要時執行強制轉換。值得注意的是,這與 torch.tensor() 不同,因為即使輸入都是整數,此建構子也始終會建構一個浮點張量。

  • 如果 data 是一個 torch.Size,則返回該大小的空張量。

此建構子不支援顯式指定返回張量的 dtypedevice。我們建議使用 torch.tensor(),它提供了此功能。

參數

data (array_like): 從中建構的張量。

關鍵字參數
device (torch.device, optional): 返回張量所需的裝置。

預設值:如果為 None,則與此張量相同的 torch.device

Tensor.T

返回此張量的一個視圖,其維度已反轉。

如果 nx 的維度數量,則 x.T 等同於 x.permute(n-1, n-2, ..., 0)

警告

在維度非 2 的 tensors 上使用 Tensor.T() 來反轉其形狀已被棄用,並且在未來的版本中會拋出錯誤。請考慮使用 mT 來轉置矩陣批次,或者使用 x.permute(*torch.arange(x.ndim - 1, -1, -1)) 來反轉 tensor 的維度。

Tensor.H

傳回一個矩陣(二維 tensor)的共軛轉置檢視。

對於複數矩陣,x.H 等同於 x.transpose(0, 1).conj();對於實數矩陣,則等同於 x.transpose(0, 1)

另請參閱

mH:一個也適用於矩陣批次的屬性。

Tensor.mT

傳回此 tensor 的檢視,其中最後兩個維度已轉置。

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

Tensor.mH

存取此屬性等同於呼叫 adjoint()

Tensor.new_tensor

傳回一個新的 Tensor,其 data 作為 tensor 資料。

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

如果 Tensor 儲存在 GPU 上,則為 True,否則為 False

Tensor.is_quantized

如果 Tensor 已量化,則為 True,否則為 False

Tensor.is_meta

如果 Tensor 是一個 meta tensor,則為 True,否則為 False

Tensor.device

這是 torch.device,表示此 Tensor 所在的裝置。

Tensor.grad

預設情況下,此屬性為 None,並且在第一次呼叫 backward() 計算 self 的梯度時,會變成一個 Tensor。

Tensor.ndim

dim() 的別名

Tensor.real

傳回一個新的 tensor,其中包含複數值輸入 tensor self 的實數值。

Tensor.imag

傳回一個新的 tensor,其中包含 self tensor 的虛數值。

Tensor.nbytes

如果 Tensor 不使用稀疏儲存佈局,則傳回 Tensor 元素 "view" 所消耗的位元組數。

Tensor.itemsize

element_size() 的別名

Tensor.abs

請參閱 torch.abs()

Tensor.abs_

abs() 的原地 (in-place) 版本

Tensor.absolute

請參閱 abs()

Tensor.absolute_

absolute() 的原地 (in-place) 版本。 abs_() 的別名

Tensor.acos

請參閱 torch.acos()

Tensor.acos_

acos() 的原地 (in-place) 版本

Tensor.arccos

請參閱 torch.arccos()

Tensor.arccos_

arccos() 的原地 (in-place) 版本

Tensor.add

將純量或 tensor 加到 self tensor。

Tensor.add_

add() 的原地 (in-place) 版本

Tensor.addbmm

請參閱 torch.addbmm()

Tensor.addbmm_

addbmm() 的原地 (in-place) 版本

Tensor.addcdiv

請參閱 torch.addcdiv()

Tensor.addcdiv_

addcdiv() 的原地 (In-place) 版本

Tensor.addcmul

請參閱 torch.addcmul()

Tensor.addcmul_

addcmul() 的原地 (In-place) 版本

Tensor.addmm

請參閱 torch.addmm()

Tensor.addmm_

addmm() 的原地 (In-place) 版本

Tensor.sspaddmm

請參閱 torch.sspaddmm()

Tensor.addmv

請參閱 torch.addmv()

Tensor.addmv_

addmv() 的原地 (In-place) 版本

Tensor.addr

請參閱 torch.addr()

Tensor.addr_

addr() 的原地 (In-place) 版本

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 中的每個元素,並將每個元素替換為 callable 返回的值。

Tensor.argmax

請參閱 torch.argmax()

Tensor.argmin

請參閱 torch.argmin()

Tensor.argsort

請參閱 torch.argsort()

Tensor.argwhere

請參閱 torch.argwhere()

Tensor.asin

請參閱 torch.asin()

Tensor.asin_

asin() 的原地 (In-place) 版本

Tensor.arcsin

請參閱 torch.arcsin()

Tensor.arcsin_

arcsin() 的原地 (In-place) 版本

Tensor.as_strided

請參閱 torch.as_strided()

Tensor.atan

請參閱 torch.atan()

Tensor.atan_

atan() 的原地 (In-place) 版本

Tensor.arctan

請參閱 torch.arctan()

Tensor.arctan_

arctan() 的原地 (In-place) 版本

Tensor.atan2

請參閱 torch.atan2()

Tensor.atan2_

atan2() 的原地 (In-place) 版本

Tensor.arctan2

請參閱 torch.arctan2()

Tensor.arctan2_

atan2_(other) -> Tensor

Tensor.all

請參閱 torch.all()

Tensor.any

請參閱 torch.any()

Tensor.backward

計算當前 tensor 相對於圖的葉節點的梯度。

Tensor.baddbmm

請參閱 torch.baddbmm()

Tensor.baddbmm_

baddbmm() 的原地 (In-place) 版本

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() 的原地 (in-place) 版本

Tensor.bitwise_and

請參閱 torch.bitwise_and()

Tensor.bitwise_and_

bitwise_and() 的原地 (in-place) 版本

Tensor.bitwise_or

請參閱 torch.bitwise_or()

Tensor.bitwise_or_

bitwise_or() 的原地 (in-place) 版本

Tensor.bitwise_xor

請參閱 torch.bitwise_xor()

Tensor.bitwise_xor_

bitwise_xor() 的原地 (in-place) 版本

Tensor.bitwise_left_shift

請參閱 torch.bitwise_left_shift()

Tensor.bitwise_left_shift_

bitwise_left_shift() 的原地 (in-place) 版本

Tensor.bitwise_right_shift

請參閱 torch.bitwise_right_shift()

Tensor.bitwise_right_shift_

bitwise_right_shift() 的原地 (in-place) 版本

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 distribution) 抽取的數字填充張量

Tensor.ceil

請參閱 torch.ceil()

Tensor.ceil_

ceil() 的原地 (in-place) 版本

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() 的原地 (in-place) 版本

Tensor.clip

clamp() 的別名。

Tensor.clip_

clamp_() 的別名。

Tensor.clone

請參閱 torch.clone()

Tensor.contiguous

返回一個在記憶體中連續的 Tensor,其中包含與 self Tensor 相同的資料。

Tensor.copy_

將元素從 src 複製到 self Tensor,並返回 self

Tensor.conj

請參閱 torch.conj()

Tensor.conj_physical

請參閱 torch.conj_physical()

Tensor.conj_physical_

conj_physical() 的原地 (in-place) 版本

Tensor.resolve_conj

請參閱 torch.resolve_conj()

Tensor.resolve_neg

請參閱 torch.resolve_neg()

Tensor.copysign

請參閱 torch.copysign()

Tensor.copysign_

copysign() 的原地 (in-place) 版本

Tensor.cos

請參閱 torch.cos()

Tensor.cos_

cos() 的原地 (in-place) 版本

Tensor.cosh

請參閱 torch.cosh()

Tensor.cosh_

cosh() 的原地 (in-place) 版本

Tensor.corrcoef

請參閱 torch.corrcoef()

Tensor.count_nonzero

請參閱 torch.count_nonzero()

Tensor.cov

請參閱 torch.cov()

Tensor.acosh

請參閱 torch.acosh()

Tensor.acosh_

acosh() 的原地 (in-place) 版本

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() 的原地 (in-place) 版本

Tensor.cumsum

請參閱 torch.cumsum()

Tensor.cumsum_

cumsum() 的原地 (in-place) 版本

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 的第一個元素的位址。

Tensor.deg2rad

請參閱 torch.deg2rad()

Tensor.dequantize

給定一個量化的 Tensor,將其反量化並返回反量化的 float Tensor。

Tensor.det

請參閱 torch.det()

Tensor.dense_dim

返回 稀疏 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() 的原地 (in-place) 版本

Tensor.dim

傳回 self 張量的維度數量。

Tensor.dim_order

傳回唯一確定的整數元組,描述 self 的維度順序或物理佈局。

Tensor.dist

請參閱 torch.dist()

Tensor.div

請參閱 torch.div()

Tensor.div_

div() 的原地 (in-place) 版本

Tensor.divide

請參閱 torch.divide()

Tensor.divide_

divide() 的原地 (in-place) 版本

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() 的原地 (in-place) 版本

Tensor.equal

請參閱 torch.equal()

Tensor.erf

請參閱 torch.erf()

Tensor.erf_

erf() 的原地 (in-place) 版本

Tensor.erfc

請參閱 torch.erfc()

Tensor.erfc_

erfc() 的原地 (in-place) 版本

Tensor.erfinv

請參閱 torch.erfinv()

Tensor.erfinv_

erfinv() 的原地 (in-place) 版本

Tensor.exp

請參閱 torch.exp()

Tensor.exp_

exp() 的原地 (in-place) 版本

Tensor.expm1

請參閱 torch.expm1()

Tensor.expm1_

expm1() 的原地 (in-place) 版本

Tensor.expand

傳回 self 張量的新視圖,其中單例維度擴展到更大的尺寸。

Tensor.expand_as

將此張量擴展到與 other 相同的大小。

Tensor.exponential_

使用從 PDF(機率密度函數)中提取的元素填充 self 張量

Tensor.fix

請參閱 torch.fix()

Tensor.fix_

fix() 的原地 (in-place) 版本

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() 的原地 (In-place) 版本

Tensor.floor

請參閱 torch.floor()

Tensor.floor_

floor() 的原地 (In-place) 版本

Tensor.floor_divide

請參閱 torch.floor_divide()

Tensor.floor_divide_

floor_divide() 的原地 (In-place) 版本

Tensor.fmod

請參閱 torch.fmod()

Tensor.fmod_

fmod() 的原地 (In-place) 版本

Tensor.frac

請參閱 torch.frac()

Tensor.frac_

frac() 的原地 (In-place) 版本

Tensor.frexp

請參閱 torch.frexp()

Tensor.gather

請參閱 torch.gather()

Tensor.gcd

請參閱 torch.gcd()

Tensor.gcd_

gcd() 的原地 (In-place) 版本

Tensor.ge

請參閱 torch.ge()

Tensor.ge_

ge() 的原地 (In-place) 版本。

Tensor.greater_equal

請參閱 torch.greater_equal()

Tensor.greater_equal_

greater_equal() 的原地 (In-place) 版本。

Tensor.geometric_

使用從幾何分布中抽取的元素填充 self 張量

Tensor.geqrf

請參閱 torch.geqrf()

Tensor.ger

請參閱 torch.ger()

Tensor.get_device

對於 CUDA 張量,此函數會傳回張量所在的 GPU 的設備序數。

Tensor.gt

請參閱 torch.gt()

Tensor.gt_

gt() 的原地 (In-place) 版本。

Tensor.greater

請參閱 torch.greater()

Tensor.greater_

greater() 的原地 (In-place) 版本。

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() 的原地 (In-place) 版本

Tensor.i0

請參閱 torch.i0()

Tensor.i0_

i0() 的原地 (In-place) 版本

Tensor.igamma

請參閱 torch.igamma()

Tensor.igamma_

igamma() 的原地 (In-place) 版本

Tensor.igammac

請參閱 torch.igammac()

Tensor.igammac_

igammac() 的原地 (in-place) 版本

Tensor.index_add_

alpha 乘以 source 的元素,按照 index 中給定的順序,累加到 self tensor 中。

Tensor.index_add

torch.Tensor.index_add_() 的異地 (out-of-place) 版本。

Tensor.index_copy_

tensor 的元素,按照 index 中給定的順序選擇索引,複製到 self tensor 中。

Tensor.index_copy

torch.Tensor.index_copy_() 的異地 (out-of-place) 版本。

Tensor.index_fill_

self tensor 的元素,按照 index 中給定的順序選擇索引,用值 value 填充。

Tensor.index_fill

torch.Tensor.index_fill_() 的異地 (out-of-place) 版本。

Tensor.index_put_

使用 indices (一個 Tensors 的元組) 中指定的索引,將 tensor values 中的值放入 tensor self 中。

Tensor.index_put

index_put_() 的異地 (out-place) 版本。

Tensor.index_reduce_

source 的元素,按照 index 中給定的順序累加到 self tensor 中,使用 reduce 參數給定的歸約方式。

Tensor.index_reduce

Tensor.index_select

請參閱 torch.index_select()

Tensor.indices

返回 稀疏 COO tensor 的索引 tensor。

Tensor.inner

請參閱 torch.inner()

Tensor.int

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

Tensor.int_repr

給定一個量化的 Tensor,self.int_repr() 返回一個以 uint8_t 作為資料類型的 CPU Tensor,它儲存給定 Tensor 的底層 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 tensor 以記憶體格式指定的順序在記憶體中是連續的,則返回 True。

Tensor.is_complex

如果 self 的資料類型是複數資料類型,則返回 True。

Tensor.is_conj

如果 self 的共軛位元 (conjugate bit) 設為 true,則返回 True。

Tensor.is_floating_point

如果 self 的資料類型是浮點資料類型,則返回 True。

Tensor.is_inference

請參閱 torch.is_inference()

Tensor.is_leaf

依照慣例,所有 requires_gradFalse 的 Tensors 都是 leaf Tensors。

Tensor.is_pinned

如果此 tensor 位於鎖頁記憶體 (pinned memory) 中,則返回 true。

Tensor.is_set_to

如果兩個 tensor 都指向完全相同的記憶體(相同的儲存體、偏移量、大小和步幅),則返回 True。

Tensor.is_shared

檢查 tensor 是否位於共享記憶體中。

Tensor.is_signed

如果 self 的資料類型是有符號資料類型,則返回 True。

Tensor.is_sparse

如果 Tensor 使用稀疏 COO 儲存佈局,則為 True,否則為 False

Tensor.istft

請參閱 torch.istft()

Tensor.isreal

請參閱 torch.isreal()

Tensor.item

將此 tensor 的值作為標準 Python 數字返回。

Tensor.kthvalue

請參閱 torch.kthvalue()

Tensor.lcm

請參閱 torch.lcm()

Tensor.lcm_

lcm() 的原地 (in-place) 版本

Tensor.ldexp

請參閱 torch.ldexp()

Tensor.ldexp_

原地 (In-place) 版本的 ldexp()

Tensor.le

請參閱 torch.le()

Tensor.le_

原地 (In-place) 版本的 le()

Tensor.less_equal

請參閱 torch.less_equal()

Tensor.less_equal_

原地 (In-place) 版本的 less_equal()

Tensor.lerp

請參閱 torch.lerp()

Tensor.lerp_

原地 (In-place) 版本的 lerp()

Tensor.lgamma

請參閱 torch.lgamma()

Tensor.lgamma_

原地 (In-place) 版本的 lgamma()

Tensor.log

請參閱 torch.log()

Tensor.log_

原地 (In-place) 版本的 log()

Tensor.logdet

請參閱 torch.logdet()

Tensor.log10

請參閱 torch.log10()

Tensor.log10_

原地 (In-place) 版本的 log10()

Tensor.log1p

請參閱 torch.log1p()

Tensor.log1p_

原地 (In-place) 版本的 log1p()

Tensor.log2

請參閱 torch.log2()

Tensor.log2_

原地 (In-place) 版本的 log2()

Tensor.log_normal_

使用給定的平均數 μ\mu 和標準差 σ\sigmaself tensor 填入來自對數常態分佈的數字樣本。

Tensor.logaddexp

請參閱 torch.logaddexp()

Tensor.logaddexp2

請參閱 torch.logaddexp2()

Tensor.logsumexp

請參閱 torch.logsumexp()

Tensor.logical_and

請參閱 torch.logical_and()

Tensor.logical_and_

原地 (In-place) 版本的 logical_and()

Tensor.logical_not

請參閱 torch.logical_not()

Tensor.logical_not_

原地 (In-place) 版本的 logical_not()

Tensor.logical_or

請參閱 torch.logical_or()

Tensor.logical_or_

原地 (In-place) 版本的 logical_or()

Tensor.logical_xor

請參閱 torch.logical_xor()

Tensor.logical_xor_

原地 (In-place) 版本的 logical_xor()

Tensor.logit

請參閱 torch.logit()

Tensor.logit_

原地 (In-place) 版本的 logit()

Tensor.long

self.long() 等同於 self.to(torch.int64)

Tensor.lt

請參閱 torch.lt()

Tensor.lt_

原地 (In-place) 版本的 lt()

Tensor.less

lt(other) -> Tensor

Tensor.less_

原地 (In-place) 版本的 less()

Tensor.lu

請參閱 torch.lu()

Tensor.lu_solve

請參閱 torch.lu_solve()

Tensor.as_subclass

建立一個與 self 具有相同資料指標的 cls 實例。

Tensor.map_

self tensor 中的每個元素以及給定的 tensor 應用 callable,並將結果儲存在 self tensor 中。

Tensor.masked_scatter_

source 中的元素複製到 self tensor 中,位置為 mask 為 True 的地方。

Tensor.masked_scatter

torch.Tensor.masked_scatter_() 的非原地 (out-of-place) 版本

Tensor.masked_fill_

value 填充 self tensor 的元素,其中 mask 為 True。

Tensor.masked_fill

torch.Tensor.masked_fill_() 的非原地 (out-of-place) 版本

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() 的原地 (in-place) 版本。

Tensor.multiply

請參閱 torch.multiply()

Tensor.multiply_

multiply() 的原地 (in-place) 版本。

Tensor.multinomial

請參閱 torch.multinomial()

Tensor.mv

請參閱 torch.mv()

Tensor.mvlgamma

請參閱 torch.mvlgamma()

Tensor.mvlgamma_

mvlgamma() 的原地 (in-place) 版本

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() 的原地 (in-place) 版本。

Tensor.ne

請參閱 torch.ne()

Tensor.ne_

ne() 的原地 (in-place) 版本。

Tensor.not_equal

請參閱 torch.not_equal()

Tensor.not_equal_

not_equal() 的原地 (in-place) 版本。

Tensor.neg

請參閱 torch.neg()

Tensor.neg_

neg() 的原地 (in-place) 版本。

Tensor.negative

請參閱 torch.negative()

Tensor.negative_

negative() 的原地 (in-place) 版本。

Tensor.nelement

numel() 的別名。

Tensor.nextafter

請參閱 torch.nextafter()

Tensor.nextafter_

nextafter() 的原地 (in-place) 版本。

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() 的原地 (in-place) 版本。

Tensor.positive

請參閱 torch.positive()

Tensor.pow

請參閱 torch.pow()

Tensor.pow_

pow() 的原地 (in-place) 版本。

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,回傳底層量化器的 scale。

Tensor.q_zero_point

給定一個經由線性(仿射)量化所量化的 Tensor,回傳底層量化器的 zero_point。

Tensor.q_per_channel_scales

給定一個經由線性(仿射)逐通道量化所量化的 Tensor,回傳一個 Tensor,其包含底層量化器的 scales。

Tensor.q_per_channel_zero_points

給定一個經由線性(仿射)逐通道量化所量化的 Tensor,回傳一個 Tensor,其包含底層量化器的 zero_points。

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() 的原地 (in-place) 版本

Tensor.record_stream

將張量標記為已被此串流使用。

Tensor.register_hook

註冊一個向後鉤子 (backward hook)。

Tensor.register_post_accumulate_grad_hook

註冊一個在梯度累積後運行的向後鉤子。

Tensor.remainder

請參閱 torch.remainder()

Tensor.remainder_

remainder() 的原地 (in-place) 版本

Tensor.renorm

請參閱 torch.renorm()

Tensor.renorm_

renorm() 的原地 (in-place) 版本

Tensor.repeat

沿著指定的維度重複此張量。

Tensor.repeat_interleave

請參閱 torch.repeat_interleave()

Tensor.requires_grad

如果需要為此張量計算梯度,則為 True,否則為 False

Tensor.requires_grad_

更改 autograd 是否應記錄此張量的操作:原地設定此張量的 requires_grad 屬性。

Tensor.reshape

回傳一個與 self 具有相同資料和元素數量,但具有指定形狀的張量。

Tensor.reshape_as

回傳與 other 相同形狀的此張量。

Tensor.resize_

self 張量調整為指定大小。

Tensor.resize_as_

調整 self 張量的大小,使其與指定的 tensor 相同。

Tensor.retain_grad

啟用此張量以使其 gradbackward() 期間被填充。

Tensor.retains_grad

如果此張量是非葉節點,並且啟用了其 grad 以在 backward() 期間被填充,則為 True,否則為 False

Tensor.roll

請參閱 torch.roll()

Tensor.rot90

請參閱 torch.rot90()

Tensor.round

請參閱 torch.round()

Tensor.round_

round() 的原地 (in-place) 版本

Tensor.rsqrt

請參閱 torch.rsqrt()

Tensor.rsqrt_

rsqrt() 的原地 (in-place) 版本

Tensor.scatter

torch.Tensor.scatter_() 的異地 (out-of-place) 版本

Tensor.scatter_

將張量 src 中的所有值,根據 index 張量中指定的索引,寫入 self 中。

Tensor.scatter_add_

以類似於 scatter_() 的方式,將張量 src 中的所有值,根據 index 張量中指定的索引,加到 self 中。

Tensor.scatter_add

torch.Tensor.scatter_add_() 的異地 (out-of-place) 版本

Tensor.scatter_reduce_

使用透過 reduce 參數定義的縮減方式("sum""prod""mean""amax""amin"),將 src 張量中的所有值縮減到 index 張量中指定的 self 張量索引。

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() 的原地 (in-place) 版本

Tensor.sign

請參閱 torch.sign()

Tensor.sign_

sign() 的原地 (in-place) 版本

Tensor.signbit

請參閱 torch.signbit()

Tensor.sgn

請參閱 torch.sgn()

Tensor.sgn_

sgn() 的原地 (in-place) 版本

Tensor.sin

請參閱 torch.sin()

Tensor.sin_

sin() 的原地 (in-place) 版本

Tensor.sinc

請參閱 torch.sinc()

Tensor.sinc_

sinc() 的原地 (in-place) 版本

Tensor.sinh

請參閱 torch.sinh()

Tensor.sinh_

sinh() 的原地 (in-place) 版本

Tensor.asinh

請參閱 torch.asinh()

Tensor.asinh_

asinh() 的原地 (in-place) 版本

Tensor.arcsinh

請參閱 torch.arcsinh()

Tensor.arcsinh_

arcsinh() 的原地 (in-place) 版本

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() 的原地 (in-place) 版本

Tensor.square

請參閱 torch.square()

Tensor.square_

square() 的原地 (in-place) 版本

Tensor.squeeze

請參閱 torch.squeeze()

Tensor.squeeze_

squeeze() 的原地 (in-place) 版本

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 張量的步幅 (stride)。

Tensor.sub

請參閱 torch.sub()

Tensor.sub_

sub() 的原地 (in-place) 版本

Tensor.subtract

請參閱 torch.subtract()

Tensor.subtract_

subtract() 的原地 (in-place) 版本。

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() 的原地 (in-place) 版本

Tensor.tensor_split

請參閱 torch.tensor_split()

Tensor.tile

請參閱 torch.tile()

Tensor.to

執行 Tensor dtype 和/或裝置轉換。

Tensor.to_mkldnn

返回 torch.mkldnn 佈局中張量的副本。

Tensor.take

請參閱 torch.take()

Tensor.take_along_dim

請參閱 torch.take_along_dim()

Tensor.tan

請參閱 torch.tan()

Tensor.tan_

tan() 的原地 (in-place) 版本

Tensor.tanh

請參閱 torch.tanh()

Tensor.tanh_

tanh() 的原地 (in-place) 版本

Tensor.atanh

請參閱 torch.atanh()

Tensor.atanh_

atanh() 的原地 (in-place) 版本

Tensor.arctanh

請參閱 torch.arctanh()

Tensor.arctanh_

arctanh() 的原地 (in-place) 版本

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() 的原地 (In-place) 版本

Tensor.triangular_solve

請參閱 torch.triangular_solve()

Tensor.tril

請參閱 torch.tril()

Tensor.tril_

tril() 的原地 (In-place) 版本

Tensor.triu

請參閱 torch.triu()

Tensor.triu_

triu() 的原地 (In-place) 版本

Tensor.true_divide

請參閱 torch.true_divide()

Tensor.true_divide_

true_divide_() 的原地 (In-place) 版本

Tensor.trunc

請參閱 torch.trunc()

Tensor.trunc_

trunc() 的原地 (In-place) 版本

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() 的原地 (In-place) 版本

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() 的原地 (In-place) 版本

Tensor.xpu

傳回此物件在 XPU 記憶體中的副本。

Tensor.zero_

用零填充 self 張量。

文件

訪問 PyTorch 的全面開發人員文檔

檢視文檔

教學課程

獲取針對初學者和高級開發人員的深入教學課程

檢視教學課程

資源

查找開發資源並獲得問題的解答

檢視資源