张量操作

一、张量的操作: 拼接、切分、索引和变换

一、张量的拼接与切分

1、torch.cat()

1、torch.cat()

功能: 将张量按维度dim进行拼接

torch.cat(tensors, dim=0, out=None)

tensors: 张量数据
dim: 需要拼接维度

主要有两种拼接方式:按行拼接和按列拼接,也就是dim=0和dim=1

e1 = torch.cat((A1,A2),0), 拼接完的结果是A1上,A2下,类似与python dataframe append功能

e2 = torch.cat((A1,A2),1), 拼接完的结果是A1左,A2右,类似[A1, A2]

2、torch.stack()

1、torch.stack()

功能: 在新创建的维度dim上进行拼接

torch.stack(tensors, dim=0, out=None)


example: 相当于将两个矩阵,再一个新的维度上进行链接,原始的数据维度增加一维(如 二维变成三维)


T1.shape = torch.Size([3, 3])
T2.shape = torch.Size([3, 3])

torch.stack((T1,T2),dim=0).shape = torch.Size([2, 3, 3])
torch.stack((T1,T2),dim=1).shape = torch.Size([3, 2, 3])
torch.stack((T1,T2),dim=2).shape = torch.Size([3, 3, 2])

3、torch.chunk()

torch.chunk(input, chunks, dim=0)


功能: 将张量按维度dim进行平均切分

返回值: 张量列表

input: 要切分的张量
chunks: 要切分的份数
dim: 要切分的维度

example:

>>> a = torch.Tensor([[1,2,4]])
>>> b = torch.Tensor([[4,5,7], [3,9,8], [9,6,7]])
>>> c = torch.cat((a,b), dim=0)
>>> c
tensor([[1., 2., 4.],
        [4., 5., 7.],
        [3., 9., 8.],
        [9., 6., 7.]])
>>> d = torch.chunk(c,4,dim=0)
>>> d
(tensor([[1., 2., 4.]]), tensor([[4., 5., 7.]]), tensor([[3., 9., 8.]]), tensor([[9., 6., 7.]]))

4、torch.split()

torch.split(tensor, split_size_or_sections, dim=0)

功能: 将张量按维度dim进行切分

返回值: 张量列表

tensor: 要切分的张量
split_size_or_sections: 为int时,表示每一份的长度;为list时,按list元素切分
dim: 要切分的维度

example:

>>> section = [1,2,1,2,2]
>>> d = torch.randn(8,4)
>>> d
tensor([[ 0.1100,  1.8889,  1.1625,  0.7474],
        [-0.8003, -0.7463, -1.7287,  0.2690],
        [-0.0310, -0.7457,  0.9924,  0.9361],
        [ 0.5262, -2.3752, -0.0715,  0.8350],
        [ 0.8388, -0.3284,  1.6460,  0.9162],
        [-0.1060,  0.4595,  1.2993,  1.0798],
        [ 0.1333, -0.3867,  0.8843, -1.6599],
        [ 0.7458,  1.8324,  0.7660,  0.3449]])
>>> torch.split(d, section, dim=0)
(tensor([[0.1100, 1.8889, 1.1625, 0.7474]]), tensor([[-0.8003, -0.7463, -1.7287,  0.2690],
        [-0.0310, -0.7457,  0.9924,  0.9361]]), tensor([[ 0.5262, -2.3752, -0.0715,  0.8350]]), tensor([[ 0.8388, -0.3284,  1.6460,  0.9162],
        [-0.1060,  0.4595,  1.2993,  1.0798]]), tensor([[ 0.1333, -0.3867,  0.8843, -1.6599],
        [ 0.7458,  1.8324,  0.7660,  0.3449]]))

二、张量索引

1、torch.index_select()

torch.index_select(input, dim, index, out=None)

功能: 在维度dim上,按index索引数据
返回值: 根据index索引数据拼接的张量

input: 要索引的张量
dim: 要索引的维度
index: 要索引数据的序号


>>> x = torch.rand(3,5)
>>> x
tensor([[0.2641, 0.4911, 0.9260, 0.9348, 0.5120],
        [0.2791, 0.6143, 0.4737, 0.4615, 0.2795],
        [0.8597, 0.4310, 0.2033, 0.3675, 0.5774]])
>>> index = torch.LongTensor([2,0])
>>> index
tensor([2, 0])

# dim=0表示行,取对应index的行数
>>> torch.index_select(x, dim=0, index=index)
tensor([[0.8597, 0.4310, 0.2033, 0.3675, 0.5774],
        [0.2641, 0.4911, 0.9260, 0.9348, 0.5120]])

