๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #63

Gradient Descent (Intro)

This note introduces gradient descent as the algorithm that actually uses backpropagation's gradients to improve the network โ€” closing the loop between "measuring error" and "getting better." The full Optimization category covers its many variants (SGD, Adam, etc.) in depth.

The Core Update Rule

\[ \mathbf{w} \leftarrow \mathbf{w} - \eta \nabla L(\mathbf{w}) \]

\(\eta\) (eta) is the learning rate โ€” a small positive hyperparameter controlling how large a step to take. \(\nabla L(\mathbf{w})\) is the gradient computed by backpropagation, pointing toward steepest loss increase (see Gradient) โ€” subtracting it moves the weights toward decreasing loss.

Numerical Example

A single weight \(w=2.0\), gradient \(\nabla L = 3.0\), learning rate \(\eta=0.1\):

\[ w_{\text{new}} = 2.0 - (0.1)(3.0) = 2.0 - 0.3 = 1.7 \]

The weight moved from 2.0 to 1.7 โ€” a small step in the direction that reduces the loss, based on the current gradient. Repeating this process many times, across every weight in the network simultaneously, is what "training" means.

Why the Learning Rate Matters So Much

Too Small converges too slowly Good converges smoothly Too Large overshoots, may diverge

Too small a learning rate wastes training time; too large a learning rate can overshoot the minimum entirely and cause the loss to diverge.

Code โ€” A Complete Minimal Training Step

import torch
import torch.nn as nn

model = nn.Linear(1, 1)
x = torch.tensor([[2.0]])
y_true = torch.tensor([[10.0]])
learning_rate = 0.01

y_pred = model(x)                       # forward propagation
loss = nn.MSELoss()(y_pred, y_true)      # loss function
loss.backward()                           # backpropagation -- computes gradients

with torch.no_grad():                     # gradient descent update -- no gradient tracking needed here
    for param in model.parameters():
        param -= learning_rate * param.grad
        param.grad.zero_()                 # reset gradient for the next step

In practice, you'll almost always use an optimizer object (torch.optim.SGD, torch.optim.Adam, ...) to handle this update loop instead of writing it manually โ€” covered in the PyTorch category โ€” but this is exactly what those optimizers do underneath.

The Complete Training Flow, Now Fully Assembled

This closes the loop first previewed in Why Calculus for Neural Networks: Input โ†’ Weighted Sum โ†’ Activation โ†’ Prediction (forward propagation) โ†’ Loss (loss function) โ†’ Gradient (backpropagation) โ†’ Weight Update (gradient descent) โ€” repeated over and over, across many batches and epochs, is the entirety of how a neural network learns.

Common Mistakes

  • Setting the learning rate too high "to train faster" โ€” this frequently causes the loss to oscillate or diverge entirely instead, as shown in the diagram above.
  • Forgetting to zero out gradients between steps (as flagged already in Gradient Vector) โ€” this silently accumulates stale gradients from previous steps into the current update.

Interview Relevance

Q: "What happens if the learning rate is set far too high?" Each weight update overshoots the loss surface's minimum by a large margin. Instead of smoothly converging, the loss can oscillate wildly or increase without bound (diverge) โ€” the optimizer keeps "jumping over" the minimum in a self-reinforcing way rather than settling into it.

Practice Question

A weight is currently 5.0, with gradient 2.0. Using learning rate 0.5, compute the updated weight. What would happen to the update's size if the learning rate were 5.0 instead โ€” is that likely to help or hurt convergence?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Gradient Descent (Intro) โ€“ FAQs

Quick answers about learning Gradient Descent (Intro) in Deep Learning.

This free note from CodingNow 2.0 explains Gradient Descent (Intro) in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Gradient Descent (Intro), is 100% free with no signup required.
With focused practice, most students grasp Gradient Descent (Intro) in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now