pytorch学习记录

时间:2023-03-09 18:27:00
pytorch学习记录

1、pytorch中的torch.split只能将tensor分割为相等的几分,如果需要特定的需求将tensor分割开,可以用torch.index_select。使用的时候,先生成index索引,示例程序如下:

import torch
import random
# tensor
data = torch.randn(10,10,10,10)
channel_list = [i for i in range(0,data.shape[1])]
# 在channel_list中返回k个随机数
k = 5
select_channel = random.sample(channel_list,k)
select_channel = torch.tensor(select_channel,dtype=torch.long)
# 从小到大排下序
select_channel,i = torch.sort(select_channel)
print(select_channel)
select_tensor = torch.index_select(data,1,select_channel)

2、转置图像和翻转图像,平移图像

转置直接用transpose即可,后面只跟两个参数,就是两个要转置的维度。