torch.Tensor
是一种包含单一数据类型元素的多维矩阵。
Torch定义了七种CPU tensor类型和八种GPU tensor类型:
Data tyoe | CPU tensor | GPU tensor |
---|---|---|
32-bit floating point | torch.FloatTensor |
torch.cuda.FloatTensor |
64-bit floating point | torch.DoubleTensor |
torch.cuda.DoubleTensor |
16-bit floating point | N/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。
请查看torch.abs()
abs()
的in-place运算形式
请查看torch.acos()
acos()
的in-place运算形式
请查看torch.add()
add()
的in-place运算形式
请查看torch.addbmm()
addbmm()
的in-place运算形式
请查看torch.addcdiv()
addcdiv()
的in-place运算形式
请查看torch.addcmul()
addcmul()
的in-place运算形式
请查看torch.addmm()
addmm()
的in-place运算形式
请查看torch.addmv()
addmv()
的in-place运算形式
请查看torch.addr()
addr()
的in-place运算形式
将函数callable
作用于tensor中每一个元素,并将每个元素用callable
函数返回值替代。
!注意: 该函数只能在CPU tensor中使用,并且不应该用在有较高性能要求的代码块。
请查看torch.asin()
asin()
的in-place运算形式
请查看torch.atan()
请查看torch.atan2()
atan2()
的in-place运算形式
atan()
的in-place运算形式
请查看torch.baddbmm()
baddbmm()
的in-place运算形式
请查看torch.bernoulli()
bernoulli()
的in-place运算形式
请查看torch.bmm()
将tensor改为byte类型
将tensor中元素用柯西分布得到的数值填充: $$ P(x)={\frac1 \pi} {\frac \sigma {(x-median)^2 + \sigma^2}} $$
请查看torch.ceil()
ceil()
的in-place运算形式
将tensor元素改为char类型
将tensor分割为tensor元组.
请查看torch.chunk()
请查看torch.clamp()
clamp()
的in-place运算形式
返回与原tensor有相同大小和数据类型的tensor
返回一个内存连续的有相同数据的tensor,如果原tensor内存连续则返回原tensor
将src
中的元素复制到tensor中并返回这个tensor。
两个tensor应该有相同数目的元素,可以是不同的数据类型或存储在不同的设备上。
参数:
请查看torch.cos()
cos()
的in-place运算形式
请查看torch.cosh()
cosh()
的in-place运算形式
如果在CPU上没有该tensor,则会返回一个CPU的副本
请查看torch.cross()
返回此对象在CPU内存中的一个副本 如果对象已近存在与CUDA存储中并且在正确的设备上,则不会进行复制并返回原始对象。
参数:
请查看torch.cumprod()
请查看torch.cumsum()
返回tensor第一个元素的地址
请查看torch.diag()
返回tensor的维数
请查看torch.dist()
请查看torch.div()
div()
的in-place运算形式
请查看torch.dot()
将该tensor投射为double类型
请查看torch.eig()
返回单个元素的字节大小。 例:
>>> torch.FloatTensor().element_size()
4
>>> torch.ByteTensor().element_size()
1
请查看torch.eq()
eq()
的in-place运算形式
请查看torch.equal()
请查看torch.exp()
exp()
的in-place运算形式
返回tensor的一个新视图,单个维度扩大为更大的尺寸。
tensor也可以扩大为更高维,新增加的维度将附在前面。
扩大tensor不需要分配新内存,只是仅仅新建一个tensor的视图,其中通过将stride
设为0,一维将会扩展位更高维。任何一个一维的在不分配新内存情况下可扩展为任意的数值。
参数:
例:
>>> 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]
将tensor扩展为参数tensor的大小。 该操作等效与:
self.expand(tensor.size())
将该tensor用指数分布得到的元素填充: $$ P(x)= \lambda e^{- \lambda x} $$
将该tensor用指定的数值填充
将tensor投射为float类型
请查看torch.floor()
floor()
的in-place运算形式
请查看torch.fmod()
fmod()
的in-place运算形式
请查看torch.frac()
frac()
的in-place运算形式
请查看torch.gather()
请查看torch.ge()
ge()
的in-place运算形式
请查看torch.gels()
将该tensor用几何分布得到的元素填充: $$ P(X=k)= (1-p)^{k-1}p $$
请查看torch.geqrf()
请查看torch.ger()
请查看torch.gesv()
请查看torch.gt()
gt()
的in-place运算形式
将tensor投射为半精度浮点类型
请查看torch.histc()
用一个二进制的掩码或沿着一个给定的维度从tensor中选取元素。tensor.index(m)
与tensor[m]
完全相同。
参数:
按参数index中的索引数确定的顺序,将参数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中的索引数确定的顺序,将参数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中的索引数确定的顺序,将原tensor用参数val
值填充。
参数:
例:
>>> 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]
请查看torch.index_select()
将该tensor投射为int类型
请查看torch.inverse()
如果该tensor在内存中是连续的则返回True。
如果该tensor在固定内内存中则返回True
如果此对象引用与Torch C API相同的THTensor
对象作为给定的张量,则返回True。
请查看torch.kthvalue()
请查看torch.le()
le()
的in-place运算形式
请查看torch.lerp()
lerp()
的in-place运算形式
请查看torch.log()
请查看torch.loglp()
loglp()
的in-place运算形式
log()
的in-place运算形式
将该tensor用均值为$\mu$,标准差为$\sigma$的对数正态分布得到的元素填充。要注意mean
和stdv
是基本正态分布的均值和标准差,不是返回的分布:
$$
P(X)= \frac {1} {x \sigma \sqrt {2 \pi}}e^{- \frac {(lnx- \mu)^2} {2 \sigma^2}}
$$
将tensor投射为long类型
请查看torch.lt()
lt()
的in-place运算形式
将callable
作用于本tensor和参数tensor中的每一个元素,并将结果存放在本tensor中。callable
应该有下列标志:
def callable(a, b) -> number
将mask
中值为1元素对应的source
中位置的元素复制到本tensor中。mask
应该有和本tensor相同数目的元素。source
中元素的个数最少为mask
中值为1的元素的个数。
参数:
注意:
mask
作用于self
自身的tensor,而不是参数中的source
。
在mask
值为1的位置处用value
填充。mask
的元素个数需和本tensor相同,但尺寸可以不同。
参数:
请查看torch.masked_select()
请查看torch.max()
请查看torch.mean()
请查看torch.median()
请查看torch.min()
请查看torch.mm()
请查看torch.mode()
请查看torch.mul()
mul()
的in-place运算形式
请查看torch.multinomial()
请查看torch.mv()
返回一个本tensor经过缩小后的tensor。维度dim
缩小范围是start
到start+length
。原tensor与返回的tensor共享相同的底层内存。
参数:
例:
>>> 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]
dim()
的另一种表示。
请查看torch.ne()
ne()
的in-place运算形式
请查看torch.neg()
neg()
的in-place运算形式
numel()
的另一种表示
构建一个有相同数据类型的tensor
请查看`torch.nonezero()
请查看`torch.norm()
将tensor用均值为mean
和标准差为std
的正态分布填充。
请查看numel()
将该tensor以NumPy的形式返回ndarray
,两者共享相同的底层内存。原tensor改变后会相应的在ndarray
有反映,反之也一样。
请查看torch.orgqr()
请查看torch.ormqr()
将tensor的维度换位。
参数:
例:
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3])
如果原来没有在固定内存中,则将tensor复制到固定内存中。
请查看torch.potrf()
请查看torch.potri()
请查看torch.potrs()
请查看torch.pow()
pow()
的in-place运算形式
请查看torch.prod()
请查看torch.pstrf()
请查看torch.qr()
将tensor用从在[from, to-1]上的正态分布或离散正态分布取样值进行填充。如果没有明确说明,则填充值仅由本tensor的数据类型限定。
请查看torch.reciprocal()
reciprocal()
的in-place运算形式
请查看torch.remainder()
remainder()
的in-place运算形式
请查看torch.renorm()
renorm()
的in-place运算形式
沿着指定的维度重复tensor。
不同于expand()
,本函数复制的是tensor中的数据。
参数:
例:
>>> 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])
将tensor的大小调整为指定的大小。如果元素个数比当前的内存大小大,就将底层存储大小调整为与新元素数目一致的大小。如果元素个数比当前内存小,则底层存储不会被改变。原来tensor中被保存下来的元素将保持不变,但新内存将不会被初始化。
参数:
例:
>>> x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
>>> x.resize_(2, 2)
>>> x
1 2
3 4
[torch.FloatTensor of size 2x2]
将本tensor的大小调整为与参数中的tensor相同的大小。等效于:
self.resize_(tensor.size())
请查看torch.round()
round()
的in-place运算形式
请查看torch.rsqrt()
rsqrt()
的in-place运算形式
将src
中的所有值按照index
确定的索引写入本tensor中。其中索引是根据给定的dimension,dim按照gather()
描述的规则来确定。
注意,index的值必须是在_0_到_(self.size(dim)-1)_之间,
参数:
例:
>>> 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]
按照index中选定的维度将tensor切片。如果tensor是一维的,则返回一个数字。否则,返回给定维度已经被移除的tensor。
参数:
!注意:
select()
等效于切片。例如,tensor.select(0, index)
等效于tensor[index]
,tensor.select(2, index)
等效于tensor[:, :, index]
.
设置底层内存,大小和步长。如果tensor
是一个tensor,则将会与本tensor共享底层内存并且有相同的大小和步长。改变一个tensor中的元素将会反映在另一个tensor。
如果source
是一个Storage
,则将设置底层内存,偏移量,大小和步长。
参数:
将底层内存移到共享内存中。 如果底层内存已经在共享内存中是将不进行任何操作。在共享内存中的tensor不能调整大小。
将tensor投射为short类型。
请查看torch.sigmoid()
sidmoid()
的in-place运算形式
请查看torch.sign()
sign()
的in-place运算形式
请查看torch.sin()
sin()
的in-place运算形式
请查看torch.sinh()
sinh()
的in-place运算形式
返回tensor的大小。返回的值是tuple
的子类。
例:
>>> torch.Tensor(3, 4, 5).size()
torch.Size([3, 4, 5])
请查看torhc.sort()
将tensor分割成tensor数组。
请查看torhc.split()
请查看torch.sqrt()
sqrt()
的in-place运算形式
请查看torch.squeeze()
squeeze()
的in-place运算形式
请查看torch.std()
返回底层内存。
以储存元素的个数的形式返回tensor在地城内存中的偏移量。 例:
>>> x = torch.Tensor([1, 2, 3, 4, 5])
>>> x.storage_offset()
0
>>> x[3:].storage_offset()
3
返回tesnor的步长。
从tensor中抽取一个标量或tensor。如果value
和other
都是给定的,则在使用之前other
的每一个元素都会被value
缩放。
sub()
的in-place运算形式
请查看torch.sum()
请查看torch.svd()
请查看torch.symeig()
请查看torch.t()
t()
的in-place运算形式
请查看torch.tan()
tan()
的in-place运算形式
请查看torch.tanh()
tanh()
的in-place运算形式
返回一个tensor的嵌套列表表示。
请查看torch.topk()
请查看torch.trace()
请查看torch.transpose()
transpose()
的in-place运算形式
请查看torch.tril()
tril()
的in-place运算形式
请查看torch.triu()
triu()
的in-place运算形式
请查看torch.trtrs()
请查看torch.trunc()
trunc()
的in-place运算形式
将对象投为指定的类型。 如果已经是正确的类型,则不会进行复制并返回原对象。
参数:
将tensor投射为参数给定tensor类型并返回。 如果tensor已经是正确的类型则不会执行操作。等效于:
self.type(tensor.type())
参数:
返回一个tensor,其中含有在dim
维tianchong度上所有大小为size
的分片。两个分片之间的步长为step
。
如果_sizedim_是dim维度的原始大小,则在返回tensor中的维度dim大小是_(sizedim-size)/step+1_
维度大小的附加维度将附加在返回的tensor中。
参数:
例:
>>> 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]
将tensor用从均匀分布中抽样得到的值填充。
请查看torch.unsqueeze()
unsqueeze()
的in-place运算形式
请查看torch.var()
返回一个有相同数据但大小不同的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])
返回被视作与给定的tensor相同大小的原tensor。 等效于:
self.view(tensor.size())
用0填充该tensor。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。