pytorch

pytorch 是一个python的开源项目,旨在解决python中的机器学习、数学应用等问题。

本文的目的是介绍pytorch 的常用方法与其在机器学习中的应用,但是对于详细的pytorch应用,应当自行搜索Pytorch官方文档库

torch.reshape()

torch.reshape()是一种改变矩阵排列的方法,reshape中比较特殊的是-1这个关键字。

  • 当存在一个张量tensor,reshape(-1,1)就是随着第二个参数的确定,-1会将剩余参数依次排列。
1
2
3
4
5
6
t = torch.tensor([
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
], dtype = torch.float32)
t2 = t.reshape(-1,1)

result:

  • 当输出格式为(2,-1)时:
1
t2 = t.reshape(2,-1)

result: