DEV Community

Cover image for Pytorch for Neural Networks Part 10: Completing Training and Verifying the Results
Rijul Rajesh
Rijul Rajesh

Posted on

Pytorch for Neural Networks Part 10: Completing Training and Verifying the Results

In the previous article, we completed the implementation for optimizing final_bias using gradient descent.

In this final part, we will run the code and observe what happens before and after optimization.


Before Optimization

When we first run the code, the value of final_bias is:

0.0
Enter fullscreen mode Exit fullscreen mode

This is the starting value before any optimization takes place.

If we graph the model at this stage, we get the following result:

As we can see, the model does not fit the training data very well.

The predictions are far from the values we want, which means the loss is relatively large.


Watching Gradient Descent Update the Bias

As the training loop runs, we can observe final_bias changing at each step of gradient descent.

With every epoch:

  1. The model calculates the loss.
  2. Backpropagation computes the derivatives.
  3. The optimizer updates final_bias.
  4. The process repeats.

Over time, the model gradually moves toward a better value for final_bias.


After Optimization

After 34 steps, the total loss becomes very small.

At this point, the optimization process stops.

The final optimized value becomes:

-16.0019
Enter fullscreen mode Exit fullscreen mode


Verifying the Result

Finally, we can verify that the optimized model now fits the training data correctly by graphing the updated outputs.

Using the graphing code from earlier, we get:

Now the model fits the training data much better.

This shows that gradient descent successfully optimized final_bias, allowing the neural network to learn the correct relationship from the training data.

So that is it for this small introduction to building and training neural networks with PyTorch.

We will explore more areas related to coding neural networks in the coming articles.


AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

Give it a ⭐ star on Github

Top comments (0)