Day-13 Pytorch Tensors

  • 程序语言会有一些常见的资料组单位,例如 python 会有 list,C、C++ 有 array 等等,Pytorch 也有一个自己的资料组单位,那就是 Tensor
  • 机器学习/深度学习领域鲜少只有单笔资料,因此 Tensor 是一个一维~多维度的运算基本单位,下面会详细介绍 Tensor 的基础使用和注意事项

What is Tensor

  • Tensor 中文翻译叫做张量,是一个可用来表示在一些向量、纯量和其他张量之间的线性关系的多线性函数
  • 可以想像成一个高维度向量,也是深度学习里进行运算的基本元素。

Pytorch and Tensor

  • Pytorch 的基本元素都是基於 Tensor 为基础元素

    Tensor 与 Pytorch 的关联就类似於 Array 与 Python,就是一个元素,不用想太多

Usage

Create Tensor

  • 在开始运用 Tensor 之前,当然要学会创建一个 Tensor 来使用,所以一开始我们先来看看怎麽创建一个 Tensor

  • 创建空的 tensor torch.empty()

    x = torch.empty(2)
    print(x)
    # tensor([5.2430e+33, 5.4511e-43])
    
    • 也可以创建多维
    x = torch.empty(2, 3)
    print(x)
    # tensor([[0., 0., 0.],
    #         [0., 0., 0.]])
    
  • 创建 0 tensor torch.zeros()

    x = torch.zeros(2, 3)
    print(x)
    # tensor([[0., 0., 0.],
    #         [0., 0., 0.]])
    
  • 创建 random tensor torch.rand()

    x = torch.rand(2, 3)
    print(x)
    # tensor([[0.6430, 0.0116, 0.1679],
    #         [0.8597, 0.1615, 0.4508]])
    
  • emptyzeros 的差别在 zeros 是创建零元素,empty 则是单纯宣告记忆体区块,并没有初始化参数,因此里面的参数可以是任何值

  • 创建 1 tensor torch.ones()

    x = torch.ones(2, 3)
    print(x)
    # tensor([[1., 1., 1.],
    #         [1., 1., 1.]])
    
  • 资料型态 torch.dtype

    • 检查资料型态
      x = torch.ones(2, 3)
      print(x.dtype)
      # torch.float32
      
      • 所以可以知道预设 dtype 是 torch.float32
    • 也可以赋予资料型态
      x = torch.ones(2, 3, dtype=torch.int)
      print(x.dtype)
      # torch.int
      
  • 资料大小 torch.size()

    x = torch.ones(2, 3)
    print(x.size())
    # torch.Size([2, 3])
    
  • 也可以直接赋值

    x = torch.tensor([2, 3])
    print(x)
    # tensor([2, 3])
    print(x.size())
    # torch.Size([2])
    

Basic + - * /

  • 那知道怎麽创建基本的 Tensor 之後,我们来聊聊如何做 Tensor 的加减乘除吧~
  • 两个 Tensor 相加
    • ans = x1 + x2
    • ans = torch.add(x1, x2)
    • x1 加到 x2
      • x2.add_(x1)
  • 两个 Tensor 相减
    • ans = x1 - x2
    • ans = torch.sub(x1, x2)
  • 两个 Tensor 相乘
    • ans = x1 * x2
    • ans = torch.mul(x1, x2)
  • 两个 Tensor 相除
    • ans = x1 / x2
    • ans = torch.div(x1, x2)

