# torch.Tensor
# torch.Tensor
`torch.Tensor`是一種包含單一數據類型元素的多維矩陣。
Torch定義了七種CPU tensor類型和八種GPU tensor類型:
Data tyoeCPU tensorGPU tensor32-bit floating point`torch.FloatTensor``torch.cuda.FloatTensor`64-bit floating point`torch.DoubleTensor``torch.cuda.DoubleTensor`16-bit floating pointN/A`torch.cuda.HalfTensor`8-bit integer (unsigned)`torch.ByteTensor``torch.cuda.ByteTensor`8-bit integer (signed)`torch.CharTensor``torch.cuda.CharTensor`16-bit integer (signed)`torch.ShortTensor``torch.cuda.ShortTensor`32-bit integer (signed)`torch.IntTensor``torch.cuda.IntTensor`64-bit integer (signed)`torch.LongTensor``torch.cuda.LongTensor``torch.Tensor`是默認的tensor類型(`torch.FlaotTensor`)的簡稱。
一個張量tensor可以從Python的`list`或序列構建:
```
>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
```
一個空張量tensor可以通過規定其大小來構建:
```
>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
[torch.IntTensor of size 2x4]
```
可以用python的索引和切片來獲取和修改一個張量tensor中的內容:
```
>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
6.0
>>> x[0][1] = 8
>>> print(x)
1 8 3
4 5 6
[torch.FloatTensor of size 2x3]
```
每一個張量tensor都有一個相應的`torch.Storage`用來保存其數據。類tensor提供了一個存儲的多維的、橫向視圖,并且定義了在數值運算。
> **!注意:**會改變tensor的函數操作會用一個下劃線后綴來標示。比如,`torch.FloatTensor.abs_()`會在原地計算絕對值,并返回改變后的tensor,而`tensor.FloatTensor.abs()`將會在一個新的tensor中計算結果。
```
class torch.Tensor
class torch.Tensor(*sizes)
class torch.Tensor(size)
class torch.Tensor(sequence)
class torch.Tensor(ndarray)
class torch.Tensor(tensor)
class torch.Tensor(storage)
```
根據可選擇的大小和數據新建一個tensor。 如果沒有提供參數,將會返回一個空的零維張量。如果提供了`numpy.ndarray`,`torch.Tensor`或`torch.Storage`,將會返回一個有同樣參數的tensor.如果提供了python序列,將會從序列的副本創建一個tensor。
#### abs() → Tensor
請查看`torch.abs()`
#### abs\_() → Tensor
`abs()`的in-place運算形式
#### acos() → Tensor
請查看`torch.acos()`
#### acos\_() → Tensor
`acos()`的in-place運算形式
#### add(*value*)
請查看`torch.add()`
#### add*(\_value*)
`add()`的in-place運算形式
#### addbmm(*beta=1, mat, alpha=1, batch1, batch2*) → Tensor
請查看`torch.addbmm()`
#### addbmm*(\_beta=1, mat, alpha=1, batch1, batch2*) → Tensor
`addbmm()`的in-place運算形式
#### addcdiv(*value=1, tensor1, tensor2*) → Tensor
請查看`torch.addcdiv()`
#### addcdiv*(\_value=1, tensor1, tensor2*) → Tensor
`addcdiv()`的in-place運算形式
#### addcmul(*value=1, tensor1, tensor2*) → Tensor
請查看`torch.addcmul()`
#### addcmul*(\_value=1, tensor1, tensor2*) → Tensor
`addcmul()`的in-place運算形式
#### addmm(*beta=1, mat, alpha=1, mat1, mat2*) → Tensor
請查看`torch.addmm()`
#### addmm*(\_beta=1, mat, alpha=1, mat1, mat2*) → Tensor
`addmm()`的in-place運算形式
#### addmv(*beta=1, tensor, alpha=1, mat, vec*) → Tensor
請查看`torch.addmv()`
#### addmv*(\_beta=1, tensor, alpha=1, mat, vec*) → Tensor
`addmv()`的in-place運算形式
#### addr(*beta=1, alpha=1, vec1, vec2*) → Tensor
請查看`torch.addr()`
#### addr*(\_beta=1, alpha=1, vec1, vec2*) → Tensor
`addr()`的in-place運算形式
#### apply*(\_callable*) → Tensor
將函數`callable`作用于tensor中每一個元素,并將每個元素用`callable`函數返回值替代。
> **!注意:**該函數只能在CPU tensor中使用,并且不應該用在有較高性能要求的代碼塊。
>
> #### asin() → Tensor
>
> 請查看`torch.asin()`
>
> #### asin\_() → Tensor
>
> `asin()`的in-place運算形式
>
> #### atan() → Tensor
>
> 請查看`torch.atan()`
>
> #### atan2() → Tensor
>
> 請查看`torch.atan2()`
>
> #### atan2\_() → Tensor
>
> `atan2()`的in-place運算形式
>
> #### atan\_() → Tensor
>
> `atan()`的in-place運算形式
>
> #### baddbmm(*beta=1, alpha=1, batch1, batch2*) → Tensor
>
> 請查看`torch.baddbmm()`
>
> #### baddbmm*(\_beta=1, alpha=1, batch1, batch2*) → Tensor
>
> `baddbmm()`的in-place運算形式
>
> #### bernoulli() → Tensor
>
> 請查看`torch.bernoulli()`
>
> #### bernoulli\_() → Tensor
>
> `bernoulli()`的in-place運算形式
>
> #### bmm(*batch2*) → Tensor
>
> 請查看`torch.bmm()`
>
> #### byte() → Tensor
>
> 將tensor改為byte類型
>
> #### bmm(*median=0, sigma=1, \*, generator=None*) → Tensor
>
> 將tensor中元素用柯西分布得到的數值填充:
>
> $$ P(x)={\\frac1 \\pi} {\\frac \\sigma {(x-median)^2 + \\sigma^2}}
>
> $$
>
> #### ceil() → Tensor
>
> 請查看`torch.ceil()`
>
> #### ceil\_() → Tensor
>
> `ceil()`的in-place運算形式
>
> #### char()
>
> 將tensor元素改為char類型
>
> #### chunk(*n\_chunks, dim=0*) → Tensor
>
> 將tensor分割為tensor元組. 請查看`torch.chunk()`
>
> #### clamp(*min, max*) → Tensor
>
> 請查看`torch.clamp()`
>
> #### clamp*(\_min, max*) → Tensor
>
> `clamp()`的in-place運算形式
>
> #### clone() → Tensor
>
> 返回與原tensor有相同大小和數據類型的tensor
>
> #### contiguous() → Tensor
>
> 返回一個內存連續的有相同數據的tensor,如果原tensor內存連續則返回原tensor
>
> #### copy*(\_src, async=False*) → Tensor
>
> 將`src`中的元素復制到tensor中并返回這個tensor。 兩個tensor應該有相同數目的元素,可以是不同的數據類型或存儲在不同的設備上。 **參數:**
>
> - **src** (*Tensor*)-復制的源tensor
> - **async** (*bool*)-如果為True并且復制是在CPU和GPU之間進行的,則復制后的拷貝可能會與源信息異步,對于其他類型的復制操作則該參數不會發生作用。
#### cos() → Tensor
請查看`torch.cos()`
#### cos\_() → Tensor
`cos()`的in-place運算形式
#### cosh() → Tensor
請查看`torch.cosh()`
#### cosh\_() → Tensor
`cosh()`的in-place運算形式
#### cpu() → Tensor
如果在CPU上沒有該tensor,則會返回一個CPU的副本
#### cross(*other, dim=-1*) → Tensor
請查看`torch.cross()`
#### cuda(device=None, async=False)
返回此對象在CPU內存中的一個副本 如果對象已近存在與CUDA存儲中并且在正確的設備上,則不會進行復制并返回原始對象。
**參數:**
- **device**(*int*)-目的GPU的id,默認為當前的設備。
- **async**(*bool*)-如果為True并且資源在固定內存中,則復制的副本將會與原始數據異步。否則,該參數沒有意義。#### cumprod(*dim*) → Tensor
請查看`torch.cumprod()`#### cumsum(*dim*) → Tensor
請查看`torch.cumsum()`#### data\_ptr() → int
返回tensor第一個元素的地址#### diag(*diagonal=0*) → Tensor
請查看`torch.diag()`#### dim() → int
返回tensor的維數#### dist(*other, p=2*) → Tensor
請查看`torch.dist()`#### div(*value*)
請查看`torch.div()`#### div*(\_value*)
`div()`的in-place運算形式#### dot(*tensor2*) → float
請查看`torch.dot()`#### double()
將該tensor投射為double類型#### eig(*eigenvectors=False*) -> (*Tensor, Tensor*)
請查看`torch.eig()`#### element\_size() → int
返回單個元素的字節大小。 例:
```
>>> torch.FloatTensor().element_size()
4
>>> torch.ByteTensor().element_size()
1
```
#### eq(*other*) → Tensor
請查看`torch.eq()`#### eq*(\_other*) → Tensor
`eq()`的in-place運算形式#### equal(*other*) → bool
請查看`torch.equal()`#### exp() → Tensor
請查看`torch.exp()`#### exp\_() → Tensor
`exp()`的in-place運算形式#### expand(\*sizes)
返回tensor的一個新視圖,單個維度擴大為更大的尺寸。 tensor也可以擴大為更高維,新增加的維度將附在前面。 擴大tensor不需要分配新內存,只是僅僅新建一個tensor的視圖,其中通過將`stride`設為0,一維將會擴展位更高維。任何一個一維的在不分配新內存情況下可擴展為任意的數值。
**參數:**
- **sizes**(*torch.Size or int...*)-需要擴展的大小
**例:**
```
>>> x = torch.Tensor([[1], [2], [3]])
>>> x.size()
torch.Size([3, 1])
>>> x.expand(3, 4)
1 1
1 1
2 2 2 2
3 3 3 3
[torch.FloatTensor of size 3x4]
```
#### expand*as(\_tensor*)
將tensor擴展為參數tensor的大小。 該操作等效與:
```
self.expand(tensor.size())
```
#### exponential*(\_lambd=1, \*, generator=None*) $to$ Tensor
將該tensor用指數分布得到的元素填充:
$$ P(x)= \\lambda e^{- \\lambda x}
$$
#### fill*(\_value*) → Tensor
將該tensor用指定的數值填充
#### float()
將tensor投射為float類型
#### floor() → Tensor
請查看`torch.floor()`
#### floor\_() → Tensor
`floor()`的in-place運算形式
#### fmod(*divisor*) → Tensor
請查看`torch.fmod()`
#### fmod*(\_divisor*) → Tensor
`fmod()`的in-place運算形式
#### frac() → Tensor
請查看`torch.frac()`
#### frac\_() → Tensor
`frac()`的in-place運算形式
#### gather(*dim, index*) → Tensor
請查看`torch.gather()`
#### ge(*other*) → Tensor
請查看`torch.ge()`
#### ge*(\_other*) → Tensor
`ge()`的in-place運算形式
#### gels(*A*) → Tensor
請查看`torch.gels()`
#### geometric*(\_p, \*, generator=None*) → Tensor
將該tensor用幾何分布得到的元素填充:
$$ P(X=k)= (1-p)^{k-1}p
$$
#### geqrf() -> (*Tensor, Tensor*)
請查看`torch.geqrf()`
#### ger(*vec2*) → Tensor
請查看`torch.ger()`
#### gesv(*A*) → Tensor, Tensor
請查看`torch.gesv()`
#### gt(*other*) → Tensor
請查看`torch.gt()`
#### gt*(\_other*) → Tensor
`gt()`的in-place運算形式
#### half()
將tensor投射為半精度浮點類型
#### histc(*bins=100, min=0, max=0*) → Tensor
請查看`torch.histc()`
#### index(*m*) → Tensor
用一個二進制的掩碼或沿著一個給定的維度從tensor中選取元素。`tensor.index(m)`與`tensor[m]`完全相同。
**參數:**
- **m**(*int or Byte Tensor or slice*)-用來選取元素的維度或掩碼#### index*add*(*dim, index, tensor*) → Tensor
按參數index中的索引數確定的順序,將參數tensor中的元素加到原來的tensor中。參數tensor的尺寸必須嚴格地與原tensor匹配,否則會發生錯誤。
**參數:**
- **dim**(*int*)-索引index所指向的維度
- **index**(*LongTensor*)-需要從tensor中選取的指數
- **tensor**(*Tensor*)-含有相加元素的tensor
**例:**
```
>>> x = torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
>>> x.index_add_(0, index, t)
>>> x
2 3 4
8 9 10
5 6 7
[torch.FloatTensor of size 3x3]
```
#### index*copy*(*dim, index, tensor*) → Tensor
按參數index中的索引數確定的順序,將參數tensor中的元素復制到原來的tensor中。參數tensor的尺寸必須嚴格地與原tensor匹配,否則會發生錯誤。
**參數:**
- **dim** (*int*)-索引index所指向的維度
- **index** (*LongTensor*)-需要從tensor中選取的指數
- **tensor** (*Tensor*)-含有被復制元素的tensor
**例:**
```
>>> x = torch.Tensor(3, 3)
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
>>> x.index_copy_(0, index, t)
>>> x
1 2 3
7 8 9
4 5 6
[torch.FloatTensor of size 3x3]
```
#### index*fill*(*dim, index, val*) → Tensor
按參數index中的索引數確定的順序,將原tensor用參數`val`值填充。
**參數:**
- **dim** (*int*)-索引index所指向的維度
- **index** (*LongTensor*)-索引
- **val** (*Tensor*)-填充的值
**例:**
```
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2])
>>> x.index_fill_(0, index, -1)
>>> x
-1 2 -1
-1 5 -1
-1 8 -1
[torch.FloatTensor of size 3x3]
```
#### index*select(\_dim, index*) → Tensor
請查看`torch.index_select()`
#### int()
將該tensor投射為int類型
#### inverse() → Tensor
請查看`torch.inverse()`
#### is\_contiguous() → bool
如果該tensor在內存中是連續的則返回True。
#### is\_cuda
#### is\_pinned()
如果該tensor在固定內內存中則返回True
#### is*set\_to(\_tensor*) → bool
如果此對象引用與Torch C API相同的`THTensor`對象作為給定的張量,則返回True。
#### is\_signed()
#### kthvalue(*k, dim=None*) -> (*Tensor, LongTensor*)
請查看`torch.kthvalue()`
#### le(*other*) → Tensor
請查看`torch.le()`
#### le*(\_other*) → Tensor
`le()`的in-place運算形式
#### lerp(*start, end, weight*)
請查看`torch.lerp()`
#### lerp\_(*start, end, weight*) → Tensor
`lerp()`的in-place運算形式
#### log() → Tensor
請查看`torch.log()`
#### loglp() → Tensor
請查看`torch.loglp()`
#### loglp\_() → Tensor
`loglp()`的in-place運算形式
#### log\_()→ Tensor
`log()`的in-place運算形式
#### log*normal*(*mwan=1, std=2,* , gegnerator=None\*)
將該tensor用均值為$\\mu$,標準差為$\\sigma$的對數正態分布得到的元素填充。要注意`mean`和`stdv`是基本正態分布的均值和標準差,不是返回的分布:
$$ P(X)= \\frac {1} {x \\sigma \\sqrt {2 \\pi}}e^{- \\frac {(lnx- \\mu)^2} {2 \\sigma^2}}
$$
#### long()
將tensor投射為long類型
#### lt(*other*) → Tensor
請查看`torch.lt()`
#### lt\_(*other*) → Tensor
`lt()`的in-place運算形式
#### map\_(*tensor, callable*)
將`callable`作用于本tensor和參數tensor中的每一個元素,并將結果存放在本tensor中。`callable`應該有下列標志:
```
def callable(a, b) -> number
```
#### masked*copy*(*mask, source*)
將`mask`中值為1元素對應的`source`中位置的元素復制到本tensor中。`mask`應該有和本tensor相同數目的元素。`source`中元素的個數最少為`mask`中值為1的元素的個數。
**參數:**
- **mask** (*ByteTensor*)-二進制掩碼
- **source** (*Tensor*)-復制的源tensor
> **注意:**`mask`作用于`self`自身的tensor,而不是參數中的`source`。
>
> #### masked*fill*(*mask, value*)
>
> 在`mask`值為1的位置處用`value`填充。`mask`的元素個數需和本tensor相同,但尺寸可以不同。
**參數:**
- **mask** (*ByteTensor*)-二進制掩碼
- **value** (*Tensor*)-用來填充的值#### masked\_select(*mask*) → Tensor
請查看`torch.masked_select()`#### max(*dim=None*) -> *float or(Tensor, Tensor)*
請查看`torch.max()`#### mean(*dim=None*) -> *float or(Tensor, Tensor)*
請查看`torch.mean()`#### median(*dim=-1, value=None, indices=None*) -> *(Tensor, LongTensor)*
請查看`torch.median()`#### min(*dim=None*) -> *float or(Tensor, Tensor)*
請查看`torch.min()`#### mm(*mat2*) → Tensor
請查看`torch.mm()`#### mode(*dim=-1, value=None, indices=None*) -> *(Tensor, LongTensor)*
請查看`torch.mode()`#### mul(*value*) → Tensor
請查看`torch.mul()`#### mul\_(*value*)
`mul()`的in-place運算形式#### multinomial(*num\_samples, replacement=False,* , generator=None\*) → Tensor
請查看`torch.multinomial()`#### mv(*vec*) → Tensor
請查看`torch.mv()`#### narrow(*dimension, start, length*) → Te
返回一個本tensor經過縮小后的tensor。維度`dim`縮小范圍是`start`到`start+length`。原tensor與返回的tensor共享相同的底層內存。
**參數:**
- **dimension** (*int*)-需要縮小的維度
- **start** (*int*)-起始維度
- **length** (*int*)-
**例:**
```
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> x.narrow(0, 0, 2)
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
>>> x.narrow(1, 1, 2)
2 3
5 6
8 9
[torch.FloatTensor of size 3x2]
```
#### ndimension() → int
`dim()`的另一種表示。
#### ne(*other*) → Tensor
請查看`torch.ne()`
#### ne\_(*other*) → Tensor
`ne()`的in-place運算形式
#### neg() → Tensor
請查看`torch.neg()`
#### neg\_() → Tensor
`neg()`的in-place運算形式
#### nelement() → int
`numel()`的另一種表示
#### new(**args, \**kwargs*)
構建一個有相同數據類型的tensor
#### nonezero() → LongTensor
請查看`torch.nonezero()
#### norm(*p=2*) → float
請查看`torch.norm()
#### normal\_(*mean=0, std=1,* , gengerator=None\*)
將tensor用均值為`mean`和標準差為`std`的正態分布填充。
#### numel() → int
請查看`numel()`
#### numpy() → ndarray
將該tensor以NumPy的形式返回`ndarray`,兩者共享相同的底層內存。原tensor改變后會相應的在`ndarray`有反映,反之也一樣。
#### orgqr(*input2*) → Tensor
請查看`torch.orgqr()`
#### ormqr(*input2, input3, left=True, transpose=False*) → Tensor
請查看`torch.ormqr()`
#### permute(*dims*)
將tensor的維度換位。
**參數:**
- **\*dims** (*int..*)-換位順序
**例:**
```
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3])
```
#### pin\_memory()
如果原來沒有在固定內存中,則將tensor復制到固定內存中。
#### potrf(*upper=True*) → Tensor
請查看`torch.potrf()`
#### potri(*upper=True*) → Tensor
請查看`torch.potri()`
#### potrs(*input2, upper=True*) → Tensor
請查看`torch.potrs()`
#### pow(*exponent*)
請查看`torch.pow()`
#### pow\_()
`pow()`的in-place運算形式
#### prod()) → float
請查看`torch.prod()`
#### pstrf(*upper=True, tol=-1*) -> (*Tensor, IntTensor*)
請查看`torch.pstrf()`
#### qr()-> (*Tensor, IntTensor*)
請查看`torch.qr()`
#### random*(\_from=0, to=None, \*, generator=None*)
將tensor用從在\[from, to-1\]上的正態分布或離散正態分布取樣值進行填充。如果沒有明確說明,則填充值僅由本tensor的數據類型限定。
#### reciprocal() → Tensor
請查看`torch.reciprocal()`
#### reciprocal\_() → Tensor
`reciprocal()`的in-place運算形式
#### remainder(*divisor*) → Tensor
請查看`torch.remainder()`
#### remainder\_(*divisor*) → Tensor
`remainder()`的in-place運算形式
#### renorm(*p, dim, maxnorm*) → Tensor
請查看`torch.renorm()`
#### renorm\_(*p, dim, maxnorm*) → Tensor
`renorm()`的in-place運算形式
#### repeat(*\*sizes*)
沿著指定的維度重復tensor。 不同于`expand()`,本函數復制的是tensor中的數據。
**參數:**
- **\*sizes** (*torch.Size ot int...*)-沿著每一維重復的次數
**例:**
```
>>> x = torch.Tensor([1, 2, 3])
>>> x.repeat(4, 2)
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
[torch.FloatTensor of size 4x6]
>>> x.repeat(4, 2, 1).size()
torch.Size([4, 2, 3])
```
#### resize*(*\*sizes\_)
將tensor的大小調整為指定的大小。如果元素個數比當前的內存大小大,就將底層存儲大小調整為與新元素數目一致的大小。如果元素個數比當前內存小,則底層存儲不會被改變。原來tensor中被保存下來的元素將保持不變,但新內存將不會被初始化。
**參數:**
- **\*sizes** (*torch.Size or int...*)-需要調整的大小
**例:**
```
>>> x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
>>> x.resize_(2, 2)
>>> x
1 2
3 4
[torch.FloatTensor of size 2x2]
```
#### resize*as*(*tensor*)
將本tensor的大小調整為與參數中的tensor相同的大小。等效于:
```
self.resize_(tensor.size())
```
#### round() → Tensor
請查看`torch.round()`
#### round\_() → Tensor
`round()`的in-place運算形式
#### rsqrt() → Tensor
請查看`torch.rsqrt()`
#### rsqrt\_() → Tensor
`rsqrt()`的in-place運算形式
#### scatter\_(*input, dim, index, src*) → Tensor
將`src`中的所有值按照`index`確定的索引寫入本tensor中。其中索引是根據給定的dimension,dim按照`gather()`描述的規則來確定。
注意,index的值必須是在到*(self.size(dim)-1)*之間,
**參數:**
- **input** (*Tensor*)-源tensor
- **dim** (*int*)-索引的軸向
- **index** (*LongTensor*)-散射元素的索引指數
- **src** (*Tensor or float*)-散射的源元素
**例:**
```
>>> x = torch.rand(2, 5)
>>> x
0.4319 0.6500 0.4080 0.8760 0.2355
0.2609 0.4711 0.8486 0.8573 0.1029
[torch.FloatTensor of size 2x5]
>>> torch.zeros(3, 5).scatter_(0, torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
0.4319 0.4711 0.8486 0.8760 0.2355
0.0000 0.6500 0.0000 0.8573 0.0000
0.2609 0.0000 0.4080 0.0000 0.1029
[torch.FloatTensor of size 3x5]
>>> z = torch.zeros(2, 4).scatter_(1, torch.LongTensor([[2], [3]]), 1.23)
>>> z
0.0000 0.0000 1.2300 0.0000
0.0000 0.0000 0.0000 1.2300
[torch.FloatTensor of size 2x4]
```
#### select(*dim, index*) → Tensor or number
按照index中選定的維度將tensor切片。如果tensor是一維的,則返回一個數字。否則,返回給定維度已經被移除的tensor。
**參數:**
- **dim** (*int*)-切片的維度
- **index** (*int*)-用來選取的索引
> **!注意:**`select()`等效于切片。例如,`tensor.select(0, index)`等效于`tensor[index]`,`tensor.select(2, index)`等效于`tensor[:, :, index]`.
>
> #### set(*source=None, storage\_offset=0, size=None, stride=None*)
>
> 設置底層內存,大小和步長。如果`tensor`是一個tensor,則將會與本tensor共享底層內存并且有相同的大小和步長。改變一個tensor中的元素將會反映在另一個tensor。 如果`source`是一個`Storage`,則將設置底層內存,偏移量,大小和步長。
**參數:**
- **source** (*Tensor or Storage*)-用到的tensor或內存
- **storage\_offset** (*int*)-內存的偏移量
- **size** (*torch.Size*)-需要的大小,默認為源tensor的大小。
- **stride**(*tuple*)-需要的步長,默認為C連續的步長。#### share*memory*()
將底層內存移到共享內存中。 如果底層內存已經在共享內存中是將不進行任何操作。在共享內存中的tensor不能調整大小。#### short()
將tensor投射為short類型。#### sigmoid() → Tensor
請查看`torch.sigmoid()`#### sigmoid\_() → Tensor
`sidmoid()`的in-place運算形式#### sign() → Tensor
請查看`torch.sign()`#### sign\_() → Tensor
`sign()`的in-place運算形式#### sin() → Tensor
請查看`torch.sin()`#### sin\_() → Tensor
`sin()`的in-place運算形式#### sinh() → Tensor
請查看`torch.sinh()`#### sinh\_() → Tensor
`sinh()`的in-place運算形式#### size() → torch.Size
返回tensor的大小。返回的值是`tuple`的子類。
**例:**
```
>>> torch.Tensor(3, 4, 5).size()
torch.Size([3, 4, 5])
```
#### sort(*dim=None, descending=False*) -> (*Tensor, LongTensor*)
請查看`torhc.sort()`
#### split(*split\_size, dim=0*)
將tensor分割成tensor數組。 請查看`torhc.split()`
#### sqrt() → Tensor
請查看`torch.sqrt()`
#### sqrt\_() → Tensor
`sqrt()`的in-place運算形式
#### squeeze(*dim=None*) → Tensor
請查看`torch.squeeze()`
#### squeeze*(\_dim=None*) → Tensor
`squeeze()`的in-place運算形式
#### std() → float
請查看`torch.std()`
#### storage() → torch.Storage
返回底層內存。
#### storage\_offset() → int
以儲存元素的個數的形式返回tensor在地城內存中的偏移量。 例:
```
>>> x = torch.Tensor([1, 2, 3, 4, 5])
>>> x.storage_offset()
0
>>> x[3:].storage_offset()
3
```
#### *classmethod()* storage\_type()
#### stride() → Tensor
返回tesnor的步長。
#### sub(*value, other*) → Tensor
從tensor中抽取一個標量或tensor。如果`value`和`other`都是給定的,則在使用之前`other`的每一個元素都會被`value`縮放。
#### sub*(\_x*) → Tensor
`sub()`的in-place運算形式
#### sum(*dim=None*) → Tensor
請查看`torch.sum()`
#### svd(*some=True*) -> (Tensor, Tensor, Tensor)
請查看`torch.svd()`
#### symeig(\_eigenvectors=False, upper=True) -> (Tensor, Tensor)
請查看`torch.symeig()`
#### t() → Tensor
請查看`torch.t()`
#### t() → Tensor
`t()`的in-place運算形式
#### tan() → Tensor
請查看`torch.tan()`
#### tan\_() → Tensor
`tan()`的in-place運算形式
#### tanh() → Tensor
請查看`torch.tanh()`
#### tanh\_() → Tensor
`tanh()`的in-place運算形式
#### tolist()
返回一個tensor的嵌套列表表示。
#### topk(*k, dim=None, largest=True, sorted=True*) -> (Tensor, LongTensor)
請查看`torch.topk()`
#### trace() → float
請查看`torch.trace()`
#### transpose(*dim0, dim1*) → Tensor
請查看`torch.transpose()`
#### transpose(*dim0, dim1*) → Tensor
`transpose()`的in-place運算形式
#### tril(*k=0*) → Tensor
請查看`torch.tril()`
#### tril*(\_k=0*) → Tensor
`tril()`的in-place運算形式
#### triu(*k=0*) → Tensor
請查看`torch.triu()`
#### triu(*k=0*) → Tensor
`triu()`的in-place運算形式
#### trtrs(*A, upper=True, transpose=False, unitriangular=False*) -> (Tensor, Tensor)
請查看`torch.trtrs()`
#### trunc() → Tensor
請查看`torch.trunc()`
#### trunc() → Tensor
`trunc()`的in-place運算形式
#### type(*new\_type=None, async=False*)
將對象投為指定的類型。 如果已經是正確的類型,則不會進行復制并返回原對象。
**參數:**
- **new\_type** (*type or string*)-需要的類型
- **async** (*bool*)-如果為True,并且源地址在固定內存中,目的地址在GPU或者相反,則會相對于源主異步執行復制。否則,該參數不發揮作用。#### type*as(\_tesnor*)
將tensor投射為參數給定tensor類型并返回。 如果tensor已經是正確的類型則不會執行操作。等效于:
```
self.type(tensor.type())
```
**參數:**
- **tensor** (Tensor):有所需要類型的tensor#### unfold(*dim, size, step*) → Tensor
返回一個tensor,其中含有在`dim`維tianchong度上所有大小為`size`的分片。兩個分片之間的步長為`step`。 如果*sizedim*是dim維度的原始大小,則在返回tensor中的維度dim大小是*(sizedim-size)/step+1*維度大小的附加維度將附加在返回的tensor中。
**參數:**
- **dim** (*int*)-需要展開的維度
- **size** (*int*)-每一個分片需要展開的大小
- **step** (*int*)-相鄰分片之間的步長
**例:**
```
>>> x = torch.arange(1, 8)
>>> x
1
2
3
4
5
6
7
[torch.FloatTensor of size 7]
>>> x.unfold(0, 2, 1)
1 2
2 3
3 4
4 5
5 6
6 7
[torch.FloatTensor of size 6x2]
>>> x.unfold(0, 2, 2)
1 2
3 4
5 6
[torch.FloatTensor of size 3x2]
```
#### uniform*(\_from=0, to=1*) → Tensor
將tensor用從均勻分布中抽樣得到的值填充。
#### unsqueeze(*dim*)
請查看`torch.unsqueeze()`
#### unsqueeze*(\_dim*) → Tensor
`unsqueeze()`的in-place運算形式
#### var()
請查看`torch.var()`
#### view(*\*args*) → Tensor
返回一個有相同數據但大小不同的tensor。 返回的tensor必須有與原tensor相同的數據和相同數目的元素,但可以有不同的大小。一個tensor必須是連續的`contiguous()`才能被查看。
**例:**
```
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8) # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8])
```
#### view*as(\_tensor*)
返回被視作與給定的tensor相同大小的原tensor。 等效于:
```
self.view(tensor.size())
```
#### zero\_()
用0填充該tensor。
- PyTorch 中文文檔
- 主頁
- 自動求導機制
- CUDA語義
- 擴展PyTorch
- 多進程最佳實踐
- 序列化語義
- torch
- torch.Tensor
- torch.Storage
- torch.nn
- torch.nn.functional
- torch.autograd
- torch.optim
- torch.nn.init
- torch.multiprocessing
- torch.legacy
- torch.cuda
- torch.utils.ffi
- torch.utils.data
- torch.utils.model_zoo
- torchvision
- torchvision.datasets
- torchvision.models
- torchvision.transforms
- torchvision.utils
- 致謝