Skip to content

Commit a36634d

Browse files
edit gpr_optimization notebook
1 parent df468c6 commit a36634d

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

machine-learning/gpr_optimization.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def apply_codecut_style(ax=None):
5353
return apply_codecut_style, plt
5454

5555

56+
@app.cell(hide_code=True)
57+
def _(mo):
58+
mo.md(r"""## Motivation""")
59+
return
60+
61+
5662
@app.cell
5763
def _():
5864
import random
@@ -86,6 +92,12 @@ def evaluate_model(model):
8692
return
8793

8894

95+
@app.cell(hide_code=True)
96+
def _(mo):
97+
mo.md(r"""## Setup""")
98+
return
99+
100+
89101
@app.cell
90102
def _():
91103
import numpy as np
@@ -119,11 +131,31 @@ def _(X, apply_codecut_style, plt, y):
119131
return
120132

121133

134+
@app.cell(hide_code=True)
135+
def _(mo):
136+
mo.md(r"""## Brute force hyperparameter search""")
137+
return
138+
139+
122140
@app.cell
123141
def _(black_box_function, np):
124142
X_grid = np.linspace(0, 2, 100).reshape(-1, 1)
125143
y_grid = black_box_function(X_grid)
126144
x_best = X_grid[np.argmax(y_grid)]
145+
return X_grid, x_best, y_grid
146+
147+
148+
@app.cell
149+
def _(X_grid, apply_codecut_style, black_box_function, plt, x_best, y_grid):
150+
plt.plot(X_grid, y_grid, "--", color="white", label="True function")
151+
plt.scatter(X_grid, y_grid, c="#E583B6", label="Evaluated Points")
152+
plt.scatter(x_best, black_box_function(x_best), c="#72BEFA", s=80, edgecolors="black", label="Best Point")
153+
154+
plt.title("Brute Force Search Over Full Range")
155+
plt.xlabel("x")
156+
plt.ylabel("f(x)")
157+
plt.legend()
158+
apply_codecut_style()
127159
return
128160

129161

@@ -148,6 +180,12 @@ def _():
148180
return
149181

150182

183+
@app.cell(hide_code=True)
184+
def _(mo):
185+
mo.md(r"""## Introducing Gaussian Process Regression""")
186+
return
187+
188+
151189
@app.cell
152190
def _(black_box_function, np):
153191
# Initial sample points (simulate prior evaluations)
@@ -186,6 +224,12 @@ def _(X, X_sample, apply_codecut_style, gpr, plt, y, y_sample):
186224
return
187225

188226

227+
@app.cell(hide_code=True)
228+
def _(mo):
229+
mo.md(r"""## Bayesian Optimization Step""")
230+
return
231+
232+
189233
@app.cell
190234
def _(np):
191235
from scipy.stats import norm
@@ -229,6 +273,12 @@ def _(
229273
return
230274

231275

276+
@app.cell(hide_code=True)
277+
def _(mo):
278+
mo.md(r"""## Hypeparameter Search Loop""")
279+
return
280+
281+
232282
@app.cell
233283
def _(X, black_box_function, expected_improvement, gpr, np):
234284
def bayesian_optimization(n_iter=10):
@@ -272,5 +322,11 @@ def _(X, X_opt, apply_codecut_style, black_box_function, plt, y_opt):
272322
return
273323

274324

325+
@app.cell
326+
def _():
327+
import marimo as mo
328+
return (mo,)
329+
330+
275331
if __name__ == "__main__":
276332
app.run()

public/machine-learning/gpr_optimization.html

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)