torch基础知识

torch基础知识_第1张图片

一、张量的创建

很好的总结:PyTorch中张量的创建方法

二、张量的操作

  1. tensor.squeeze() 与 torch.squeeze()
x = torch.linspace(0, 10, 20).reshape(4,5)
x = x.unsqueeze(dim=0)
x = x.unsqueeze(dim=0)
print(x.shape)
print(torch.squeeze(x, dim=0).shape)
#output
>>>torch.Size([1, 1, 4, 5])
>>>torch.Size([1, 4, 5])
___________________________________________________
x = torch.linspace(0, 10, 20).reshape(4,5)
x = x.unsqueeze(dim=0)
x = x.unsqueeze(dim=0)
print(x.squeeze().shape)
#output
>>>torch.Size([4, 5])
  1. torch.transpose()

实现两个维度交换。

x = torch.linspace(0,10,20).reshape(4,1,5)
print(x.shape)
x = torch.transpose(x, 1, 2)
print(x.shape)
#output
>>>torch.Size([4, 1, 5])
>>>torch.Size([4, 5, 1])
  1. tensor.sort()
use:
torch.sort(input, dim=None, descending=False, out=None) -> (Tensor, LongTensor)
return:
1 A tuple of (sorted_tensor, sorted_indices) is returned, (排序后的)
2 where the sorted_indices are the indices of the elements in the original input tensor.(排序后相对于原数据的索引)
  1. 预训练模型加载与修改

很好的总结:
torch.chunk与torch.split的区别
pytorch中reshape、view以及resize_之间的区别
pytorch乘法总结
torch中.sort的使用

你可能感兴趣的:(torch)