例子运行环境为Ubuntu16.04,Python2.7 ,PyTorch.
Tensor
一、拼接张量
1、torch.cat(seq, dim=0, out=None)
参数:
seq (sequence of Tensors) - Python序列或相同类型的张量序列
dim (int, optional) - 沿着此维度连接张量
out (Tensor, optional) - 输出参数
>>> import torch
>>> x=torch.randn(2,3) #randn标准分布
>>> x
tensor([[ 2.9627, -0.1048, -1.2396],
[-1.1191, 0.3302, 0.9481]])
>>> c=torch.cat((x,x,x),0)
>>> d=torch.cat((x,x,x),1)
>>> print(c)
tensor([[ 2.9627, -0.1048, -1.2396],
[-1.1191, 0.3302, 0.9481],
[ 2.9627, -0.1048, -1.2396],
[-1.1191, 0.3302, 0.9481],
[ 2.9627, -0.1048, -1.2396],
[-1.1191, 0.3302, 0.9481]])
>>> print(d)
tensor([[ 2.9627, -0.1048, -1.2396, 2.9627, -0.1048, -1.2396, 2.9627, -0.1048,
-1.2396],
[-1.1191, 0.3302, 0.9481, -1.1191, 0.3302, 0.9481, -1.1191, 0.3302,
0.9481]])
>>> c.size()
(6, 3)
>>> d.size()
(2, 9)
2、torch.stack((Tensor), dim)
>>> import torch
>>> a=torch.IntTensor([[1,2,3],[11,22,33]])
>>> b=torch.IntTensor([[4,5,6],[44,55,66]])
>>> c=torch.stack([a,b],0)
>>> d=torch.stack([a,b],1)
>>> e=torch.stack([a,b],2)
>>> print(c)
tensor([[[ 1, 2, 3],
[11, 22, 33]],
[[ 4, 5, 6],
[44, 55, 66]]], dtype=torch.int32)
>>> print(d)
tensor([[[ 1, 2, 3],
[ 4, 5, 6]],
[[11, 22, 33],
[44, 55, 66]]], dtype=torch.int32)
>>> print(e)
tensor([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[11, 44],
[22, 55],
[33, 66]]], dtype=torch.int32)
>>> c.size()
(2, 2, 3)
>>> d.size()
(2, 2, 3)
>>> e.size()
(2, 3, 2)
c, dim = 0时,
c = [ a, b]
d, dim =1 时,
d = [ [a[0] , b[0] ] , [a[1], b[1] ] ]
e, dim = 2 时,
e=[[[a[0][0],b[0][0]],[a[0][1],b[0][1]],[a[0][2],b[0][2]]],[[a[1][0],b[1][0]],[a[1][1],b[1][1]],[a[1][2],b[1][2]]]]
二、扩大张量
torch.Tensor.expand(*sizes) → Tensor
返回张量的一个新视图,可以将张量的单个维度扩大为更大的尺寸。
张量
也可以扩大为更高维,新增加的维度将附在前面。 扩大张量不需要分配新内存,仅仅是新建一个张量的视图。任意一个一维张量在不分配新内存情况下都可以扩展为任意的维度。
传入-1则意味着维度扩大不涉及这个维度。
参数:
sizes (torch.Size or int…) – 想要扩展的目标维度
>>> import torch
>>> x=torch.Tensor([[1],[2],[3]])
>>> x.size()
(3, 1)
>>> x.expand(3,4)
tensor([[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.]])
>>> x.size()
(3, 1)
三、压缩张量
torch.squeeze(input, dim=None, out=None) → Tensor
参数:
input (Tensor) – 输入张量
dim (int, optional) – 如果给定,则只会在给定维度压缩
out (Tensor, optional) – 输出张量
1、除去输入张量input中数值为1的维度,并返回新的张量。如果输入张量的形状为(A×1×B×C×1×D),那么输出张量的形状为(A×B×C×D)。
2、当通过dim参数指定维度时,维度压缩操作只会在指定的维度上进行。如果输入向量的形状为(A×1×B),squeeze(input, 0)会保持张量的维度不变,只有在执行squeeze(input, 1)时,输入张量的形状会被压缩至(A×B)。
3、如果一个张量只有1个维度,那么它不会受到上述方法的影响。
4、输出的张量与原张量共享内存,如果改变其中的一个,另一个也会改变。
>>> import torch
>>> x=torch.zeros(2,1,2,1,2)
>>> x.size()
(2, 1, 2, 1, 2)
>>> print(x)
tensor([[[[[0., 0.]],
[[0., 0.]]]],
[[[[0., 0.]],
[[0., 0.]]]]])
>>> a=torch.squeeze(x)
>>> b=torch.squeeze(x,0)
>>> c=torch.squeeze(x,1)
>>> print(a)
tensor([[[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.]]])
>>> a.size()
(2, 2, 2)
>>> print(b)
tensor([[[[[0., 0.]],
[[0., 0.]]]],
[[[[0., 0.]],
[[0., 0.]]]]])
>>> b.size()
(2, 1, 2, 1, 2)
>>> print(c)
tensor([[[[0., 0.]],
[[0., 0.]]],
[[[0., 0.]],
[[0., 0.]]]])
>>> c.size()
(2, 2, 1, 2)
四、复制张量
torch.Tensor.unfold(dim, size, step) → Tensor
返回一个新的张量,其中元素复制于有原张量在dim维度上的数据,复制重复size次,复制时的步进值为step。
参数:
dim (int) - 目标维度
size (int) - 复制重复的次数(展开维度)
step (int) - 步长
>>> import torch
>>> x=torch.arange(1,8) #arange(s,e,step)从s到e,步长为step
>>> x
tensor([1, 2, 3, 4, 5, 6, 7])
>>> x.unfold(0,1,1)
tensor([[1],
[2],
[3],
[4],
[5],
[6],
[7]])
>>> x.unfold(0,2,1)
tensor([[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6],
[6, 7]])
>>> x.unfold(0,3,1)
tensor([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]])
>>> x.unfold(0,2,2)
tensor([[1, 2],
[3, 4],
[5, 6]])
>>> x.unfold(0,2,3)
tensor([[1, 2],
[4, 5]])
五、缩小张量
torch.Tensor.narrow(dimension, start, length) → Tensor
返回一个经过缩小后的张量。操作的维度由dimension指定。缩小范围是从start开始到start+length。执行本方法的张量与返回的张量共享相同的底层内存。
参数:
dimension (int) – 要进行缩小的维度
start (int) – 开始维度索引
length (int) – 缩小持续的长度
>>> import torch
>>> x=torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
>>> a=x.narrow(1,1,2)
>>> a.size()
(3, 2)
>>> print(a)
tensor([[2., 3.],
[5., 6.],
[8., 9.]])
>>> b=x.narrow(0,1,2)
>>> b.size()
(2, 3)
>>> print(b)
tensor([[4., 5., 6.],
[7., 8., 9.]])
>>> x.narrow(0,0,2)
tensor([[1., 2., 3.],
[4., 5., 6.]])
>>> x.size()
(2, 3)
六、张量变形
torch.Tensor.view(*args) → Tensor
返回一个有相同数据但是不同形状的新的向量。
参数:
args (torch.Size or int…) - 理想的指定尺寸
>>> import torch
>>> x=torch.randn(4,4)
>>> print(x)
tensor([[ 0.4701, -0.3604, 0.9954, 0.9410],
[-1.6931, -0.1464, -0.9245, 1.8368],
[-0.7987, -0.8332, -0.1940, 0.0370],
[ 0.2307, 0.5712, 2.4788, 1.4932]])
>>> x.size()
(4, 4)
>>> y=x.view(16)
>>> print(y)
tensor([ 0.4701, -0.3604, 0.9954, 0.9410, -1.6931, -0.1464, -0.9245, 1.8368,
-0.7987, -0.8332, -0.1940, 0.0370, 0.2307, 0.5712, 2.4788, 1.4932])
>>> y.size()
(16,)
七、重设张量尺寸
torch.Tensor.resize_(*sizes)
将张量的尺寸调整为指定的大小。
如果元素个数比当前的内存大,就将底层存储大小调整为与新元素数目一致的大小。
如果元素个数比当前内存小,则底层存储不会被改变。原来张量中被保存下来的元素将保持不变,但新内存将不会被初始化。
参数:
sizes (torch.Size or int…) - 需要调整的大小
>>> import torch
>>> x = torch.randn(2, 3, 5)
>>> print(x)
tensor([[[ 3.1125e+00, 7.0817e-01, 5.5992e-01, -3.3711e-01, 2.1945e+00],
[-9.0231e-02, -1.4458e+00, -8.2581e-01, -4.5206e-02, 5.5160e-01],
[-4.0049e-02, 1.1659e-01, -1.2883e+00, -7.5520e-01, 2.1785e+00]],
[[-4.0957e-01, -2.3837e-01, -3.6260e-01, -2.0422e-01, 2.1653e+00],
[-2.6621e-01, -1.7249e-03, 1.7166e-01, 2.6876e+00, -4.4573e-01],
[-1.6560e+00, -1.9790e-01, -5.7034e-02, 8.1805e-01, 8.1822e-01]]])
>>> x.permute(2,0,1)
tensor([[[ 3.1125e+00, -9.0231e-02, -4.0049e-02],
[-4.0957e-01, -2.6621e-01, -1.6560e+00]],
[[ 7.0817e-01, -1.4458e+00, 1.1659e-01],
[-2.3837e-01, -1.7249e-03, -1.9790e-01]],
[[ 5.5992e-01, -8.2581e-01, -1.2883e+00],
[-3.6260e-01, 1.7166e-01, -5.7034e-02]],
[[-3.3711e-01, -4.5206e-02, -7.5520e-01],
[-2.0422e-01, 2.6876e+00, 8.1805e-01]],
[[ 2.1945e+00, 5.5160e-01, 2.1785e+00],
[ 2.1653e+00, -4.4573e-01, 8.1822e-01]]])
>>> x.permute(2,0,1).size()
(5, 2, 3)
>>> x.size()
(2, 3, 5)