AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Hyperparameter Optimization: Bayesian Approach

📚 AutoML⏱️ 23 min read🎓 Grade 11
✍️ AI Computer Institute Editorial Team Updated: September 2026 CBSE-aligned · Peer-reviewed · 23 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

A bank's fraud-detection team is retraining the gradient-boosted model that flags suspicious UPI transactions in real time. Every candidate configuration — learning rate, tree depth, L2 regularization strength, number of boosting rounds — has to be trained end to end on a held-out slice of transaction history and scored on a validation set before anyone knows whether it is any good. One run takes about forty minutes on the team's GPU cluster and costs real money in compute time. A modest grid search over just three hyperparameters, five values each, is 5 × 5 × 5 = 125 configurations. At forty minutes apiece that is 125 × 40 = 5000 minutes, or roughly 83 hours of compute, most of it spent re-testing learning rates in ranges the very first few trials already showed were hopeless. Random search does a little better on average, but it still throws darts blind — it has no memory of what the last ten trials revealed about where the good region of the search space actually is. The question this chapter answers is: can the search itself learn, after every trial, exactly where to look next?

What exactly are we optimizing?

Keep two kinds of numbers apart. Parameters — the weights of the boosted trees, or the weight matrices of a neural net — are learned automatically by the training algorithm from data, via gradient descent or its tree-ensemble equivalent. Hyperparameters — learning rate, regularization strength, tree depth, batch size, number of layers — are fixed before training starts and control how the parameter-learning process itself behaves. Choosing them is a separate optimization problem, sitting one level above the one gradient descent solves.

Formally, let λ denote a vector of hyperparameters and L(λ) the validation loss obtained after training a model with those hyperparameters to convergence. The tuning problem is λ* = argminλ L(λ). What makes this hard is not the optimization itself but the nature of L: it is a black box (no formula, only input-output pairs), it is expensive to evaluate (one query means one full training run), and it is generally not differentiable with respect to λ — you cannot backpropagate through "train a model for 40 minutes" the way you backpropagate through a single forward pass, and some hyperparameters (tree depth, number of layers, choice of optimizer) are not even continuous. Any strategy that searches for λ* has to work with function values alone, and has to be economical about how many times it calls L.

Why grid and random search leave information on the table

Grid search evaluates every combination on a fixed lattice — cost grows as kd for k values per hyperparameter and d hyperparameters, which is why three hyperparameters at five values each already cost 83 hours above; add a fourth hyperparameter and the same five-value grid balloons to 625 trials, over 17 days of compute. Random search, shown by Bergstra and Bengio in 2012 to reliably outperform grid search at a fixed budget, samples configurations independently at random — it wastes less effort on unimportant dimensions, but it is still memoryless: trial number 50 is drawn without any regard for what trials 1 through 49 revealed. If the first ten random trials all show that learning rates above 0.05 blow up training, an eleventh random draw can still land at 0.08. Both methods treat every hyperparameter evaluation as an isolated, disposable event. The fraud-detection team's forty-minute training runs make that waste expensive in a very literal, budgeted-compute sense.

The Bayesian idea: model the unknown function itself

Bayesian optimization keeps a running belief about the shape of L(λ) across the whole search space, updates that belief after every trial, and uses it to choose the single most informative next trial. It has two moving parts. The first is a probabilistic surrogate model — almost always a Gaussian Process (GP) — that, for any candidate hyperparameter value, outputs not a single number but a distribution: a predictive mean μ(λ) ("our best guess of the validation loss here") and a predictive variance σ²(λ) ("how unsure we are"). The second is an acquisition function that converts (μ, σ) at every candidate into one score balancing exploitation (favor low predicted loss) against exploration (favor high uncertainty), and the next real trial is run at whichever candidate maximizes that score. After the result comes back, the GP is updated with the new data point and the loop repeats — this is called sequential model-based optimization.

A Gaussian Process is a probability distribution not over numbers but over functions. Before any trials are run, the GP prior says: here is a family of plausible smooth curves that L(λ) might trace out across the search space, and "smooth" is enforced by a kernel (covariance function) that measures how correlated the loss values at two hyperparameter settings should be, purely as a function of how far apart those settings are. The most common choice, the squared-exponential (RBF) kernel, is k(λ,λ') = σf² · exp(−(λ−λ')² / (2ℓ²)). It encodes a very reasonable inductive bias for hyperparameter tuning: a learning rate of 0.010 and 0.011 should give similar validation loss, while a learning rate of 0.001 and 0.1 need not. (the lengthscale) sets how quickly correlation decays with distance; σf² (the signal variance) sets the overall vertical scale of plausible variation in the loss. Once real trials come in, standard Gaussian-conditioning algebra collapses that broad prior into a sharp posterior: near an observed point the model becomes confident (small σ); far from every observed point, uncertainty relaxes back toward the prior's full width. The closed-form update, for observed inputs X and outputs y, prior mean m0, Gram matrix K of kernel values among observed points, observation-noise variance σn² (validation loss is itself noisy — a rerun with a different random seed gives a slightly different number), and kernel vector k* between a candidate and the observed points, is:

