当前位置:网站首页>Pytorch中torch.numel(),torch.shape,torch.size()和torch.reshape()函数解析
Pytorch中torch.numel(),torch.shape,torch.size()和torch.reshape()函数解析
2022-07-15 17:30:00 【cv_lhp】
一. torch.numel()函数解析
1. 官网链接
torch.numel(),如下图所示:
2. torch.numel()函数解析
torch.numel(input) → int
返回输入张量中元素的总数。
3. 代码举例
a1 = torch.randn(1, 2, 3, 4, 5)
b = torch.numel(a1)#输入元素总数为1x2x3x4x5=120
a2 = torch.zeros(4,4)
c = torch.numel(a2)#输入元素总数为4x4=16
a3 = torch.randn(size=(5,4))
d = torch.numel(a3)#输入元素总数为5x4=20
a4 = torch.randn(6)
e = torch.numel(a4)#输入元素总数为6
a1.shape,b,a2.shape,c,a3.shape,d,a4.shape,e
输出结果如下:
(torch.Size([1, 2, 3, 4, 5]),
120,
torch.Size([4, 4]),
16,
torch.Size([5, 4]),
20,
torch.Size([6]),
6)
二. torch.shape解析
1. torch.shape解析
返回输入tensor张量的维度大小
2. 代码举例
a1 = torch.randn(1, 2, 3, 4, 5)
a2 = torch.randn(size=(5,4))
a1.shape,a2.shape
输出结果如下:
(torch.Size([1, 2, 3, 4, 5]), torch.Size([5, 4]))
三. torch.size()函数解析
1.torch.size()函数解析
跟torch.shape效果相同,也是返回输入tensor张量的维度大小。
2.代码举例
a1 = torch.randn(1, 2, 3, 4, 5)
a2 = torch.randn(size=(5,4))
a1.size(),a2.size()
输出结果如下:
(torch.Size([1, 2, 3, 4, 5]), torch.Size([5, 4]))
四.torch.reshape()函数解析
1. 官网链接
torch.reshape(),如下图所示:
2. torch.reshape()函数解析
torch.reshape(input, shape) → Tensor
返回将输入的形状转变为shape指定的形状大小,元素总数不变。
3.代码举例
a = torch.zeros(size=(5,4))
b = a.reshape(-1)#输出张量b的size为torch.Size([20])
c = a.reshape(2,-1) #输出张量c的size为torch.Size([2, 10])
d = a.reshape(shape=(2,10)) #输出张量d的size为torch.Size([2, 10])
e = a.reshape(shape=(4,-1))#输出张量e的size为torch.Size([4, 5])
a,a.shape,b,b.shape,c,c.shape,d,d.shape,e,e.shape
输出结果如下:
(tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]),
torch.Size([5, 4]),
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
torch.Size([20]),
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]),
torch.Size([2, 10]),
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]),
torch.Size([2, 10]),
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]),
torch.Size([4, 5]))
边栏推荐
- 渗透测试流程
- AMD Ryzen 5 7600X 6核心和4.4GHz 'Zen 4 ' CPU现身跑分数据库
- 服务端的那些事儿
- HTAP能力加速TPC-H执行前要怎么部署PolarDB for PostgreSQL?
- CAS与AQS简单理解
- Millimeter wave radar learning (V) -- angle estimation
- Intel releases open source AI Reference Suite
- Redis distributed lock: what have you experienced from Xiaobai to Dashen?
- 英特尔发布开源AI参考套件
- LINQ implements query string splicing: and and or
猜你喜欢
随机推荐
LAN attack and network device security configuration
Openharmony module 2 file samgr_ Server parsing (4)
福赛生物解读2022上半年大气环境变化,VOCs治理依然是破局关键
Can you use redis? Then come and learn about redis protocol
2、趋势科技2017校招开发岗试题
抽丝剥茧C语言(高阶)静态通讯录
Redis 分布式锁:从小白到大神方案都经历了什么?
[use win10's own remote connection tool] to remotely access and control [another win10 Computer]
Queue(单项队列)和Deque(双端队列)的知识点整理
Implement a few simple loaders
OpenHarmony模块二初分析
Those things on the server side
[brother hero July training] day 15: depth first search
Outside the bomb, can enterprises use DMS in as many environments as inside the bomb when using RDS? For example, how many databases do I want under these production instances
C语言:使用宏重定义printf,打印【debug调试信息】
mysql中 decimal(10,2)格式的通过stream 方式写到 kafka 变成 stri
Detailed explanation of time complexity and space complexity
聊聊程序员的简历应该怎么写(帮修改简历)
Eight guidelines for modbus-rs485 wiring
ASP. Net