Tensor items

  • 一般程序语言中,我们操作 array 或是 list 的时候往往也会选取部分资料,那如何取得 Tensor 之中的 items 呢?下面我们会基於
    x = torch.rand(5, 3)
    print(x)
    # tensor([[0.8033, 0.7967, 0.0952],
    #         [0.0960, 0.2553, 0.9135],
    #         [0.5835, 0.1997, 0.4466],
    #         [0.7153, 0.9609, 0.7458],
    #         [0.1914, 0.5431, 0.2532]])
    
    来介绍各种资料的取法,那由於中文的那个 row column 的行列问题,我们这边就直接用英文表示
  • 取得第一 row 的 Tensor
    print(x[0, :])
    # tensor([0.8033, 0.7967, 0.0952])
    
  • 取得第一 column 的 Tensor
    print(x[:, 0])
    # tensor([0.8033, 0.0960, 0.5835, 0.7153, 0.1914])
    
  • 取得第一 row, 取得第一 column 的 Tensor
    print(x[0, 0])
    # tensor(0.8033)
    
  • 取得第一 row, 取得第一 column 的 item
    print(x[0, 0].item())
    # 0.8032872080802917
    
  • Reshape Tensor
    y = x.view(15)
    print(y)
    # tensor([0.8033, 0.7967, 0.0952, 0.0960, 0.2553, 0.9135, 0.5835, 0.1997, 0.4466,
    #         0.7153, 0.9609, 0.7458, 0.1914, 0.5431, 0.2532])
    
    • 自动分配
      y = x.view(-1, 5)
      print(y.size())
      # torch.Size([3, 5])
      

Tensor <-> Numpy

  • Numpy 是 Python 数学操作的过程中常见的套件,有精准的数学工具和方便的运算函式,因此很多资料的前处理也往往会用到,所以如何把 numpy array 的资料和 Tensor 做灵活的转换就非常重要了
  • Change Tensor to Numpy array
    a = torch.ones(5)
    print(a)
    b = a.numpy()
    print(b)
    # tensor([1., 1., 1., 1., 1.])
    # [1. 1. 1. 1. 1.]
    
  • Change Numpy array to Tensor
    a = np.ones(5)
    print(a)
    b = torch.from_numpy(a)
    print(b)
    # [1. 1. 1. 1. 1.]
    # tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
    
  • ==如果你是使用 CPU 来操作 Tensor,在这里你的 Tensor a 跟 Numpy b 会共享同一个 memory address ,所以操作会被同步,要特别注意==
    a.add_(1)
    print(a)
    print(b)
    # tensor([2., 2., 2., 2., 2.])
    # [2. 2. 2. 2. 2.]
    
    a += 1
    print(a)
    print(b)
    # [2. 2. 2. 2. 2.]
    # tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
    

使用 GPU

  • 我们有说过 Pytorch 有准备好 GPU 的操作套件,因此可以非常简单的利用宣告来起用 GPU 来达到加速运算的功能
  • 利用 torch.cuda.is_available() 来检查是否可以使用 GPU cuda
    if torch.cuda.is_available():
        device = torch.device("cuda")
        x = torch.ones(5, device=device)
        y = torch.ones(5)
        y = y.to(device)
        z = x + y
        print(z)
    
  • 在这里特别提醒,numpy 只能在 CPU 上面坐使用,因此如果需要转回 numpy ,需要先把 Tensor 送回 CPU
    z = z.to("cpu")
    z = z.numpy()
    

每日小结

  • Tensor 听起来像是一个全新的东西,但是其实真的不用想得太复杂,就是一个基础的资料组单位,因此只要记住 Pytorch 的基础运算单位是 Tensor 就没问题了
  • Tensor 的操作有一些记忆体上的使用问题,需要被特别注意,其他就是当作一般程序撰写的方式去做操作即可
  • 明天我们来聊聊在 Python 中如何实作之前提到非常重要的 Grandient Descent 和 Pytorch 中的 Grandient Descent

<<:  [DAY20]新手学Istio

>>:  Day 13 onchange

IT铁人DAY 17-State 状态模式

  今天介绍的State Pattern与昨天的Strategy Pattern非常的相似,不过它们...

Flutter基础介绍与实作-Day7 Hello Flutter(1)

我们今天终於要进到Flutter的世界,大家应该都准备好了吧!还没安装完成的小夥伴也不要紧张,可以参...

Day 1 - Series Overview

In this series, I want to introduce some concepts ...

SQL Server VM 常见的 CPU 设定问题 - 心得分享

DBA Bootcamp 前一阵子,发现一台 SQL Server 的 compute 效能好像不如...

Day-19 Excel列印时常见问题

今日练习档 ԅ( ¯་། ¯ԅ) 大家好呀 ٩(ˊ〇ˋ*)و 列印我相信也是很多人会使用的一个功能,...