15
15
app = marimo .App (width = "medium" )
16
16
17
17
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
+
18
51
@app .cell
19
52
def _ ():
20
53
import matplotlib .pyplot as plt
21
54
import numpy as np
22
55
from sklearn .gaussian_process import GaussianProcessRegressor
23
56
from sklearn .gaussian_process .kernels import ConstantKernel as C
24
57
from sklearn .gaussian_process .kernels import Matern , WhiteKernel
58
+ return C , GaussianProcessRegressor , Matern , WhiteKernel , np , plt
59
+
25
60
61
+ @app .cell
62
+ def _ (np ):
26
63
def black_box_function (x ):
27
64
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 ,)
37
66
38
67
39
68
@app .cell
@@ -56,6 +85,26 @@ def _(black_box_function, np):
56
85
return
57
86
58
87
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
+
59
108
@app .cell
60
109
def _ (black_box_function , np ):
61
110
# Initial sample points (simulate prior evaluations)
0 commit comments