当前位置:网站首页>【DL】关于tensor(张量)的介绍和理解
【DL】关于tensor(张量)的介绍和理解
2022-07-29 05:21:00 【机器不学习我学习】
1. 前言
在深度学习中,我们肯定会遇到一个名词:张量(tensor)。对于一维、二维我们比较好理解,但是三维、四维、…、n维,我们该如何理解呢?下面我们将以pytorch深度学习框架为例进行详细介绍。
2. 一维
import torch # 版本:1.8.0+cpu
a = torch.tensor([1,2,3,4])
print(a)
print(a.shape)
输出:
tensor([1, 2, 3, 4])
torch.Size([4])
输出只有一个维度,所以是一维。
3. 二维
import torch # 版本:1.8.0+cpu
a = torch.tensor([[1,2,3,4]])
print(a)
print(a.shape)
输出:
tensor([[1, 2, 3, 4]])
torch.Size([1, 4])
上面输出有行和列,所以它二维张量,实际上就是一个二维矩阵。
4. 三维
import torch # 版本:1.8.0+cpu
a = torch.ones(1,3,3)
print(a)
print(a.shape)
输出:
tensor([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])
torch.Size([1, 3, 3])
从输出结果可以看到这个张量是有三个维度的,前面多了一个维度1。但无法直观看到这个1体现在哪里。下面再来看一个张量,直观感受一下这个最前面的维度体现在哪里:
import torch # 版本:1.8.0+cpu
a = torch.ones(3,4,5)
print(a)
print(a.shape)
输出:
tensor([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]])
torch.Size([3, 4, 5])
从上面输出结果,可以直观感受到最前面维度(数字3)的体现;
第一个数字3:分成3大行
第二个数字4:每一大行分为4小行
第三个数字5:每一大行分为5小列
所以数据的维度是3×4×5,最后一个数字表示列的维度;我们也可以理解为3个4行5列的数据。
如果我们将上面的张量类比为一张RGB的图像,数字3表示3个通道,每个通道的大小为4行5列。
5. 四维
import torch # 版本:1.8.0+cpu
a = torch.ones(2,3,4,5)
print(a)
print(a.shape)
输出:
tensor([[[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]],
[[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]]])
torch.Size([2, 3, 4, 5])
我们用分割线将上面的输出分开:
红线上面一部分是一个“维度”,下面又是一个“维度”,所以是两个维度。
再直白一点,就是张量a,有2大行,每大行又分了3小行,每行又分了4行,然后又分了5列。
张量a在日常图像数据集中可以这么理解:
第一个数字2:其实就是batchsize,就相当于这个张量是输入了2张图像
第二个数字3:每张图像的通道数是3
第三个数字4:图像的高为4
第四个数字5:图像的宽为5
参考:
https://mp.weixin.qq.com/s/9gdoufWGE8xOvwPvAcVEGw
https://blog.csdn.net/z240626191s/article/details/124204965
我的微信公众号:
欢迎大家关注,不定时分享相关技术。
边栏推荐
- 华为2020校招笔试编程题 看这篇就够了(下)
- 并发编程学习笔记 之 原子操作类AtomicInteger详解
- Realize the scheduled backup of MySQL database in Linux environment through simple script (mysqldump command backup)
- Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
- 性能对比|FASS iSCSI vs NVMe/TCP
- 华为2020校招笔试编程题 看这篇就够了(上)
- Detailed explanation of tool classes countdownlatch and cyclicbarrier of concurrent programming learning notes
- Machine learning makes character recognition easier: kotlin+mvvm+ Huawei ml Kit
- 中海油集团,桌面云&网盘存储系统应用案例
- "Shandong University mobile Internet development technology teaching website construction" project training log I
猜你喜欢
Reporting Service 2016 自定义身份验证
30 knowledge points that must be mastered in quantitative development [what is level-2 data]
Training log 4 of the project "construction of Shandong University mobile Internet development technology teaching website"
Semaphore (semaphore) for learning notes of concurrent programming
【go】defer的使用
Reporting Services- Web Service
Fantom (FTM) prices will soar by 20% in the next few days
Most PHP programmers don't understand how to deploy safe code
mysql 的show profiles 使用。
Thinkphp6 pipeline mode pipeline use
随机推荐
The LAAS protocol of defi 2.0 is the key to revitalizing the development of defi track
Detailed explanation of MySQL statistical function count
Realize the scheduled backup of MySQL database in Linux environment through simple script (mysqldump command backup)
“山东大学移动互联网开发技术教学网站建设”项目实训日志三
Reporting Services- Web Service
Thinkphp6 pipeline mode pipeline use
"Shandong University mobile Internet development technology teaching website construction" project training log I
Markdown syntax
Show profiles of MySQL is used.
Power BI Report Server 自定义身份验证
Fantom (FTM) surged 45% before the FOMC meeting
DataX installation
C# 连接 SharepointOnline WebService
Get the number of daffodils
重庆大道云行作为软件产业代表受邀参加渝中区重点项目签约仪式
并发编程学习笔记 之 原子操作类AtomicInteger详解
day02 作业之文件权限
深度学习的趣味app简单优化(适合新手)
Detailed steps of JDBC connection to database
How to obtain openid of wechat applet in uni app project