当前位置:网站首页>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
边栏推荐
- Record a Makefile just written
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-18
- Conference OA Project My Conference
- SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
- AUTOMATION DAY06( Ansible进阶 、 Ansible Role)
- 八股文之并发编程
- The ramdisk practice 1: the root file system integrated into the kernel
- iptables 使用脚本来管理规则
- SECURITY DAY02 (Zabbix alarm mechanism, Zabbix advanced operation and monitoring case)
- MySQl进阶之索引结构
猜你喜欢
随机推荐
Local yum source build
照片的35x45,300dpi怎么弄
CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )
visio文件批量转pdf
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
window10吐槽
ETCD cluster fault emergency recovery - local data is available
My meeting of the OA project (meeting seating & review)
vi display line number in buildroot embedded file system
【LeetCode】306.累加数(思路+题解)
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-19
查看可执行文件依赖的库ldd
HCIP BGP neighbor building, federation, and aggregation experiments
HCIP实验(pap、chap、HDLC、MGRE、RIP)
arcgis填坑_1
HCIP BGP建邻、联邦、汇总实验
SECURITY DAY01 (Monitoring Overview, Zabbix Basics, Zabbix Monitoring Services)
slurm cluster construction
Eight-legged text of mysql
vnc remote desktop installation (available for personal testing on 2021-10-20)