首页 > 其他分享 >Four---pytorch学习---基本数据类型/标量/张量/dim值

Four---pytorch学习---基本数据类型/标量/张量/dim值

时间:2022-08-13 19:36:38浏览次数:82  
标签:dim tensor ... torch 数据类型 --- print bit

pytorch学习(1)

pytorch的基本数据类型

  • 在torch中默认的数据类型是32位浮点型(torch.FloatTensor)
  • 可以通过torch.set_default_tensor_type()函数设置默认的数据类型,但该函数只支持设置浮点型数据类型
Data type dtype CPU tensor GPU tensor
32-bit floating torch.float torch.FloatTensor torch.cuda.FloatTensor
6-bit floating torch.double torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating torch.half torch.HalfTensor torch.cuda.HalfTensor
8-bit integer(unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor
8-bit integer(signed) torch.int8 torch.CharTensor torch.cuda.CharTensor
16-bit integer(signed) torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer(signed) torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer(signed) torch.long torch.LongTensor torch.cuda.LongTensor

字符串的表达

  1. one-hot编码(独热编码)
  2. word embedding(词嵌入)
    • word2vec
    • GloVe

标量

  • loss 一般是一个标量,标量维度等于0
import torch

a = torch.tensor(1)
print(a)
print(type(a))
print(a.dim()) #检验维度的方法
print(len(a.shape)) #检验维度的方法,与a.dim()返回值相同
-----------------------------------------------------------------------
tensor(1)
<class 'torch.Tensor'>
0
0

张量

  • 查看设备 a.device
  • 判断 isinstance(a, torch.LongTensor) ---输出值为True或False
  • 查看形状(即具体维度分量)a.shape / a.size()
import torch
a = torch.tensor([1,2,3,4])
print(a.device)
print(isinstance(a, torch.LongTensor))
print(a.shape)
print(a.size())
-----------------------------------------------------------------------
cpu
True
torch.Size([4]) #表示该张量为1维,且该tensor中含有四个元素
torch.Size([4])

维度dim=1(bias,linear layer input 线性层的输入)

# dim=1 ---linear layer input 线性层的输入
import torch
b = torch.tensor([1,2,3,4])
print(b)
print(b.dim())
print(b.type())
print(b.device)
print(b.shape)
print(b.item) #得到tensor中的元素值
-------------------------------------------------------
tensor([1, 2, 3, 4])
1
torch.LongTensor
cpu
torch.Size([4])
<built-in method item of Tensor object at 0x000001C08A635958>

维度dim=2(batch,linear layer input)

import torch
c = torch.tensor([[1,2,3],[5,6,7]])
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.shape)
-------------------------------------------------------
tensor([[1, 2, 3],
        [5, 6, 7]])
2
torch.LongTensor
cpu
torch.Size([2, 3])

维度dim=3(RNN循环神经网络)

import torch
d = torch.tensor([[[1,2,3,4],[5,6,7,8]]])
print(d)
print(d.device)
print(d.type())
print(d.dim())
print(d.shape)
print(torch.numel(d)) #---输出元素的个数
tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]]])
cpu
torch.LongTensor
3
torch.Size([1, 2, 4])
8

维度dim=4(CNN卷积神经网络)

import torch
dim_4 = torch.rand(2,3,32,32) 
#创建一个矩阵,数值随机[照片数量,rgb色彩通道,高度,宽度]
print(dim_4.shape)
print(dim_4)
print(dim_4.type)
print(dim_4.dim())
-------------------------------------------------------
torch.Size([2, 3, 32, 32])
tensor([[[[0.3390, 0.9483, 0.3298,  ..., 0.2760, 0.0703, 0.2972],
          [0.0949, 0.2986, 0.4318,  ..., 0.4174, 0.1951, 0.7157],
          [0.0490, 0.5559, 0.3220,  ..., 0.2061, 0.0510, 0.5373],
          ...,
          [0.9616, 0.8165, 0.3552,  ..., 0.5538, 0.0972, 0.3558],
          [0.4873, 0.4493, 0.1939,  ..., 0.0287, 0.2172, 0.3970],
          [0.4623, 0.2889, 0.6900,  ..., 0.0053, 0.6636, 0.3639]],

         [[0.0261, 0.7910, 0.5141,  ..., 0.9742, 0.4137, 0.4942],
          [0.7926, 0.2910, 0.4155,  ..., 0.4437, 0.2966, 0.1524],
          [0.8576, 0.6514, 0.8738,  ..., 0.4547, 0.8521, 0.9133],
          ...,
          [0.5866, 0.4359, 0.5897,  ..., 0.3321, 0.3467, 0.3109],
          [0.7515, 0.2906, 0.7983,  ..., 0.8138, 0.8543, 0.5278],
          [0.2317, 0.1437, 0.8601,  ..., 0.1081, 0.5985, 0.0319]],

         [[0.2833, 0.2059, 0.4972,  ..., 0.1107, 0.9525, 0.2098],
          [0.7568, 0.1247, 0.5259,  ..., 0.6636, 0.7032, 0.7777],
          [0.0343, 0.3005, 0.4679,  ..., 0.5751, 0.2757, 0.2981],
          ...,
          [0.2303, 0.0075, 0.9801,  ..., 0.5632, 0.2719, 0.0312],
          [0.6431, 0.7712, 0.5269,  ..., 0.6739, 0.9665, 0.5807],
          [0.8637, 0.4354, 0.9796,  ..., 0.8433, 0.5195, 0.5800]]],


        [[[0.0621, 0.3406, 0.6101,  ..., 0.0182, 0.5445, 0.0415],
          [0.6412, 0.3514, 0.3366,  ..., 0.9296, 0.7228, 0.5305],
          [0.1690, 0.4457, 0.0372,  ..., 0.7834, 0.7996, 0.4179],
          ...,
          [0.6941, 0.3181, 0.0958,  ..., 0.7913, 0.2281, 0.8063],
          [0.9883, 0.6680, 0.3952,  ..., 0.0072, 0.7170, 0.2350],
          [0.2541, 0.4535, 0.8134,  ..., 0.5470, 0.8399, 0.5755]],

         [[0.3427, 0.4750, 0.8580,  ..., 0.6513, 0.9364, 0.6572],
          [0.5132, 0.9791, 0.9604,  ..., 0.3622, 0.1767, 0.1125],
          [0.8013, 0.9486, 0.3601,  ..., 0.6069, 0.1177, 0.7456],
          ...,
          [0.1332, 0.0858, 0.5337,  ..., 0.9435, 0.5755, 0.4948],
          [0.5014, 0.5651, 0.2074,  ..., 0.1604, 0.7589, 0.7703],
          [0.0965, 0.0679, 0.3821,  ..., 0.4512, 0.5636, 0.9658]],

         [[0.1001, 0.0370, 0.7520,  ..., 0.5650, 0.4478, 0.4201],
          [0.7473, 0.3291, 0.1565,  ..., 0.7339, 0.1427, 0.2472],
          [0.2715, 0.5282, 0.5252,  ..., 0.3999, 0.0868, 0.5859],
          ...,
          [0.1139, 0.2224, 0.6831,  ..., 0.6763, 0.3141, 0.5551],
          [0.4020, 0.5816, 0.2453,  ..., 0.3932, 0.1766, 0.9528],
          [0.9874, 0.4264, 0.3816,  ..., 0.8513, 0.7539, 0.4453]]]])
<built-in method type of Tensor object at 0x000001C08A6509F8>
4

标签:dim,tensor,...,torch,数据类型,---,print,bit
From: https://www.cnblogs.com/311dih/p/16583846.html

相关文章

  • Five---pytorch学习---创建tensor/API/有初始化创建/无初始化创建/从tensor中创建tens
    ##pytorch学习(2)###创建tensor1.从list中创建tensor2.从numpy中创建tensor/将numpy转换为tensor3.有初始化值创建4.无初始化值创建5.从tensor创建tensor(torch.......
  • Seven---pytorch学习---维度变换
    ##pytorch学习(4)###维度变换-view&reshape-squeeze&unsqueeze-transpose&permute-expand&repeat-contiguous ####view&reshape>view()与r......
  • Six---pytorch学习---索引与切片
    pytorch学习(3)索引与切片普通索引冒号索引(切片)index_select选择特定索引masked_select选择符合条件的索引take索引普通索引index(有负索引)importtorcha......
  • Eight---pytorch学习---广播机制(broadcast)
    pytorch学习(5)广播机制(broadcast)矩阵运算往往都是在两个维度相同或者相匹配(前面矩阵的列数等于后一个矩阵的行数)的矩阵之间定义的,广播机制亦是如此。在机器学习的某......
  • Nine---pytorch学习---拼接与拆分/运算统计
    ##pytorch学习(6)###拼接与拆分-cat-stack-split-chunk####cat()-连接给定维度中给定的张量序列-所有张量必须具有相同的形状(拼接维度除外)或为空-torch.ca......
  • C#并发编程-4 同步
    如果程序用到了并发技术,那就要特别留意这种情况:一段代码需要修改数据,同时其他代码需要访问同一个数据。这种情况就需要考虑同步地访问数据。如果下面三个条件都满足,就必......
  • Three---面向对象与面向过程/属性和变量/关于self/一些魔法方法的使用/继承/super方法
    python的面向对象面向对象与面向过程面向过程面向过程思想:需要实现一个功能的时候,看重的是开发的步骤和过程,每一个步骤都需要自己亲力亲为,需要自己编写代码(自己来做)......
  • AICA第6期-学习笔记汇总
    AICA第6期-学习笔记汇总AICA第六期|预科班课程1.《跨上AI的战车》2.《产业中NLP任务的技术选型与落地》3.《计算机视觉产业落地挑战与应对》4.《搭建适合企业的AI中......
  • djnago-filter用法
    django-filter用法集成drf自定义filter文件内fromdjango_filtersimportrest_frameworkasrs_filtersfrom.modelsimport*classTestFilter(rs_filters.Filter......
  • 常见docker命令(二)-容器生命周期相关
    dockerrun命令主要参数-d后台运行,返回容器id-i以交互模式运行,通常与-t连用-t为容器重新分配一个伪输入终端,通常与-i连用-P(大写)随机端口映射,容器内部端口随机映射到......