PyTorchLearning
Basic requirements & comparison with other frameworks:
Software: IDE with dubg function (such as PyCharm, Eclipse), Jupyter, anaconda, Pytorch Compared
with PyTorch , Tensorflow has more definitions and rules, and it is not very convenient in the debugging process.
framework learning method
Do not stick to specific operations, find and use in the process of actual application
Basic usage
Create a matrix
x = torch.empty(5,3); x =torch.rand(5.3);
It can be seen that the data format output by the
pytorch framework is tensor (tensor) The data format output by the
TensorFlow framework is ndarray
Show matrix size torch.size()
change matrix dimension view(dimension)
Numpy (ndarry) and PyTorch (tensor) interaction
autograd mechanism
Why use a frame? Because programming by yourself is too troublesome, especially when doing backpropagation. When backpropagating
, we need to differentiate the matrix , which is laborious. If the framework helps us finish it, we can concentrate on building the model up
requires_grad=True
Note: If the gradient is not cleared , the gradient will be accumulated!!!
So, when we calculate the gradient, the basic process is:
**清零->梯度计算->反向传播->清零**
example
Create two arrays x and y:
Build a model:
Formulate hyperparameters & loss function
training model:
Target | annotation | the code |
---|---|---|
Data format conversion | Convert the input data format to tensor format | torch.from_numpy(x_train) |
Gradient reset | Avoid errors in gradient accumulation | .zero_grad() |
forward propagation | get predicted data | |
calculate loss | i.e. the gap between the prediction loss and the label | |
backpropagation | Find the gradient | loss.backward() |
Perform parameter update | Automatically update parameters according to learning rate and gradient | .step() |
process display | To prevent the graphics card from exploding | loss.item |
predict | ||
model save | After the model is saved, it is a dictionary type (the model is not just a model, | torch.save(model.state_dict(),‘model.pkl’) |
model read | There may also be model losses, results, etc. can be put in) | model.load_state_dict(torch.load(“model.pkl”)) |
At present, the most concise version is finished, and there are many problems that have not been solved (for example, in our code, one batch is all run, and theoretically it should be run one batch at a time), let’s ignore it and learn later
Now we test the model.
The test is to put the data into the trained network. The obtained data must be converted to the type~
the model is saved