• Pytorch学习笔记14----torch中相关函数使用:view函数、max()函数、squeeze()函数


    1.View函数

    把原先tensor中的数据按照行优先的顺序排成一个一维的数据(这里应该是因为要求地址是连续存储的),然后按照参数组合成其他维度的tensor。比如说是不管你原先的数据是[[[1,2,3],[4,5,6]]]还是[1,2,3,4,5,6],因为它们排成一维向量都是6个元素,所以只要view后面的参数一致,得到的结果都是一样的。
    小案例:
    import torch
    a=torch.Tensor([[[1,2,3],[4,5,6]]])
    b=torch.Tensor([1,2,3,4,5,6])
    
    print(a.view(1,6))
    print(b.view(1,6))
    #得到的结果都是tensor([[1., 2., 3., 4., 5., 6.]])
    
    #再看一个例子:
    a=torch.Tensor([[[1,2,3],[4,5,6]]])
    print(a.view(3,2))

    控制台输出:

    参数不可为空。参数中的-1就代表这个位置由其他位置的数字来推断,只要在不致歧义的情况下,view参数就可以推断出来,也就是人可以推断出形状的情况下,view函数也可以推断出来。比如a tensor的数据个数是6个,如果view(1,-1),我们就可以根据tensor的元素个数推断出-1代表6。而如果是view(-1,-1,2),人不知道怎么推断,机器也不知道。还有一种情况是人可以推断出来,但是机器推断不出来的:view(-1,-1,6),人可以知道-1都代表1,但是机器不允许同时有两个-1。
     
    2.max函数
    import  torch
    a = torch.randn(3,3)
    print(a)#返回生成的随机tensor(3*3)
    print(torch.max(a,0))#返回每一列中最大值的那个元素,且返回索引(返回最大元素在这一列的行索引)
    print(torch.max(a,1))#返回每一行中最大值的那个元素,且返回其索引(返回最大元素在这一行的列索引)
    print(torch.max(a))#返回tensor a 中的最大值
    print(torch.max(a,0)[0] )# 只返回最大值的每个数
    print(torch.max(a,0)[1])#只返回最大值的每个索引

    控制台输出:

    D:softwaretoolsanacondapython.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
    tensor([[ 1.6294,  0.2568,  0.2093],
            [ 0.4974, -2.1175,  0.1659],
            [ 0.0640,  1.4387, -0.5895]])
    torch.return_types.max(
    values=tensor([1.6294, 1.4387, 0.2093]),
    indices=tensor([0, 2, 0]))
    torch.return_types.max(
    values=tensor([1.6294, 0.4974, 1.4387]),
    indices=tensor([0, 0, 1]))
    tensor(1.6294)
    tensor([1.6294, 1.4387, 0.2093])
    tensor([0, 2, 0])
    
    Process finished with exit code 0

    max函数用法总结:

    torch.max(a) 返回输入tensor a中所有元素的最大值

    torch.max(a,0) 返回每一列中最大值的那个元素,且返回索引(返回最大元素在这一列的行索引)

    torch.max(a,1) 返回每一行中最大值的那个元素,且返回其索引(返回最大元素在这一行的列索引)

    torch.max()[0], 只返回最大值的每个数

    troch.max()[1], 只返回最大值的每个索引

    torch.max()[1].data 只返回variable中的数据部分(去掉Variable containing:)

    torch.max()[1].data.numpy() 把数据转化成numpy ndarry

    torch.max()[1].data.numpy().squeeze() 把数据条目中维度为1 的删除掉

    torch.max(tensor1,tensor2) element-wise 比较tensor1 和tensor2 中的元素,返回较大的那个值

    3.squeeze()函数
    torch.unsqueeze(input, dim, out=None)

    作用:扩展维度

    返回一个新的张量,对输入的既定位置插入维度 1

    如果dim为负,则将会被转化dim+input.dim()+1

    参数:

    tensor (Tensor) – 输入张量

    dim (int) – 插入维度的索引

    out (Tensor, optional) – 结果张量

    import torch
    
    x = torch.Tensor([1, 2, 3, 4])  # torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称。
    
    print('-' * 50)
    print(x)  # tensor([1., 2., 3., 4.])
    print(x.size())  # torch.Size([4])
    print(x.dim())  # 1
    print(x.numpy())  # [1. 2. 3. 4.]
    
    print('-' * 50)
    print(torch.unsqueeze(x, 0))  # tensor([[1., 2., 3., 4.]])
    print(torch.unsqueeze(x, 0).size())  # torch.Size([1, 4])
    print(torch.unsqueeze(x, 0).dim())  # 2
    print(torch.unsqueeze(x, 0).numpy())  # [[1. 2. 3. 4.]]
    
    print('-' * 50)
    print(torch.unsqueeze(x, 1))
    # tensor([[1.],
    #         [2.],
    #         [3.],
    #         [4.]])
    print(torch.unsqueeze(x, 1).size())  # torch.Size([4, 1])
    print(torch.unsqueeze(x, 1).dim())  # 2
    
    print('-' * 50)
    print(torch.unsqueeze(x, -1))
    # tensor([[1.],
    #         [2.],
    #         [3.],
    #         [4.]])
    print(torch.unsqueeze(x, -1).size())  # torch.Size([4, 1])
    print(torch.unsqueeze(x, -1).dim())  # 2
    
    print('-' * 50)
    print(torch.unsqueeze(x, -2))  # tensor([[1., 2., 3., 4.]])
    print(torch.unsqueeze(x, -2).size())  # torch.Size([1, 4])
    print(torch.unsqueeze(x, -2).dim())  # 2

    控制台输出:

    D:softwaretoolsanacondapython.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
    --------------------------------------------------
    tensor([1., 2., 3., 4.])
    torch.Size([4])
    1
    [1. 2. 3. 4.]
    --------------------------------------------------
    tensor([[1., 2., 3., 4.]])
    torch.Size([1, 4])
    2
    [[1. 2. 3. 4.]]
    --------------------------------------------------
    tensor([[1.],
            [2.],
            [3.],
            [4.]])
    torch.Size([4, 1])
    2
    --------------------------------------------------
    tensor([[1.],
            [2.],
            [3.],
            [4.]])
    torch.Size([4, 1])
    2
    --------------------------------------------------
    tensor([[1., 2., 3., 4.]])
    torch.Size([1, 4])
    2
    
    Process finished with exit code 0

    4.torch.squeeze 详解

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

    作用:降维

    将输入张量形状中的1 去除并返回。 如果输入是形如(A×1×B×1×C×1×D),那么输出形状就为: (A×B×C×D)

    当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B), squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)。

    参数:
    input (Tensor) – 输入张量
    dim (int, optional) – 如果给定,则input只会在给定维度挤压
    out (Tensor, optional) – 输出张量

    案例:

    import torch
    print("*" * 50)
    
    m = torch.zeros(2, 1, 2, 1, 2)
    print(m.size())  # torch.Size([2, 1, 2, 1, 2])
    
    n = torch.squeeze(m)
    print(n.size())  # torch.Size([2, 2, 2])
    
    n = torch.squeeze(m, 0)  # 当给定dim时,那么挤压操作只在给定维度上
    print(n.size())  # torch.Size([2, 1, 2, 1, 2])
    
    n = torch.squeeze(m, 1)
    print(n.size())  # torch.Size([2, 2, 1, 2])
    
    n = torch.squeeze(m, 2)
    print(n.size())  # torch.Size([2, 1, 2, 1, 2])
    
    n = torch.squeeze(m, 3)
    print(n.size())  # torch.Size([2, 1, 2, 2])

    控制台输出:

    5.cat()函数
    torch.cat(inputs, dimension=0) → Tensor

    cat是concatnate的意思:拼接,联系在一起。

    参数:

    • inputs (sequence of Tensors) – 可以是任意相同Tensor 类型的python 序列
    • dimension (intoptional) – 沿着此维连接张量序列

    注意:输入数据必须是序列,序列中数据是任意相同的shape的同类型tensor

    按维数0拼接(竖着拼)

    C = torch.cat( (A,B),0 )

    按维数1拼接(横着拼)

    C = torch.cat( (A,B),1 )

    案例:

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

    控制台输出:

    D:softwaretoolsanacondapython.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
    A:
     tensor([[1., 1., 1.],
            [1., 1., 1.]]) 
    A.shape:
     torch.Size([2, 3]) 
    
    B:
     tensor([[2., 2., 2.],
            [2., 2., 2.],
            [2., 2., 2.],
            [2., 2., 2.]]) 
    B.shape:
     torch.Size([4, 3]) 
    
    C:
     tensor([[1., 1., 1.],
            [1., 1., 1.],
            [2., 2., 2.],
            [2., 2., 2.],
            [2., 2., 2.],
            [2., 2., 2.]]) 
    C.shape:
     torch.Size([6, 3]) 
    
    
    Process finished with exit code 0

    6.permute()函数

    torch.Tensor.permute (Python method, in torch.Tensor)

    作用:将tensor的维度换位。

    permute是更灵活的transpose,可以灵活的对原数据的维度进行调换,而数据本身不变。

    import torch
    x = torch.randn(2,3,4)
    print(x.size())
    x_p = x.permute(1,0,2) # 将原来第1维变为0维,同理,0→1,2→2 print(x_p.size())
    print(x_p.size())

    控制台输出:

    参考文献:
    https://www.jianshu.com/p/b23367ec9097
    https://blog.csdn.net/Jane_JXR/article/details/98341092
    https://zhuanlan.zhihu.com/p/86763381
    https://www.pytorchtutorial.com/docs/  (pytorch 中文官方文档)
     
     

     

  • 相关阅读:
    Mybatis 框架下 SQL 注入攻击的方式
    Vue 环境准备
    HTTP.sys漏洞的检测和修复(附补丁包下载)
    BPM工作流中的一些业务场景
    关系型数据库
    .NET中使用Redis总结——2.项目实战
    Java 开源项目整合
    在IIS 搭建FTP站点
    悲观锁和乐观锁详解
    C# 通过一个控制台打开另一个控制台
  • 原文地址:https://www.cnblogs.com/luckyplj/p/13461922.html
Copyright © 2020-2023  润新知