zoukankan      html  css  js  c++  java
  • torch.Tensor文档学习笔记

    A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

    张量(torch.Tensor)是包含单个数据类型元素的多维矩阵。

    1、张量定义了如下八种CPU张量类型和八种GPU张量类型:

    #CPU对应八种数据类型,GPU对应也有八种数据类型,如torch.cuda.FloatTensor([])
    torch.FloatTensor([])
    torch.DoubleTensor([])
    torch.HalfTensor([])
    torch.IntTensor([])
    torch.LongTensor([])
    torch.ShortTensor([])
    torch.ByteTensor([])
    torch.CharTensor([])
    
    Data type                    CPU tensor                GPU tensor
    32-bit floating point    torch.FloatTensor         torch.cuda.FloatTensor
    64-bit floating point    torch.DoubleTensor   torch.cuda.DoubleTensor
    16-bit floating point    torch.HalfTensor        torch.cuda.HalfTensor
    8-bit integer (unsigned)    torch.ByteTensor        torch.cuda.ByteTensor
    8-bit integer (signed)    torch.CharTensor        torch.cuda.CharTensor
    16-bit integer (signed)    torch.ShortTensor    torch.cuda.ShortTensor
    32-bit integer (signed)    torch.IntTensor         torch.cuda.IntTensor
    64-bit integer (signed)    torch.LongTensor         torch.cuda.LongTensor

    2、数据的原地修改还是生成副本

    #函数后面跟短下划线,表示就地修改
    a=torch.Tensor([-1,-2])
    b=torch.Tensor([-2,2])
    print('a.abs() :',a.abs(),'
    ','a:',a)
    print('b.abs_() :',b.abs_(),'
    ','b:',b)
    # a.abs():
    # 1
    # 2
    # [torch.FloatTensor of size 2]
    #
    # a:
    # -1
    # -2
    # [torch.FloatTensor of size 2]
    #
    # b.abs_():
    # 2
    # 2
    # [torch.FloatTensor of size 2]
    #
    # b:
    # 2
    # 2
    # [torch.FloatTensor of size 2]

    3、backward

     x=torch.tensor([[1,1],[1.,1.]],requires_grad=True)
    out=x.pow(2).sum()
    out.backward()
    x.grad
    Out[198]:
    tensor([[ 2.,  2.],
            [ 2.,  2.]])
  • 相关阅读:
    iOS 字典实现原理
    IOS中armv7,armv7s,arm64以及i386和x86_64讲解
    SDWebImage源码解析
    iOS Runtime的消息转发机制
    二叉树的遍历
    LINUX 常用命令 ps 详解
    LINUX 文件权限详解
    LINUX查看内存使用情况 free
    PHP isset() empty() isnull() 的区别
    PHP unset()函数销毁变量 但没有实现释放内存
  • 原文地址:https://www.cnblogs.com/bawu/p/8267452.html
Copyright © 2011-2022 走看看