当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
中国移动通信集团有限公司:业务委托书
HCIP OSPF/MGRE综合实验
arcgis填坑_1
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-2
ETCD cluster fault emergency recovery - to recover from the snapshot
HCIA实验
grep、sed、awk
vi display line number in buildroot embedded file system
ovnif摄像头修改ip
CLUSTER DAY01 (Introduction to cluster and LVS, LVS-NAT cluster, LVS-DR cluster)
【LeetCode】2034. 股票价格波动(思路+题解)双map
iptables入门
HCIP-BGP的选路实验
SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
局域网文件传输
China Mobile Communications Group Co., Ltd.: Business Power of Attorney
CLUSTER DAY04 (Block Storage Use Cases, Distributed File Systems, Object Storage)
My meeting of the OA project (meeting seating & review)
HCIA experiment
arcgis填坑_4