μ(λ*) = m0 + k*ᵀ (K + σn²I)⁻¹ (y − m0)
σ²(λ*) = k(λ*,λ*) − k*ᵀ (K + σn²I)⁻¹ k*

From belief to a decision: Expected Improvement

Knowing μ(λ) and σ(λ) everywhere does not by itself say where to test next. The most widely used acquisition function, Expected Improvement (EI), answers a precise question: at each candidate, what is the expected amount by which a trial there would beat the best validation loss observed so far, fmin? Treating the unknown true loss at that candidate as a random variable with the GP's own predictive distribution N(μ,σ²), integrating max(fmin − loss, 0) over that distribution has a closed form:

Z = (f_min − μ(x)) / σ(x)
EI(x) = (f_min − μ(x)) · Φ(Z) + σ(x) · φ(Z)      [σ(x) > 0]
EI(x) = max(f_min − μ(x), 0)                     [σ(x) = 0]

where Φ and φ are the standard normal CDF and PDF. The first term rewards a candidate whose predicted mean is already better than the current best — pure exploitation. The second term rewards uncertainty regardless of the mean — pure exploration, because a wide predictive distribution carries a real chance of a pleasant surprise even when the average guess looks unremarkable. As σ(x) → 0, Z → ±∞, and EI collapses to the deterministic improvement max(fmin − μ(x), 0) — with nothing left to be unsure about, "expected" improvement is just improvement.

GP surrogate + Expected Improvement over log₁₀(learning rate) Validation loss L(x) 0.30 0.45 0.60 f_min = 0.35 (best trial so far) Trial 1: LR=1e-3, loss=0.35 Trial 2: LR≈0.032, loss=0.55 x=−2: EI≈0.00008 x=−4: EI≈0.0173 → next trial Expected Improvement 0.02 0.01 0.00 −4 −3 −2 −1 log₁₀(learning rate) posterior mean μ(x) ±1σ uncertainty observed trial EI(x) argmax EI (next trial)

Worked example: choosing the third learning rate to try

Formalize the fraud-detection scenario as a one-dimensional search over x = log10(learning rate), with x ∈ [−4, −1], i.e. learning rate from 0.0001 to 0.1. Bayesian optimization always needs a small warm start of trials before the surrogate has anything to condition on; suppose two have already run:

x1 = −3.0  (LR = 1e-3)     y1 = 0.35 validation loss
x2 = −1.5  (LR ≈ 0.032)   y2 = 0.55 validation loss

