当前位置:网站首页>【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
我的微信公众号:
欢迎大家关注,不定时分享相关技术。
边栏推荐
- Rsync+inotyfy realize real-time synchronization of single data monitoring
- Detailed explanation of MySQL statistical function count
- 机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
- ssm整合
- Print out all prime numbers between 1-100
- July 28 ens/usd Value Forecast: ENS attracts huge profits
- Intelligent security of the fifth space ⼤ real competition problem ----------- PNG diagram ⽚ converter
- XDFS&空天院HPC集群典型案例
- Detailed explanation of atomic operation classes atomicreference and atomicstampedreference in learning notes of concurrent programming
- 北京宝德&TaoCloud共建信创之路
猜你喜欢

centos7 静默安装oracle

How can Plato obtain premium income through elephant swap in a bear market?

Starfish OS: create a new paradigm of the meta universe with reality as the link
![[go] use of defer](/img/10/9e4e1c593870450c381a154f31ebef.png)
[go] use of defer

Reporting Services- Web Service

Tear the ORM framework by hand (generic + annotation + reflection)

day02作业之进程管理

超简单集成HMS ML Kit 人脸检测实现可爱贴纸

超简单集成HMS ML Kit 实现parental control

Plato farm is expected to further expand its ecosystem through elephant swap
随机推荐
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
并发编程学习笔记 之 Lock锁及其实现类ReentrantLock、ReentrantReadWriteLock和StampedLock的基本用法
How to PR an open source composer project
File permissions of day02 operation
CMD window under Windows connects to MySQL and operates the table
Power BI Report Server 自定义身份验证
C# 连接 SharepointOnline WebService
如何零代码制作深度学习的趣味app(适合新手)
Crypto giants all in metauniverse, and platofarm may break through
Fantom (FTM) prices will soar by 20% in the next few days
xtrabackup 的使用
PHP write a diaper to buy the lowest price in the whole network
赓续新征程,共驭智存储
"Shandong University mobile Internet development technology teaching website construction" project training log I
【数据库】数据库课程设计一一疫苗接种数据库
Basic use of array -- traverse the circular array to find the maximum value, minimum value, maximum subscript and minimum subscript of the array
Nailing alarm script
Training log 7 of the project "construction of Shandong University mobile Internet development technology teaching website"
Thinkphp6 pipeline mode pipeline use
Get the number of daffodils