From da7ec70c803f311e94f15e11cbdd3ad7ebab8b2d Mon Sep 17 00:00:00 2001 From: khuyentran1401 Date: Fri, 30 May 2025 18:31:42 -0500 Subject: [PATCH 1/2] merge with main and move file --- .../gpr_optimization.py | 21 ++--- public/GPR_Optimization.html | 82 ------------------- public/index.html | 16 +++- public/llm/diffbot_llm.html | 82 +++++++++++++++++++ public/llm/lchain_deepseek.html | 82 +++++++++++++++++++ public/llm/lchain_ollama.html | 82 +++++++++++++++++++ public/temp.html | 82 ------------------- public/temp/temp.html | 82 ------------------- 8 files changed, 269 insertions(+), 260 deletions(-) rename GPR_Optimization.py => machine-learning/gpr_optimization.py (89%) delete mode 100644 public/GPR_Optimization.html create mode 100644 public/llm/diffbot_llm.html create mode 100644 public/llm/lchain_deepseek.html create mode 100644 public/llm/lchain_ollama.html delete mode 100644 public/temp.html delete mode 100644 public/temp/temp.html diff --git a/GPR_Optimization.py b/machine-learning/gpr_optimization.py similarity index 89% rename from GPR_Optimization.py rename to machine-learning/gpr_optimization.py index 7d6f814e..8723efc3 100644 --- a/GPR_Optimization.py +++ b/machine-learning/gpr_optimization.py @@ -17,10 +17,11 @@ @app.cell def _(): - import numpy as np import matplotlib.pyplot as plt + import numpy as np from sklearn.gaussian_process import GaussianProcessRegressor - from sklearn.gaussian_process.kernels import Matern, WhiteKernel, ConstantKernel as C + from sklearn.gaussian_process.kernels import ConstantKernel as C + from sklearn.gaussian_process.kernels import Matern, WhiteKernel def black_box_function(x): return - (np.sin(3*x) + 0.5 * x) @@ -81,10 +82,10 @@ def _(X, X_sample, gpr, plt, y, y_sample): # Plot the result plt.figure(figsize=(10, 5)) - plt.plot(X, y, 'k--', label="True function") - plt.plot(X, mu, 'b-', label="GPR mean") + plt.plot(X, y, "k--", label="True function") + plt.plot(X, mu, "b-", label="GPR mean") plt.fill_between(X.ravel(), mu - std, mu + std, alpha=0.3, label="Uncertainty") - plt.scatter(X_sample, y_sample, c='red', label="Samples") + plt.scatter(X_sample, y_sample, c="red", label="Samples") plt.legend() plt.title("Gaussian Process Fit") plt.xlabel("x") @@ -101,7 +102,7 @@ def expected_improvement(X, X_sample, y_sample, model, xi=0.01): mu, std = model.predict(X, return_std=True) mu_sample_opt = np.min(y_sample) - with np.errstate(divide='warn'): + with np.errstate(divide="warn"): imp = mu_sample_opt - mu - xi # because we are minimizing Z = imp / std ei = imp * norm.cdf(Z) + std * norm.pdf(Z) @@ -118,7 +119,7 @@ def _(X, X_sample, expected_improvement, gpr, np, plt, y_sample): plt.figure(figsize=(10, 4)) plt.plot(X, ei, label="Expected Improvement") - plt.axvline(X[np.argmax(ei)], color='r', linestyle='--', label="Next sample point") + plt.axvline(X[np.argmax(ei)], color="r", linestyle="--", label="Next sample point") plt.title("Acquisition Function (Expected Improvement)") plt.xlabel("x") plt.ylabel("EI(x)") @@ -135,7 +136,7 @@ def bayesian_optimization(n_iter=10): X_sample = np.array([[1.0], [2.5], [4.0]]) y_sample = black_box_function(X_sample) - for i in range(n_iter): + for _ in range(n_iter): gpr.fit(X_sample, y_sample) ei = expected_improvement(X, X_sample, y_sample, gpr) x_next = X[np.argmax(ei)].reshape(-1, 1) @@ -161,8 +162,8 @@ def _(bayesian_optimization): @app.cell def _(X, X_opt, black_box_function, plt, y_opt): # Plot final sampled points - plt.plot(X, black_box_function(X), 'k--', label="True function") - plt.scatter(X_opt, y_opt, c='red', label="Sampled Points") + plt.plot(X, black_box_function(X), "k--", label="True function") + plt.scatter(X_opt, y_opt, c="red", label="Sampled Points") plt.title("Bayesian Optimization with Gaussian Process") plt.xlabel("x") plt.ylabel("f(x)") diff --git a/public/GPR_Optimization.html b/public/GPR_Optimization.html deleted file mode 100644 index 6bb3dc95..00000000 --- a/public/GPR_Optimization.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - GPR Optimization - - - - - - -
- - - - - - diff --git a/public/index.html b/public/index.html index 6c8c1cd4..b5373537 100644 --- a/public/index.html +++ b/public/index.html @@ -86,12 +86,20 @@

pyspark parametrize

