Pytorch(二)

                   张量操作与线性回归

目录

张量的操作:

拼接:

 torch.cat()

 torch.stack()

切分:

torch.chunk()

torch.split()

索引:

torch.index_select()

torch.masked_select()

变换:

torch.reshape()

torch.transpose()

torch.t()

torch.squeeze()

torch.unsqueeze()


张量的操作:

  • 拼接:

  •  torch.cat()

功能:将张量按维度dim进行拼接

成员变量:

  • tensors:张量序列 
Union[Tuple[Tensor, ...], List[Tensor]]
  • dim:要拼接的维度
torch.cat()
  •  torch.stack()

功能:在新创建的维度dim进行拼接

成员变量:

  • tensors:张量序列
  • dim:要拼接的维度

 例程:

import torch
import numpy as np

n=np.array([[1,2,3,4],[2,3,4,5],[3,4,5,6]],dtype=int)
t=torch.tensor(n,device="cuda",requires_grad=False)
t_cat_0=torch.cat([t,t],dim=0)
t_cat_1=torch.cat([t,t],dim=1)
t_stack_0=torch.stack([t,t],dim=0)
t_stack_1=torch.stack([t,t],dim=1)
print("tensor: ",t)
print("tensor_cat_0: ",t_cat_0)
print("tensor_cat_1: ",t_cat_1)
print("tensor_stack_0: ",t_stack_0)
print("tensor_stack_1: ",t_stack_1)

结果:

注意:cat()不会扩展张量的维度,而stack()会拓展张量的维度。

  • 切分:

  • torch.chunk()

功能:将张量按维度dim进行平均切分。

返回值:张量列表

成员变量:

  • input:要切分的张量
  • chunks:要切分的份数
  • dim:要切分的维度

注意事项:若不能整除,最后一项张量小于其他张量 

t_chunk=torch.chunk(t,2,1)

print("tensor _chunk: ",t_chunk)

t_chunk=torch.chunk(t,2,1)
for chunk in t_chunk:
    print("tensor _chunk: ",chunk)

  • torch.split()

功能:将张量按维度dim进行切分。

返回值:张量列表

成员变量:

  • tensor:要切分的张量
  • split_size_or_sections:为int时,表示每一份的长度;为list时,按list元素切分
  • dim:要切分的维度

实现:

t_split=torch.split(t,2,1)
for split in t_split:
    print("tensor _split: ", split)

t_split_list=torch.split(t,[1,3],1)
for split in t_split_list:
    print("tensor _split_list: ", split)

注意:如果使用list作为参数,则元素总和等于切分前的数量。

  • 索引:

  • torch.index_select()

功能:在维度dim上,按index索引数据

返回值:依index索引数据拼接的张量

成员变量:

  • input:要索引的张量
  • dim:要切分的维度
  • index:要索引数据的序号

实现:

x = torch.randn(3, 4)		# 目标矩阵
indices = torch.tensor([0, 2],dtype=torch.long)	# 在轴上筛选座标
t_index_select_0=torch.index_select(x, dim=0, index=indices)	# 指定筛选对象、轴、筛选座标

t_index_select_1=torch.index_select(x, dim=1, index=indices)
print("tensor: ",x);
print("tensor_0: ",t_index_select_0)
print("tensor_1: ",t_index_select_1)

  • torch.masked_select()

功能:按mask中的true进行索引

返回值:一维张量

成员变量:

  • input:要索引的张量
  • mask:与input同形状的布尔类型张量

实现:

print("tensor: ",t)
mask=t.ge(5)
print("mask: ",mask)
t_mask_select=torch.masked_select(t,mask=mask)
print("t_mask_select: ",t_mask_select)

  • 变换:

  • torch.reshape()

功能:变换张量形状

注意事项:当张量在内存中是连续时,新张量与input共享数据内存

成员变量:

  • input:要变换的张量
  • shape:新张量的形状

实现:

t_reshape=torch.reshape(t,[-1,2])
print("t_reshape: ",t_reshape)

注意:[-1,2]中的-1表示我们不关心的维度,由系统决定。

  • torch.transpose()

功能:交换张量的两个维度

成员变量:

  • input:要变换的张量
  • dim0:要交换的维度
  • dim1:要交换的维度

实现:

d=torch.randint(5,(2,3,3))
print("t: ",d)
t_transpose=torch.transpose(d,dim0=0,dim1=1)
print("t_transpose: ",t_transpose)

  • torch.t()

功能:2维张量装置,对矩阵而言等价于torch.transpose(input,0,1)

成员变量:

  • input:要变换的张量

实现:

t_transpose=torch.t(t)
print("t_transpose: ",t_transpose)

 

  • torch.squeeze()

功能:压缩长度为1的维度(轴)

成员变量:

  • dim:若为None,移除所有长度1的轴;若指定维度,当且仅当该轴长度为1时,可以被移除;

实现:

t1=torch.rand((1,2,3))
print("t1: ",t1)
print("t1.size:",t1.size())
t1_sq=torch.squeeze(t1)
print("t1_sq: ",t1_sq)
print("t1_sq.size: ",t1_sq.size())

 

  • torch.unsqueeze()

功能:y依据dim扩展维度

成员变量:

  • dim:扩展的维度

实现:

t1=torch.rand((1,2,3))
print("t1: ",t1)
print("t1.size:",t1.size())
t1_sq=torch.squeeze(t1)
print("t1_sq: ",t1_sq)
print("t1_sq.size: ",t1_sq.size())
t1_usq=torch.unsqueeze(t1_sq,0)
print("t1_usq.size: ",t1_usq.size())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章