pytorch framework learning (1) simple construction of the network

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.
insert image description here

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);
insert image description here
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)
insert image description here

Numpy (ndarry) and PyTorch (tensor) interaction
tensor-->ndarry
ndarry-->tensor

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
Manually define which parameter to import
insert image description here
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:
x_values = [i for i in range]
insert image description here

Build a model:

insert image description here
Formulate hyperparameters & loss function
insert image description here
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”))

insert image description here
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~
insert image description here
the model is saved
save is to save, load is to read, state_dict is the weight parameter

Guess you like

Origin blog.csdn.net/vibration_xu/article/details/125958183