Skip to content

Commit aafe688

Browse files
edit gpr_optimization notebook
1 parent da7ec70 commit aafe688

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed

machine-learning/gpr_optimization.py

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,54 @@
1515
app = marimo.App(width="medium")
1616

1717

18+
@app.cell
19+
def _():
20+
import random
21+
import time
22+
23+
def train_model(epochs, batch_size):
24+
# Simulate training by producing a score based on epochs and batch size
25+
time.sleep(0.5) # 0.5 second delay to mimic compute time
26+
random.seed(epochs + batch_size)
27+
return {"score": random.uniform(0.7, 0.95)}
28+
29+
def evaluate_model(model):
30+
return model["score"]
31+
32+
best_score = float("-inf")
33+
best_params = None
34+
35+
for epochs in [10, 50, 100]:
36+
for batch_size in [16, 32, 64]:
37+
print(f"Training model with epochs={epochs}, batch_size={batch_size}...")
38+
model = train_model(epochs=epochs, batch_size=batch_size)
39+
score = evaluate_model(model)
40+
print(f"--> Score: {score:.4f}")
41+
if score > best_score:
42+
best_score = score
43+
best_params = {"epochs": epochs, "batch_size": batch_size}
44+
print(f"--> New best score! Updated best_params: {best_params}")
45+
46+
print("Best score:", best_score)
47+
print("Best params:", best_params)
48+
return (time,)
49+
50+
1851
@app.cell
1952
def _():
2053
import matplotlib.pyplot as plt
2154
import numpy as np
2255
from sklearn.gaussian_process import GaussianProcessRegressor
2356
from sklearn.gaussian_process.kernels import ConstantKernel as C
2457
from sklearn.gaussian_process.kernels import Matern, WhiteKernel
58+
return C, GaussianProcessRegressor, Matern, WhiteKernel, np, plt
59+
2560

61+
@app.cell
62+
def _(np):
2663
def black_box_function(x):
2764
return - (np.sin(3*x) + 0.5 * x)
28-
return (
29-
C,
30-
GaussianProcessRegressor,
31-
Matern,
32-
WhiteKernel,
33-
black_box_function,
34-
np,
35-
plt,
36-
)
65+
return (black_box_function,)
3766

3867

3968
@app.cell
@@ -56,6 +85,26 @@ def _(black_box_function, np):
5685
return
5786

5887

88+
@app.cell
89+
def _(black_box_function, np, time):
90+
def train(epochs):
91+
time.sleep(0.1) # Simulate a slow training step
92+
return black_box_function(epochs)
93+
94+
search_space = np.linspace(0, 5, 1000)
95+
results = []
96+
97+
start = time.time()
98+
for x in search_space:
99+
loss = train(x)
100+
results.append((x, loss))
101+
end = time.time()
102+
103+
print("Best x:", search_space[np.argmin([r[1] for r in results])])
104+
print("Time taken:", round(end - start, 2), "seconds")
105+
return
106+
107+
59108
@app.cell
60109
def _(black_box_function, np):
61110
# Initial sample points (simulate prior evaluations)

public/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ <h2 class="notebook-title">lchain ollama</h2>
101101
<h2 class="notebook-title">pydantic ai examples</h2>
102102
<a href="llm/pydantic_ai_examples.html" class="notebook-link">View the notebook</a>
103103
</li>
104+
<li class="notebook-item">
105+
<h2 class="notebook-title">gpr optimization</h2>
106+
<a href="machine-learning/gpr_optimization.html" class="notebook-link">View the notebook</a>
107+
</li>
104108
</ul>
105109
</body>
106110
</html>

public/machine-learning/gpr_optimization.html

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

0 commit comments

Comments
 (0)