zoukankan      html  css  js  c++  java
  • torch学习02-tensor学习

    torch学习02-tensor学习

    张量如同数组和矩阵一样, 是一种特殊的数据结构。在PyTorch中, 神经网络的输入、输出以及网络的参数等数据, 都是使用张量来进行描述。

    张量的使用和Numpy中的ndarrays很类似, 区别在于张量可以在GPU或其它专用硬件上运行, 这样可以得到更快的加速效果。如果你对ndarrays很熟悉的话, 张量的使用对你来说就很容易了。如果不太熟悉的话, 希望这篇有关张量API的快速入门教程能够帮到你。

    import torch
    import numpy as np
    

    一、张量初始化/创建

    【对齐paddle】list/numpy 转为tensor可以合并一种; 总共三种; paddle张量的创建

    张量有很多种不同的初始化方法, 先来看看四个简单的例子:

    1. 直接生成张量

    由原始数据直接生成张量, 张量类型由原始数据类型决定。 List[int] 或者 Numpy 都可以

    data = [[1, 2], [3, 4]]
    x_data = torch.tensor(data)
    print(x_data, x_data.shape, x_data.dtype)
    ----------------------------
    tensor([[1, 2],
            [3, 4]]) torch.Size([2, 2]) torch.int64
    

    2. 通过 numpy 数组来生成张量

    由已有的Numpy数组来生成张量(反过来也可以由张量来生成Numpy数组

    np_array = np.array(data)
    x_np_1 = torch.tensor(np_array)
    x_np = torch.from_numpy(np_array)
    print(np_array)
    print(x_np_1)
    print(x_np)
    --------------------------------------
    [[1 2]
     [3 4]]
    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    

    3. 通过已有张量来生成新的张量

    新的张量将继承已有张量的数据属性(结构、类型), 也可以重新指定新的数据类型

    x_ones = torch.ones_like(x_data)   # 保留 x_data 的属性
    print(f"Ones Tensor: 
     {x_ones} 
    ")
    -------------------------------------------------
    Ones Tensor:
     tensor([[1, 1],
             [1, 1]])
    
    
    x_rand = torch.rand_like(x_data, dtype=torch.float)   # 重写 x_data 的数据类型
                                                            int -> float
    print(f"Random Tensor: 
     {x_rand} 
    ")
    ----------------------------------------------------
    Random Tensor:
     tensor([[0.0381, 0.5780],
             [0.3963, 0.0840]])
    

    4.通过指定数据维度来生成张量(特定要求初始化)

    shape是元组类型【具体要求按照torch.rand() 等函数要求,不局限 元组】, 用来描述张量的维数, 下面3个函数通过传入shape来指定生成张量的维数。

    shape = (2,3,)
    rand_tensor = torch.rand(shape)
    ones_tensor = torch.ones(shape)
    zeros_tensor = torch.zeros(shape)
    
    print(f"Random Tensor: 
     {rand_tensor} 
    ")
    print(f"Ones Tensor: 
     {ones_tensor} 
    ")
    print(f"Zeros Tensor: 
     {zeros_tensor}")
    ----------------------------------------------------
    Random Tensor:
     tensor([[0.0266, 0.0553, 0.9843],
             [0.0398, 0.8964, 0.3457]])
    
    Ones Tensor:
     tensor([[1., 1., 1.],
             [1., 1., 1.]])
    
    Zeros Tensor:
     tensor([[0., 0., 0.],
             [0., 0., 0.]])
    

    二、random 相关(也可以看做生成) 10个函数

    二、张量属性

    维数数据类型以及它们所存储的设备(CPU或GPU)

    • 1)维数: tensor.shape
    • 2)数据类型: tensor.dtype
    • 3)数据存储设备: tensor.device
    tensor = torch.rand(3,4)
    
    print(f"Shape of tensor: {tensor.shape}")
    print(f"Datatype of tensor: {tensor.dtype}")
    print(f"Device tensor is stored on: {tensor.device}")
    --------------------------------------------------------
    Shape of tensor: torch.Size([3, 4])   # 维数
    Datatype of tensor: torch.float32     # 数据类型
    Device tensor is stored on: cpu       # 存储设备
    

    三、张量计算/操作:具体可以参考paddle整理:paddle01-Tensor相关操作 【完】

    转置、索引、切片、数学运算、线性代数、随机采样等等:

    # 判断当前环境GPU是否可用, 然后将tensor导入GPU内运行
    if torch.cuda.is_available():
      tensor = tensor.to('cuda')
    

    参考:

    1. ApacheCN 教程: 张量: https://pytorch.apachecn.org/docs/1.7/03.html
  • 相关阅读:
    es6学习笔记--let和const
    最新数组方法(包括es6)
    es6学习笔记--模板字符串
    版本控制工具--svn和git的使用(三) -----git的使用(2)
    版本控制工具--svn和git的使用(三) -----git的使用(1)
    版本控制工具--svn和git的使用(二) -----SVN的操作
    版本控制工具--svn和git的使用(一) -----版本控制的好处以及分类
    e.preventDefault()和e.stopPropagation()以及return false的作用和区别
    Mybatis之SessionFactory原理
    Mybatis架构简介
  • 原文地址:https://www.cnblogs.com/zhangtao-0001/p/15059652.html
Copyright © 2011-2022 走看看