当前位置:网站首页>numpy和tensor增加或删除一个维度
numpy和tensor增加或删除一个维度
2022-08-11 05:35:00 【Pr4da】
numpy
1.增加维度
import numpy as np
a = np.ones((3,4))
print(a.shape)
输出a的形状:(3,4)
现在变量a为二维数组,在图形计算中有时需要将图像处理成(length, width, channel)的形式,我们需要将a处理成三维,第三维的形状为1,方式如下:
b = np.expand_dims(a, 2)
print(b.shape)
输出的形状为:(3,4,1)
2.压缩维度
如果我们想要将b再变回二维,需要用到如下方法:
c = np.squeeze(b)
print(c.shape)
输出的形状为:(3,4)
我们也可以指定要压缩的维度
c = np.squeeze(b, axis=2)
print(c.shape)
输出为:(3,4)
Tensor
1.增加维度
import torch
a = torch.ones((3,4))
print(a.shape)
输出的形状为:torch.Size([3,4])
现在,我们要让它增加一个维度,变成[1,3,4]
b = torch.unsqueeze(a, 0)
print(b.shape)
输出结果为:torch.Size([1,3,4])
现在我们要把它再压缩到三维:
c = torch.squeeze(b, 0)
print(c.shape)
输出结果为:torch.Size([3,4])
。1
边栏推荐
- WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
- detectron2,手把手教你训练mask_rcnn
- AUTOMATION DAY06 (Ansible Advanced, Ansible Role)
- HCIP OSPF/MGRE综合实验
- 查看CPU和其他硬件温度的软件
- MoreFileRename批量文件改名工具
- HCIP-BGP的选路实验
- Es common operations and classical case
- arcgis填坑_2
- SECURITY DAY01 (Monitoring Overview, Zabbix Basics, Zabbix Monitoring Services)
猜你喜欢
随机推荐
windows10安全中心显示“修正未完成”
uboot设置默认的bootdelay
HCIP实验(pap、chap、HDLC、MGRE、RIP)
升级到Window11体验
八股文之并发编程
OA项目之我的会议(会议排座&送审)
从mask-rcnn到shp
arcgis填坑_3
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
OA项目之我的审批(查询&会议签字)
SATA、SAS、SSD三种硬盘存储性能数据
CLUSTER DAY01 (Introduction to cluster and LVS, LVS-NAT cluster, LVS-DR cluster)
中国移动通信集团有限公司:业务委托书
arcgis填坑_2
Es common operations and classical case
iptables nat
grep、sed、awk
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-19
HCIP BGP建邻实验
Concurrent programming in eight-part essay