# dim=1表示列,取对应index的列数
>>> torch.index_select(x, dim=1, index=index)
tensor([[0.9260, 0.2641],
        [0.4737, 0.2791],
        [0.2033, 0.8597]])

2、torch.masked_select()

torch.masked_select(input, mask, out=None)

功能: 按mask中的True进行索引
返回值: 一维张量

input: 要索引的张量
mask: 与input同形状的布尔类型张量

example:

>>> a =torch.Tensor([1,2,4,4,5])
>>> print(torch.masked_select(a, a<3))
tensor([1., 2.])

三、张量变换

1、torch.reshape()

torch.reshape(input, shape)

功能: 变换张量形状

input: 要变换的张量
shape: 新张量的形状

注: 张量在内存中是连续时,新张量与input共享数据内存

example:

>>> a = torch.randn(3,4)
>>> a
tensor([[-2.2423,  2.0383, -0.8332, -0.7399],
        [ 1.8236,  0.1848, -0.5453, -1.6985],
        [ 0.0915, -1.4923, -0.6636,  0.8819]])
>>> torch.reshape(a,(4,3))
tensor([[-2.2423,  2.0383, -0.8332],
        [-0.7399,  1.8236,  0.1848],
        [-0.5453, -1.6985,  0.0915],
        [-1.4923, -0.6636,  0.8819]])

2、torch.transpose()

torch.transpose(input, dim0, dim1)

功能: 交换一个tensor的两个维度

dim0、dim1: 要交换的维度


example: 


>>> cc = torch.rand((2,3,4))
>>> cc
tensor([[[0.2153, 0.2895, 0.0300, 0.0705],
         [0.3040, 0.0302, 0.5433, 0.3061],
         [0.3409, 0.1692, 0.4029, 0.0016]],

        [[0.9374, 0.5397, 0.1523, 0.2801],
         [0.4294, 0.1527, 0.7562, 0.4555],
         [0.2880, 0.1197, 0.4542, 0.3580]]])
>>> dd = torch.transpose(cc,1,2)
>>> dd
tensor([[[0.2153, 0.3040, 0.3409],
         [0.2895, 0.0302, 0.1692],
         [0.0300, 0.5433, 0.4029],
         [0.0705, 0.3061, 0.0016]],

        [[0.9374, 0.4294, 0.2880],
         [0.5397, 0.1527, 0.1197],
         [0.1523, 0.7562, 0.4542],
         [0.2801, 0.4555, 0.3580]]])
>>> dd.shape
torch.Size([2, 4, 3])
>>> cc.shape
torch.Size([2, 3, 4])

3、torch.t()

torch.t(input)

功能: 2维张量转置,对矩阵而言,等价于torch.transpose(input, 0, 1)

example:

>>> a = torch.randn(1,2)
>>> a.t()
tensor([[1.9376],
        [1.0067]])
>>> a
tensor([[1.9376, 1.0067]])

4、torch.seueeze()

torch.squeeze(input, dim=None, out=None)

功能: 压缩长度为1的维度

dim: 若为None,移除所有长度为1的维度,若指定维度,当且仅当该长度为1时,可以被移除


>>> a = torch.randn(1,10)
>>> a.shape
torch.Size([1, 10])
>>> a1 = torch.unsqueeze(a,1)
>>> a1
tensor([[[-1.0263, -0.2194,  1.3285, -1.0941,  0.3763,  0.2249, -1.1332,
           0.2836, -0.4400, -0.3642]]])

>>> a2 = torch.squeeze(a1)
>>> a2
tensor([-1.0263, -0.2194,  1.3285, -1.0941,  0.3763,  0.2249, -1.1332,  0.2836,
        -0.4400, -0.3642])

>>> a2.shape
torch.Size([10])

5、torch.unsqueeze()

torch.unsqueeze(input, dim, out=None)


功能: 依据dim扩展维度

dim: 要扩展的维度

 

二、张量的数学相关运算

一、加减乘除

张量的加减乘除函数如下所示:

1、torch.add() # 相加

2、torch.addcdiv() # 相除

3、torch.addcmul() # 逐个元素相乘,并对结果乘以标量值

4、torch.sub() # 相减

5、torch.div() # 相除

6、torch.mul() # 相乘
   

二、对数、指数、幂函数

1、torch.log(input, out=None) 

2、torch.log10(input, out=None) 

3、torch.log2(input, out=None) 

4、torch.exp(input, out=None) 

5、torch.pow()

三、三角函数

1、torch.abs(input, out=None) 

2、torch.acos(input, out=None) 

3、torch.cosh(input, out=None) 

4、torch.cos(input, out=None) 

5、torch.asin(input, out=None) 

6、torch.atan(input, out=None) 

7、torch.atan2(input, other, out=None)

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值