中文官网:https://pytorch.apachecn.org/
知乎的建议:新手如何入门pytorch?
导入包:import torch
查看某个包,如:help(torch)
>>> import torch
>>> help(torch)
>>>
或者在查看官网:https://pytorch.org/docs/stable/torch.html
创建一个新矩阵
>>> x = torch.empty(5, 3)
>>> x
tensor([[1.3563e-19, 1.3563e-19, 1.8888e+31],
[1.2727e+25, 4.3053e+21, 6.0039e+31],
[7.5338e+28, 6.1687e+16, 4.9153e-14],
[1.3556e-19, 1.3563e-19, 1.3563e-19],
[1.3563e-19, 1.0903e+27, 5.9423e-02]])
>>>
类似:创建一个全0矩阵:
x = torch.zeros(5, 3, dtype=torch.long)
创建一个全1矩阵:
x = torch.ones(2, 2, requires_grad=True)
用数据直接构造一个矩阵:
x = torch.tensor([5.5, 3])
使用numpy转换成torch
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
torch转换成numpy
y = x.numpy()
属性
运算