numpy和torch.tensor的张量操作方法是什么

其他教程   发布日期:2023年07月10日   浏览次数:481

今天小编给大家分享一下numpy和torch.tensor的张量操作方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

    1. 张量的拼接

    (1) numpy.concatenate

    1. np.concatenate((a1,a2,a3,…), axis=0)

    张量的拼接要用np.concatenate这个方法的,其中 a1,a2,a3,…是拼接的子张量,axis是维数,axis=0表示按照第一维进行拼接。

    例如将两个二维的张量按照第一维拼接成一个二维的张量:

    1. import numpy as np
    2. a=np.array([[1,2,3]])
    3. b=np.array([[4,5,6]])
    4. c=np.concatenate((a,b),axis=0)
    5. print(c)
    6. d=np.concatenate((c,a),axis=0)
    7. print(d)
    8. e=np.concatenate((c,c),axis=1)
    9. print(e)

    结果

    array([[1, 2, 3],
    [4, 5, 6]])
    array([[1, 2, 3],
    [4, 5, 6],
    [1, 2, 3]])
    array([[1, 2, 3, 1, 2, 3],
    [4, 5, 6, 4, 5, 6]])

    对于axis选择的更简单直接的理解是我们可以从将被拼接的两个矩阵的形状上来看,比如

    a.shape=(3,1,2), b.shape=(6,1,2),则我们对其进行拼接的话目的是让拼接之后的shape=(9,1,2),那么我们就选择axis=0,即代表对第0维的进行相加。

    代码如下:

    1. import numpy as np
    2. a = np.zeros((3, 1, 2))
    3. b = np.zeros((6, 1, 2))
    4. c = np.concatenate((a, b), axis=0)
    5. print(c.shape)

    结果为:

    (9, 1, 2)

    (2) torch.cat

    这里的拼接和上面介绍的numpy的拼接功能是一样的

    1. C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)
    2. C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)

    例:

    1. import torch
    2. A=torch.ones(2,3) #2x3的张量(矩阵)
    3. B=2*torch.ones(4,3) #4x3的张量(矩阵)
    4. C=torch.cat((A,B),0) #按维数0(行)拼接
    5. print(C)

    结果:

    tensor([[ 2., 2., 2.],
    [ 2., 2., 2.],
    [ 2., 2., 2.],
    [ 2., 2., 2.]])

    接着上面

    1. D=2*torch.ones(2,4) #2x4的张量(矩阵)
    2. C=torch.cat((A,D),1)#按维数1(列)拼接
    3. print(C)

    结果:

    tensor([[ 1., 1., 1., 2., 2., 2., 2.],
    [ 1., 1., 1., 2., 2., 2., 2.]])

    2. 张量的重构

    (1) np.reshape

    1. >>> import numpy as np
    2. >>> a = np.array([[1,2,3],[4,5,6]])
    3. >>> a
    4. array([[1, 2, 3],
    5. [4, 5, 6]])
    6. >>> b = np.reshape(a, (2,3,1))
    7. >>> b
    8. array([[[1],
    9. [2],
    10. [3]],
    11. [[4],
    12. [5],
    13. [6]]])
    14. >>> b.shape
    15. (2, 3, 1)

    (2) array.shape

    1. >>> import numpy as np
    2. >>> a = np.array([1,2,3,4,5,6,7,8])
    3. >>> a.shape = (2, 4)
    4. >>> a
    5. array([[1, 2, 3, 4],
    6. [5, 6, 7, 8]])

    (3) torch.view

    在pytorch中view函数的作用为重构张量的维度,相当于numpy中resize()的功能,但是用法可能不太一样。

    1.torch.view(参数a,参数b,…)

    例如:

    1. import torch
    2. tt1=torch.tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])
    3. result=tt1.view(3,2)
    4. print(result)

    结果

    tensor([[-0.3623, -0.6115],
    [ 0.7283, 0.4699],
    [ 2.3261, 0.1599]])

    在上面例子中参数a=3和参数b=2决定了将一维的tt1重构成3x2维的张量。

    2.有的时候会出现torch.view(-1)或者torch.view(参数a,-1)这种情况。

    例:

    1. import torch
    2. tt2=torch.tensor([[-0.3623, -0.6115],
    3. [ 0.7283, 0.4699],
    4. [ 2.3261, 0.1599]])
    5. result=tt2.view(-1)
    6. print(result)

    结果:

    tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])

    由上面的案例可以看到,如果是torch.view(-1),则原张量会变成一维的结构。

    例:

    1. import torch
    2. tt3=torch.tensor([[-0.3623, -0.6115],
    3. [ 0.7283, 0.4699],
    4. [ 2.3261, 0.1599]])
    5. >>> result=tt3.view(2,-1)

    结果:

    tensor([[-0.3623, -0.6115, 0.7283],
    [ 0.4699, 2.3261, 0.1599]])

    由上面的案例可以看到,如果是torch.view(参数a,-1),则表示在参数b未知,参数a已知的情况下自动补齐列向量长度,在这个例子中a=2,tt3总共由6个元素,则b=6/2=3。

    例:

    1. import torch
    2. inputs = torch.randn(1,3)
    3. print(inputs)
    4. print(inputs.view(1, 1, -1))

    结果:

    tensor([[-0.5525, 0.6355, -0.3968]])
    tensor([[[-0.5525, 0.6355, -0.3968]]])

    将二维变为三维,a=1,b=1,c=3/(1*1)

    3. 张量的形状

    (1) torch.size

    1. import torch
    2. inputs = torch.randn(1,3)
    3. print(inputs.size())

    结果:

    torch.Size([1, 3])

    4. 张量的扩展

    (1) torch.tensor扩展方法

    用unsqueeze方法将原张量进行维度扩张,unsqueeze后面括号里的数字代表在哪个维度扩张

    1. import torch
    2. a = torch.tensor([[1, 2, 3], [4, 5, 6]])
    3. b = torch.tensor([[7, 8, 9], [4, 5, 6]])
    4. print(a)
    5. print(b)
    6. a = a.unsqueeze(0)
    7. b = b.unsqueeze(0)
    8. print(a)
    9. print(b)
    10. c = torch.cat((a, b), 0)
    11. print(c)
    12. print(c.shape)

    结果为

    tensor([[1, 2, 3],
    [4, 5, 6]])
    tensor([[7, 8, 9],
    [4, 5, 6]])
    tensor([[[1, 2, 3],
    [4, 5, 6]]])
    tensor([[[7, 8, 9],
    [4, 5, 6]]])
    tensor([[[1, 2, 3],
    [4, 5, 6]],

    [[7, 8, 9],
    [4, 5, 6]]])
    torch.Size([2, 2, 3])

    用squeeze方法将原张量进行维度缩减,squeeze后面括号里的数字代表在哪个维度缩减

    1. import torch
    2. a = torch.tensor([[1, 2, 3], [4, 5, 6]])
    3. b = torch.tensor([[7, 8, 9], [4, 5, 6]])
    4. print(a)
    5. print(b)
    6. a = a.unsqueeze(0)
    7. b = b.unsqueeze(0)
    8. print(a)
    9. print(b)
    10. a = a.squeeze(0)
    11. b = b.squeeze(0)
    12. print(a)
    13. print(b)

    结果为

    tensor([[1, 2, 3],
    [4, 5, 6]])
    tensor([[7, 8, 9],
    [4, 5, 6]])
    tensor([[[1, 2, 3],
    [4, 5, 6]]])
    tensor([[[7, 8, 9],
    [4, 5, 6]]])
    tensor([[1, 2, 3],
    [4, 5, 6]])
    tensor([[7, 8, 9],
    [4, 5, 6]])

    (2) np.array扩展方法

    np.expand_dims:用于扩展数组的形状

    原始数组:

    1. import numpy as np
    2. In [12]:
    3. a = np.array([[[1,2,3],[4,5,6]]])
    4. a.shape
    5. Out[12]:
    6. (1, 2, 3)

    np.expand_dims(a, axis=0)表示在0位置添加数据,转换结果如下:

    In [13]:
    b = np.expand_dims(a, axis=0)
    b
    Out[13]:
    array([[[[1, 2, 3],
    [4, 5, 6]]]])

    In [14]:
    b.shape
    Out[14]:
    (1, 1, 2, 3)

    以上就是numpy和torch.tensor的张量操作方法是什么的详细内容,更多关于numpy和torch.tensor的张量操作方法是什么的资料请关注九品源码其它相关文章!