Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Iteration

Rome was not built in a day. Likewise, a network model cannot learn everything in a single training run.

In deep learning, model training follows a simple rule: using massive amounts of data, through thousands upon thousands of trials and errors, we approach the best model parameters in small, quick steps. This process of repetition and continuous improvement is called iteration.

More specifically, the process of feeding one data record to the network and finally updating the model parameters once is called one iteration. Each iteration consists of three steps. For our simplest network model:

  1. Forward propagation:

Forward propagation: the feature x goes through layer.forward() together with the parameters w and b to produce the prediction p
Figure: Forward propagation (model inference)
  1. Backpropagation:

Backpropagation: loss.backward() computes the error term d from the prediction p and the label y
Figure: Backpropagation (gradient computation)
  1. Parameter update:

Parameter update: optimizer.step() updates the parameters w and b from the error term d and the feature x
Figure: Parameter update

Tensor

Dataset

The massive data needed for model training is called the dataset.

Each record in the dataset is called a sample. Each sample consists of two parts: the feature and the label.


A dataset is usually split into two parts:

  • Training set: the larger part (for example, 80%), used for model training;

  • Test set: the smaller part (for example, 20%), used for model evaluation.

The purpose of splitting the dataset is to hold back some new data: the test set. With new data, we can verify whether the trained network model has really learned the patterns, rather than just memorizing some of the content. It is like a final exam: the teacher does not use the exercises you have already done as the exam paper, but a new set of questions to check whether you have truly mastered what you learned.

Ming provided the weather forecasts and sales records of the past four days, which serve as our training set. The data we used before serves as the test set.


A dataset usually provides the following functions:

  • load (load method): loads all the data;

  • train (training method): switches to model training mode;

  • eval (test method): switches to model testing mode;

  • all (all samples): returns all the samples;

  • __len__ (number of samples): returns the number of samples;

  • __getitem__(index) (a single sample): returns the sample for the given index (index).

Model

Loss function (mean squared error)

Optimizer (stochastic gradient descent)

To meet the new need for multiple iterations during model training, we upgrade the optimizer by adding a new function:

  • zero_grad (zeroing method): clears all the gradients before each iteration, getting ready for the next round of gradient computation.

💡 Tip: Why do we need to clear the gradients before every iteration? Look at the gradient functions of tensors such as the weights: the gradients they compute are accumulated. If we do not clear them before each iteration, the new gradients are added on top of the old gradients accumulated in previous iterations, and they keep growing.

Hyperparameters

Learning rate

Modeling

We need to create an instance of the dataset.

Training

Now our dataset has multiple training samples, so we need a loop: each time, take one sample from the dataset and complete one iteration.

Every iteration follows the same five steps, and this order will stay stable in later chapters:

  1. Zero the gradients (zero_grad): clear the gradients computed in the previous iteration;

  2. Forward propagation (forward): compute the prediction;

  3. Compute the loss (loss): get the loss;

  4. Backpropagation (backward): compute the gradients of all the nodes in the computational graph;

  5. Update the parameters (step): adjust the parameters with the gradients.

Remember this “five-step loop”. It is the core skeleton of almost all deep learning training code.

Inference

After training, we switch the dataset to test mode and evaluate the training result with new data (the test set).

prediction:	Tensor([[67.20489976]])

Evaluation

loss:	Tensor(9563.881631333801)

The evaluation shows that after 4 iterations the loss keeps decreasing, and the network model has improved again. But there is still a lot of room for improvement.

Exercises

  1. Does the order of the training data affect the result of model training? Try shuffling the training data (for example, reversing the order), train again, and observe what changes.