这是学习pytorch的学习笔记,个人记录篇
pytorch是一个基于Python的科学计算包,目标用户有两类
- 为了使用GPU来替代numpy
- 一个深度学习研究平台:提供最大的灵活性和速度
张量Tensor类似于numpy,但是可以使用GPU来进行相关的计算。
创建数组:
x = torch.Tensor(5, 3) 维度
x = torch.zeros(5, 3, dtype=torch.long) 维度 类型
x = torch.tensor([5.5, 3]) 内容
x = x.new_ones(5, 3, dtype=torch.double)
x = torch.randn_like(x, dtype=torch.float)
查看尺寸:
print(x.size())
执行加法;
1.y = torch.rand(5, 3)
print(x + y)
2.print(torch.add(x, y))
3.result = torch.empty(5, 3)
torch.add(x, y, out=result)
4.y.add_(x)
** 注意**
任何在原地(in-place)改变张量的操作都有一个_后缀。例如x.copy_(y), x.t_()操作将改变x.
你可以使用所有的numpy索引操作。
print(x[:, 1])
调整维度
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # -1的意思是没有指定维度
print(x.size(), y.size(), z.size())