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

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(词嵌入)
  3. word2vec
  4. GloVe

标量

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

a = torch.tensor(1)
print(a)
print(type(a))
print(a.dim()) #检验维度的方法
print(len(a.shape)) #检验维度的方法,与a.dim()返回值相同
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],
        [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卷积神经网络)

`python
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())

Original: https://www.cnblogs.com/311dih/p/16583846.html
Author: 叁_311
Title: Four—pytorch学习—基本数据类型/标量/张量/dim值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/642912/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球