Choose an RBF kernel with lengthscale ℓ=1, signal variance σf²=0.01 (a prior standard deviation of 0.1 loss units, matching the historical spread of validation loss on this task), observation noise σn²=0.0025 (std 0.05, from seed-to-seed validation variance), and prior mean m0=0.45 (the team's rough expectation before any trial-specific evidence). The residuals against the prior mean are r1 = 0.35 − 0.45 = −0.10 and r2 = 0.55 − 0.45 = +0.10.

The RBF part of the kernel between the two observed points is exp(−(−3−(−1.5))²/2) = exp(−1.125) = 0.32465, so with the noise term only on the diagonal:

K = | 0.0125     0.0032465 |     K⁻¹ = |  85.7868  −22.2807 |
    | 0.0032465  0.0125    |               | −22.2807   85.7868 |

K⁻¹r = [−10.8068, +10.8068]

Now compare two candidate next trials: x=−2 (LR≈0.01, sitting between the two known trials) and x=−4 (LR=1e-4, the unexplored edge of the search space). For each, the kernel vector to the observed points is k* = σf²·[exp(−(x−x1)²/2), exp(−(x−x2)²/2)], and the posterior mean/variance follow from the formulas above:

x = −2:  μ = 0.4798   σ = 0.0505
x = −4:  μ = 0.3892   σ = 0.0833

Both predictions still land above fmin=0.35 — neither candidate looks like a sure win. Feed each into the EI formula (Z = (fmin−μ)/σ):

x = −2:  Z = −2.572   Φ(Z) = 0.00506   φ(Z) = 0.01461
             exploit term = (0.35−0.4798)·0.00506 = −0.000657
             explore term = 0.0505·0.01461         = +0.000738
             EI(−2) = 0.0000808

x = −4:  Z = −0.470    Φ(Z) = 0.31905   φ(Z) = 0.35717
             exploit term = (0.35−0.3892)·0.31905 = −0.01251
             explore term = 0.0833·0.35717          = +0.02978
             EI(−4) = 0.01726

Notice something easy to miss: the "exploit" term is negative at both candidates, because the GP's smooth mean prediction has not yet pulled below fmin anywhere except right at the observed points. Every bit of positive Expected Improvement here — for both candidates — comes from the uncertainty term σ·φ(Z). What separates them is that x=−4 sits a full lengthscale (ℓ=1) beyond the nearest observed point on the side with no second neighbour to shrink its variance, while x=−2 sits only half a lengthscale from x2 — so its uncertainty (0.0833) ends up 65% larger than at x=−2 (0.0505), and its predicted mean also happens to be closer to fmin. Both effects compound: EI(−4) beats EI(−2) by a factor of about 213. Bayesian optimization schedules trial three at LR=1e-4 — the boundary of the domain the system has effectively never tested — not because it is confident that region is good, but because it cannot yet rule out that it might be, and the two known trials have already told it almost everything cheap to learn about the region between them.

The same computation, run in full generality (not hand-restricted to a 2×2 matrix), reproduces these exact numbers:

import math

def rbf_kernel(a, b, length_scale=1.0):
    return math.exp(-((a - b) ** 2) / (2 * length_scale ** 2))

def gp_posterior(x_star, X, y, prior_mean, signal_var, noise_var, length_scale=1.0):
    n = len(X)
    K = [[signal_var * rbf_kernel(X[i], X[j], length_scale)
          + (noise_var if i == j else 0.0)
          for j in range(n)] for i in range(n)]
    det = K[0][0] * K[1][1] - K[0][1] * K[1][0]
    K_inv = [[K[1][1] / det, -K[0][1] / det],
             [-K[1][0] / det, K[0][0] / det]]
    r = [y[i] - prior_mean for i in range(n)]
    k_star = [signal_var * rbf_kernel(x_star, X[i], length_scale) for i in range(n)]

    alpha = [sum(K_inv[i][j] * r[j] for j in range(n)) for i in range(n)]
    mean = prior_mean + sum(k_star[i] * alpha[i] for i in range(n))

    v = [sum(K_inv[i][j] * k_star[j] for j in range(n)) for i in range(n)]
    variance = signal_var - sum(k_star[i] * v[i] for i in range(n))
    return mean, max(variance, 0.0)

def expected_improvement(mean, variance, f_min):
    sigma = math.sqrt(variance)
    if sigma < 1e-12:
        return max(f_min - mean, 0.0)
    z = (f_min - mean) / sigma
    Phi = 0.5 * (1 + math.erf(z / math.sqrt(2)))
    phi = (1 / math.sqrt(2 * math.pi)) * math.exp(-z * z / 2)
    return (f_min - mean) * Phi + sigma * phi

X = [-3.0, -1.5]
y = [0.35, 0.55]
f_min = min(y)

for x_star in [-2.0, -4.0]:
    mean, variance = gp_posterior(x_star, X, y, prior_mean=0.45,
                                   signal_var=0.01, noise_var=0.0025)
    ei = expected_improvement(mean, variance, f_min)
    print(f"x={x_star:+.2f}  mean={mean:.4f}  sigma={math.sqrt(variance):.4f}  EI={ei:.6f}")

# x=-2.00  mean=0.4798  sigma=0.0505  EI=0.000081
# x=-4.00  mean=0.3892  sigma=0.0833  EI=0.017260

Common misconception: "Bayesian is always the smarter choice"

Students who first meet Bayesian optimization right after grid and random search tend to conclude it should replace both, always. It should not. Two costs are easy to forget. First, fitting the GP posterior requires inverting the Gram matrix K, an O(n³) operation in the number of trials so far — cheap at n=2, as above, but by n=500 that inversion can take longer than the model training it is supposed to be guiding. Second, the sequential loop — propose one point, wait for its result, update the posterior, propose the next — is inherently hard to parallelize; batch variants exist (like q-EI), but the basic algorithm proposes one trial at a time. The fraud-detection scenario in this chapter is exactly the regime where Bayesian optimization earns its keep: each trial is expensive (40 minutes, real compute cost) and the team has a modest, roughly ten-to-twenty-GPU budget, so choosing trials wisely matters more than running many of them at once.

Flip the scenario: a small logistic-regression baseline that trains in 0.3 seconds, three hyperparameters, and 200 idle GPUs sitting on the cluster. There, GP-based Bayesian optimization is the wrong tool — its sequential proposal loop cannot occupy more than a handful of those 200 GPUs at a time, and the overhead of fitting and inverting the GP after every trial dwarfs the 0.3-second training cost it is meant to save. Firing 200 random or grid configurations at once, in parallel, finds a good answer faster in wall-clock time. Standard GP-based Bayesian optimization also degrades past roughly fifteen to twenty hyperparameters, both because the O(n³) cost limits how many observations can be afforded and because a single smooth kernel struggles to model correlations across that many dimensions faithfully — production AutoML systems (Optuna, Hyperopt) often switch to a Tree-structured Parzen Estimator surrogate in high-dimensional or mixed continuous/categorical spaces for exactly this reason. The correct rule is not "Bayesian is smarter," it is "Bayesian optimization wins specifically when evaluations are expensive, the parallel budget is limited, and the search space is low-to-moderate dimensional" — check those three conditions before reaching for it.

Active recall

Attempt each question before reading its answer.

  1. Why can gradient descent tune a neural network's weights but not, in the same direct way, its own learning rate?
  2. In the worked example, the posterior mean at x2=−1.5 (an observed point) works out to about 0.523, not exactly the observed y2=0.55. Why doesn't the GP pass exactly through its own data?
  3. Qualitatively, what happens to σ(x) for a candidate exactly at the midpoint of two nearby observed trials, versus a candidate far outside every observed trial? Does higher σ always guarantee higher EI?
  4. A team is tuning a logistic regression model that trains in 0.3 seconds, has 3 hyperparameters, and has 200 idle GPUs available. Should they run GP-based Bayesian optimization? Justify with the two specific costs named in this chapter.
  5. In the EI formula, what does EI reduce to as σ(x) → 0 for a fixed μ(x) < fmin, and why does that make sense?
  6. Given a new candidate with μ=0.40, σ=0.06, and the same fmin=0.35 as the worked example, compute Z and EI. Is this candidate more or less attractive than either candidate evaluated in the worked example?

Worked answers

1. Weight updates use ∂Loss/∂weight, computable by backpropagation through a fixed computational graph. The learning rate controls the training procedure itself — computing ∂(validation loss)/∂(learning rate) would require differentiating through every step of that iterative procedure (and is for discrete hyperparameters like tree depth or number of layers). Bayesian optimization sidesteps this entirely: it only ever needs input-output pairs (λ, loss), no gradient with respect to λ at all.

2. Because σn²=0.0025 > 0 is included in the model, representing genuine observation noise (a rerun at the same hyperparameters would not give exactly 0.55 again, due to random seeds and batch ordering). With noise, the GP performs regression, not interpolation: it shrinks its estimate at x2 slightly toward the value implied by the nearby correlated point x1, rather than trusting y2 as noise-free ground truth. Set σn²=0 and the posterior mean at any observed point becomes exact.

3. Midway between two nearby observed points, the candidate is strongly correlated (via the kernel) with both, so K⁻¹ explains away most of its variance and σ shrinks toward the noise floor. Far outside every observed point, kernel correlation to all of them is near zero, so σ² reverts toward its maximum, the prior signal variance σf². Higher σ raises EI's potential via the σ·φ(Z) term, but does not guarantee it: if μ at that far point is extremely bad, Z becomes very negative in the unfavorable direction, and φ(Z) itself decays like e−Z²/2, so a wildly bad mean prediction can suppress EI even with a wide σ.

4. No. This is the cheap-evaluation, high-parallelism regime described in the misconception section. GP-based Bayesian optimization's sequential propose-then-update loop cannot use more than one (or a small batch) of the 200 available GPUs at a time, and its O(n³) matrix-inversion overhead per iteration is large relative to a 0.3-second training run. Launching 200 random or grid configurations in parallel finds a good configuration faster in wall-clock time.

5. As σ(x) → 0, Z = (fmin−μ(x))/σ(x) → +∞ (numerator is positive since μ < fmin), so Φ(Z) → 1 and σ(x)·φ(Z) → 0. EI collapses to fmin − μ(x), the plain deterministic improvement. This matches intuition: with zero uncertainty left, the "expected" improvement is simply the improvement.

6. Z = (0.35−0.40)/0.06 = −0.8333. Φ(−0.8333)≈0.2023, φ(−0.8333)≈0.2819. EI = (0.35−0.40)·0.2023 + 0.06·0.2819 = −0.01012 + 0.01691 = 0.00680. This candidate (EI≈0.0068) is far more attractive than x=−2 from the worked example (EI≈0.00008, about 85× smaller) but noticeably less attractive than x=−4 (EI≈0.0173, about 2.5× larger) — it sits in between because its predicted mean is much closer to fmin than either worked-example candidate, but its uncertainty is also modest.

Think About It

Think about this: How would you explain hyperparameter optimization: bayesian approach to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where hyperparameter optimization: bayesian approach is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting hyperparameter optimization: bayesian approach to at least 3 other topics you have studied.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind hyperparameter optimization: bayesian approach, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← Neural Architecture Search: Automating Network DesignAutoML: End-to-End Automation →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn