张量的操作:拼接、切分、索引、变换
一、张量的拼接与切分
torch.cat()
功能:将张量按维度dim进行拼接
tensors:张量序列
dim:要拼接的维度
torch.stack()
功能:在新创建的维度dim上进行拼接
tensors:张量序列
dim:要拼接的维度
torch.cat(tensors,dim=0,out=None)
torch.stack(tensors,dim=0,out=None)
torch.chunk()
功能:将张量按维度dim进行平均切分
返回值:张量列表 (注:若不能整除,最后一份张量小于其他张量)
input:要切分的张量
chunks:要切分的份数
dim:要切分的维度
torch.chunk(input,chunks,dim=0)
torch.split()
功能:将张量按维度dim进行切分
返回值:张量列表
tensor:要切分的张量
split_size_or_sections:为int时,表示每一份的长度;为list时,按list元素切分
dim:要切分的维度
torch.split(tensor,split_size_or_sections,dim=0)
二、张量索引
torch.index_selcet()
功能:在维度dim上,按index索引数据
返回值:依index索引数据拼接的张量
input:要索引的张量
dim:要索引的维度
index:要索引数据的序号
torch.index_select(input,dim,index,out=None)
torch.masked_select()
功能:按mask中的True进行索引
返回值:一维张量
input:要索引的张量
mask:与input同形状的布尔类型张量
torch.masked_selcet(input,mask,out=None)
三、张量的变换
torch.reshape()
功能:变换张量的形状(注:当张量在内存中是连续时,新张量与input共享数据内存)
input:要变换的张量
shape:新张量的形状
torch.reshape(input,shape)
torch.transpose()
功能:交换张量的俩个维度
input():要变换的张量
dim0:要交换的维度
dim1:要交换的维度
torch.t()
功能:2维张量的转置,对矩阵而言,等价于torch.transpose(input,0,1)
torch.transpose(input,dim0,dim1)
torch.t(input)
torch.squeeze()
功能:压缩长度为1的维度
dim:若为None,移除所有长度为1的轴;若指定维度,当且仅当该轴的长度为1时,可以被移除。
torch.usqueeze()
功能:依据dim扩展维度
dim:扩展维度
torch.squeeze(input,dim=None,out=None)
torch.usqueeze(input,dim,out=None)
最后
以上就是单身芹菜最近收集整理的关于PyTorch框架:张量的操作的全部内容,更多相关PyTorch框架:张量内容请搜索靠谱客的其他文章。
发表评论 取消回复