惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
罗磊的独立博客
腾讯CDC
云风的 BLOG
云风的 BLOG
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
U
Unit 42
I
InfoQ
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
美团技术团队
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
GbyAI
GbyAI
S
SegmentFault 最新的问题

编程与诗|飒白的个人博客|飒白的闲话仓库|一位主播和程序员的奇妙组合体

侯捷c++笔记:c++泛型与高级用法 侯捷c++笔记:c++面向对象的指针类 侯捷c++笔记:c++面向对象的无指针类 03《穷查理宝典》 查理·芒格丨读书笔记 02《穷爸爸富爸爸》 罗伯特·清崎丨读书笔记 01《影响力》罗伯特・西奥迪尼丨读书笔记 基于结构光投影三维重建:格雷码编码与解码 给CNN添加TVloss的随记 三维重建论文笔记 DeMoN:Depth and Motion Network for Learning Monocular Stereo 三维扫描系列 点云绪论 修改DCNN使其输入从无监督变为有监督模型 茫茫摄影路:好照片的基本三要素 pytorch学习日记:创建Tensor 研究生涯005 利用蒙特卡洛方法实现21点问题的最优解 研究生涯004 基于深度学习的水质图像分类 研究生涯003 基于最近邻法手写数字识别设计 研究生涯002 汽车模糊推理系统实验 研究生涯001 浅析机器视觉在医疗影像处理中的应用 人工智能问答NO.8 决策树与随机森林
pytorch学习日记:张量数据类型
飒白 · 2021-01-02 · via 编程与诗|飒白的个人博客|飒白的闲话仓库|一位主播和程序员的奇妙组合体

python与pytorch对应转换

python PyTorch
Int IntTensor of size()
float FloatTensor of size()
Int array IntTensor of size [d1, d2 ,.….]
Float array FloatTensor of size [d1, d2,…]
string

怎样表达string

  • One-hot
    • [0,1,0,0,…]
    • 缺点:单词之间的相关性并没有体现出来,并且是极度稀疏矩阵。
  • Embedding:数字表达语言
    • word2vec
    • glove

数据类型

Data type dtype CPU tensor GPU tensor
32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point torch.float16 or 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.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor

Type check

in:a = torch.randn(2,3)
in:a.type()
out:'torch.floatTensor'
in: type(a)
out:torch.Tensor
in:isinstance(a,torch.FloatTensor)
out:True
isinstance(data,torch.cude,DoubleTensor)
#false
data=data.cude()
isinstance(data,torch.FloatTensor)
#True

Dimension 0 /rank 0

torch.tensor(1.)
#tensor(1.)
torch.tensor(1.3)
#tensor(1.300)

Dim 0

>>> a=torch.tensor(2.2)
>>> a.shape
torch.Size([])
>>> len(a.shape)
0
>>> a.size()
torch.Size([])
  • 通常用在loss

dim 1 /rank 1

>>> torch.tensor([1.1])
tensor([1.1000])

>>> torch.tensor([1.1,2.2])
tensor([1.1000, 2.2000])

>>> torch.FloatTensor(1)
tensor([3.0100e+32])

>>> torch.FloatTensor(2)
tensor([0.0000, 1.8750])

>>> data = np.ones(2)
>>> data
array([1., 1.])

>>> torch.from_numpy(data)
tensor([1., 1.], dtype=torch.float64)
  • 通常用在Bias
  • 或者用在Linear Input
>>> a = torch.ones(2)
>>> a.shape
torch.Size([2])

Dim 2

>>> a = torch.randn(2,3)
>>> a
tensor([[ 2.8500, -1.0306,  0.4257],
        [-0.9918,  0.3094,  0.3869]])
>>> a.shape
torch.Size([2, 3])
>>> a.size(0)
2
>>> a.size(1)
3
>>> a.shape[1]
3
  • 通常用在带有batch的linear input
>>> import torch
>>> a=torch.rand(1,2,3)
>>> a
tensor([[[0.2698, 0.2206, 0.2297],
         [0.6471, 0.9053, 0.2742]]])
>>> a.shape
torch.Size([1, 2, 3])
>>> a[0]
tensor([[0.2698, 0.2206, 0.2297],
        [0.6471, 0.9053, 0.2742]])
>>> list(a.shape)
[1, 2, 3]
  • RNN input Batch
    • [10个单词,20句话,100维表示]

Dim4

>>> a=torch.rand(2,3,28,28)#张数,通道数,28*28尺寸
>>> a
tensor([[[[0.6408, 0.0313, 0.0700,  ..., 0.1324, 0.7475, 0.1444],
          [0.8315, 0.7339, 0.0126,  ..., 0.5260, 0.4896, 0.7950],
          [0.5399, 0.6679, 0.6118,  ..., 0.1571, 0.0436, 0.6759],
         [[0.6578, 0.7380, 0.5285,  ..., 0.0970, 0.8109, 0.2364],
          [0.4398, 0.4145, 0.6420,  ..., 0.7736, 0.1780, 0.4816],
          [0.5297, 0.0386, 0.2156,  ..., 0.3286, 0.7100, 0.1554],
          ...,
          [0.4681, 0.1787, 0.7982,  ..., 0.5238, 0.5333, 0.7992],
          [0.5267, 0.1461, 0.7075,  ..., 0.0974, 0.9872, 0.8482],
          [0.9274, 0.4764, 0.9747,  ..., 0.1132, 0.7222, 0.1312]]]])
>>> a.shape
torch.Size([2, 3, 28, 28])

Mixd

>>> a.shape
torch.Size([2, 3, 28, 28])
>>> a.numel()
4704
>>> a.dim()
4
>>> a=torch.tensor(1)
>>> a.dim()
0