Initial commit: Gradio regression visualization
Browse files- LICENSE +21 -0
- linear_regression_gradio.py +60 -0
- requirements.txt +6 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Tanittha
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
linear_regression_gradio.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.linear_model import LinearRegression
|
| 5 |
+
|
| 6 |
+
# Synthetic dataset
|
| 7 |
+
np.random.seed(42)
|
| 8 |
+
x = np.linspace(0, 5, 100).reshape(-1, 1)
|
| 9 |
+
y = 2 * x + np.random.normal(0, 0.5, x.shape)
|
| 10 |
+
|
| 11 |
+
# Explicit solution model
|
| 12 |
+
lin_reg = LinearRegression()
|
| 13 |
+
lin_reg.fit(x, y)
|
| 14 |
+
|
| 15 |
+
def visualize_model(lr=0.01, epochs=50):
|
| 16 |
+
# Gradient Descent training
|
| 17 |
+
w, b = 0.0, 0.0
|
| 18 |
+
n = len(x)
|
| 19 |
+
losses = []
|
| 20 |
+
for epoch in range(epochs):
|
| 21 |
+
y_pred = w * x + b
|
| 22 |
+
loss = np.mean((y - y_pred)**2)
|
| 23 |
+
losses.append(loss)
|
| 24 |
+
dw = (-2/n) * np.sum((y - y_pred) * x)
|
| 25 |
+
db = (-2/n) * np.sum(y - y_pred)
|
| 26 |
+
w -= lr * dw
|
| 27 |
+
b -= lr * db
|
| 28 |
+
|
| 29 |
+
# Plot predictions + loss curve
|
| 30 |
+
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
|
| 31 |
+
|
| 32 |
+
# Left: data + predictions
|
| 33 |
+
axs[0].scatter(x, y, label="Data", alpha=0.6)
|
| 34 |
+
axs[0].plot(x, lin_reg.predict(x), label="Explicit Solution", color="green")
|
| 35 |
+
axs[0].plot(x, w * x + b, label="Gradient Descent", color="red")
|
| 36 |
+
axs[0].legend()
|
| 37 |
+
axs[0].set_title("Model Predictions")
|
| 38 |
+
|
| 39 |
+
# Right: loss curve
|
| 40 |
+
axs[1].plot(range(epochs), losses, color="blue")
|
| 41 |
+
axs[1].set_title("Loss vs Epoch")
|
| 42 |
+
axs[1].set_xlabel("Epoch")
|
| 43 |
+
axs[1].set_ylabel("Loss")
|
| 44 |
+
|
| 45 |
+
return fig
|
| 46 |
+
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=visualize_model,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Slider(0.001, 0.1, value=0.01, label="Learning Rate"),
|
| 51 |
+
gr.Slider(10, 200, value=50, step=10, label="Epochs")
|
| 52 |
+
],
|
| 53 |
+
outputs=gr.Plot(),
|
| 54 |
+
title="Linear Regression Visualization",
|
| 55 |
+
description="Adjust learning rate and epochs to see Gradient Descent vs Explicit Solution."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 60 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
matplotlib
|
| 3 |
+
scikit-learn
|
| 4 |
+
mlflow
|
| 5 |
+
wandb
|
| 6 |
+
gradio
|