View the notebook
  • -

    pydantic ai examples

    - View the notebook +

    diffbot llm

    + View the notebook +
  • +
  • +

    lchain deepseek

    + View the notebook
  • -

    temp

    - View the notebook +

    lchain ollama

    + View the notebook +
  • +
  • +

    pydantic ai examples

    + View the notebook
  • diff --git a/public/llm/diffbot_llm.html b/public/llm/diffbot_llm.html new file mode 100644 index 00000000..b073304c --- /dev/null +++ b/public/llm/diffbot_llm.html @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diffbot llm + + + + + + +
    + + + + + + diff --git a/public/llm/lchain_deepseek.html b/public/llm/lchain_deepseek.html new file mode 100644 index 00000000..dc95501e --- /dev/null +++ b/public/llm/lchain_deepseek.html @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + lchain deepseek + + + + + + +
    + + + + + + diff --git a/public/llm/lchain_ollama.html b/public/llm/lchain_ollama.html new file mode 100644 index 00000000..a985a626 --- /dev/null +++ b/public/llm/lchain_ollama.html @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + lchain ollama + + + + + + +
    + + + + + + diff --git a/public/temp.html b/public/temp.html deleted file mode 100644 index 32f06346..00000000 --- a/public/temp.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - temp - - - - - - -
    - - - - - - diff --git a/public/temp/temp.html b/public/temp/temp.html deleted file mode 100644 index 82e61e0b..00000000 --- a/public/temp/temp.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - temp - - - - - - -
    - - - - - - From aafe688e94abb7768d5de2f1ee72a358a7ac7c98 Mon Sep 17 00:00:00 2001 From: khuyentran1401 Date: Sat, 31 May 2025 11:04:24 -0500 Subject: [PATCH 2/2] edit gpr_optimization notebook --- machine-learning/gpr_optimization.py | 67 +++++++++++++-- public/index.html | 4 + public/machine-learning/gpr_optimization.html | 82 +++++++++++++++++++ 3 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 public/machine-learning/gpr_optimization.html diff --git a/machine-learning/gpr_optimization.py b/machine-learning/gpr_optimization.py index 8723efc3..f7a8bf44 100644 --- a/machine-learning/gpr_optimization.py +++ b/machine-learning/gpr_optimization.py @@ -15,6 +15,39 @@ app = marimo.App(width="medium") +@app.cell +def _(): + import random + import time + + def train_model(epochs, batch_size): + # Simulate training by producing a score based on epochs and batch size + time.sleep(0.5) # 0.5 second delay to mimic compute time + random.seed(epochs + batch_size) + return {"score": random.uniform(0.7, 0.95)} + + def evaluate_model(model): + return model["score"] + + best_score = float("-inf") + best_params = None + + for epochs in [10, 50, 100]: + for batch_size in [16, 32, 64]: + print(f"Training model with epochs={epochs}, batch_size={batch_size}...") + model = train_model(epochs=epochs, batch_size=batch_size) + score = evaluate_model(model) + print(f"--> Score: {score:.4f}") + if score > best_score: + best_score = score + best_params = {"epochs": epochs, "batch_size": batch_size} + print(f"--> New best score! Updated best_params: {best_params}") + + print("Best score:", best_score) + print("Best params:", best_params) + return (time,) + + @app.cell def _(): import matplotlib.pyplot as plt @@ -22,18 +55,14 @@ def _(): from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.gaussian_process.kernels import ConstantKernel as C from sklearn.gaussian_process.kernels import Matern, WhiteKernel + return C, GaussianProcessRegressor, Matern, WhiteKernel, np, plt + +@app.cell +def _(np): def black_box_function(x): return - (np.sin(3*x) + 0.5 * x) - return ( - C, - GaussianProcessRegressor, - Matern, - WhiteKernel, - black_box_function, - np, - plt, - ) + return (black_box_function,) @app.cell @@ -56,6 +85,26 @@ def _(black_box_function, np): return +@app.cell +def _(black_box_function, np, time): + def train(epochs): + time.sleep(0.1) # Simulate a slow training step + return black_box_function(epochs) + + search_space = np.linspace(0, 5, 1000) + results = [] + + start = time.time() + for x in search_space: + loss = train(x) + results.append((x, loss)) + end = time.time() + + print("Best x:", search_space[np.argmin([r[1] for r in results])]) + print("Time taken:", round(end - start, 2), "seconds") + return + + @app.cell def _(black_box_function, np): # Initial sample points (simulate prior evaluations) diff --git a/public/index.html b/public/index.html index b5373537..e4384592 100644 --- a/public/index.html +++ b/public/index.html @@ -101,6 +101,10 @@

    lchain ollama

    pydantic ai examples

    View the notebook +
  • +

    gpr optimization

    + View the notebook +
  • \ No newline at end of file diff --git a/public/machine-learning/gpr_optimization.html b/public/machine-learning/gpr_optimization.html new file mode 100644 index 00000000..a2316ee2 --- /dev/null +++ b/public/machine-learning/gpr_optimization.html @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + gpr optimization + + + + + + +
    + + + + + +