pytorch一些常用的操作 总结

https://blog.csdn.net/TH_NUM/article/details/83088915

1、tensor的拼接

#coding=utf-8
import torch
#张量的拼接
x=torch.randn(2,3)
print(x)
x1=torch.cat((x,x,x),0)  #得到tensor大小:(6, 3)
print("x1",x1,"x1.size()",x1.size())
x2=torch.cat((x,x,x),1)  #得到tensor大小:(2, 9)
print("x2",x2,"x2.size()",x2.size())

 不同方式的拼接:

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)
print(d)
print(e)

输出结果:
tensor([[[ 1,  2,  3],
         [11, 22, 33]],

        [[ 4,  5,  6],
         [44, 55, 66]]], dtype=torch.int32)
tensor([[[ 1,  2,  3],
         [ 4,  5,  6]],

        [[11, 22, 33],
         [44, 55, 66]]], dtype=torch.int32)
tensor([[[ 1,  4],
         [ 2,  5],
         [ 3,  6]],

        [[11, 44],
         [22, 55],
         [33, 66]]], dtype=torch.int32)

稍作总结:

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[0][1]],[a[1][2],b[1][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[0][1]],[a[1][2],b[1][2]]]]
(来自博客:https://blog.csdn.net/TH_NUM/article/details/83088915  )

2、压缩张量

如果输入张量的形状为(A×1×B×C×1×D)(A×1×B×C×1×D),那么输出张量的形状为(A×B×C×D)(A×B×C×D)

当通过dim参数指定维度时,维度压缩操作只会在指定的维度上进行。如果输入向量的形状为(A×1×B)(A×1×B),squeeze(input, 0)会保持张量的维度不变,只有在执行squeeze(input, 1)时,输入张量的形状会被压缩至(A×B)(A×B)

输出的张量与原张量共享内存,如果改变其中的一个,另一个也会改变。

x = torch.zeros(2, 1, 2, 1, 2)
print("x.size()", x.size()) #输出Tensor的大小
y = torch.squeeze(x)#torch.squeeze(),压缩维度,去掉维度为1的张量
print("y.size()",y.size()) 
y = torch.squeeze(x, 0)#对指定维度中为1的张量进行压缩,第一维中没有为1的,所以对原来的没有影响
print("y.size()",y.size())
y = torch.squeeze(x, 1)#对第二个维度进行操作,第二个维度中有为1的,即将其去掉
print("y.size()",y.size())

结果:
('x.size()', (2, 1, 2, 1, 2))
('y.size()', (2, 2, 2))
('y.size()', (2, 1, 2, 1, 2))
('y.size()', (2, 2, 1, 2))

3、重复张量

torch.Tensor.repeat(*sizes)

沿着制定的维度重复张量,相当于 复制操作,,与expand()不同

x = torch.Tensor([1, 2, 3])
t=x.repeat(4, 2)#行重复4次,列重复2次,本来是一行三列,现在就是(1*4,3*2)=(4,6)
print(t)
print("x.size()",x.size())
print("t.size()",t.size())

结果:
tensor([[1., 2., 3., 1., 2., 3.],
        [1., 2., 3., 1., 2., 3.],
        [1., 2., 3., 1., 2., 3.],
        [1., 2., 3., 1., 2., 3.]])
('x.size()', (3,))
('t.size()', (4, 6))

第二种情况:

torch.Tensor.unfolder(dim,size,step)

返回一个Tensor,其中元素复制原有张量在dim 维度上的数据,复制重复size次,复制从下标为step值的时候开始

  • dim (int) - 目标维度
  • size (int) - 复制重复的次数(展开维度)
  • step (int) - 步长
x=torch.arange(1,8)
print("x",x,"x.size()",x.size())
t=x.unfold(0,2,1) #第0维,复制2次,从下标为2的开始
print("t",t,"t.size()",t.size())
t1=x.unfold(0,2,2) #其中步长为2
print("t1",t1,"t1.size()",t1.size())

结果如下:
('x', tensor([1, 2, 3, 4, 5, 6, 7]), 'x.size()', (7,))
('t', tensor([[1, 2],
        [2, 3],
        [3, 4],
        [4, 5],
        [5, 6],
        [6, 7]]), 't.size()', (6, 2))
('t1', tensor([[1, 2],
        [3, 4],
        [5, 6]]), 't1.size()', (3, 2))

4、缩小张量

torch.Tensor.narrow(dimension,start,length) 

返回一个经过缩小后的张量。 操作的维度由dimension制定,缩小的范围是从start开始到start+length

并且返回的张量与原张量共享底层的内存

  • dimension (int) – 要进行缩小的维度
  • start (int) – 开始维度索引
  • length (int) – 缩小持续的长度
x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
t=x.narrow(0, 0, 2)#从0开始,长度为2
print(x,"x.size()",x.size())
print (t,"t.size()",t.size())
t1=x.narrow(1, 1, 2)
print (t1,"t1.size()",t1.size())

结果:
(tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]]), 'x.size()', (3, 3))
(tensor([[1., 2., 3.],
        [4., 5., 6.]]), 't.size()', (2, 3))
(tensor([[2., 3.],
        [5., 6.],
        [8., 9.]]), 't1.size()', (3, 2))

 5 张量变形

torch.Tensor.view(*args)

返回一个有相同的数据但是形状不同的新的张量

注意:返回的张量具有和原张量相同的数据和相同的元素个数,但是形状不同.

x=torch.randn(4,4)
print ("x.size()",x.size())
y = x.view(16)
print("y",y)
print("y.size()",y.size())

结果:
('x.size()', (4, 4))
('y', tensor([ 1.2186,  0.0345,  0.6431, -1.8446,  0.4304,  1.3142, -0.0091, -0.6192,
        -1.0749,  0.1124,  1.9917,  0.6180,  0.6542, -0.2901, -0.6967, -0.3726]))
('y.size()', (16,))

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值