Spaces:
Sleeping
Sleeping
Commit
·
54abb16
1
Parent(s):
b9a7cc9
created gradio app
Browse files- app.py +105 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import time
|
| 6 |
+
from tqdm.notebook import tqdm_notebook
|
| 7 |
+
import plotly.express as px
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_error(data, a, b):
|
| 14 |
+
n = data.size()[0]
|
| 15 |
+
y_pred = a * data[:,0] + b
|
| 16 |
+
#return (1 / (2 * n)) * ((y_pred - data[:,-1]) ** 2).sum().item()
|
| 17 |
+
return (1 / 2) * torch.mean((y_pred - data[:,-1]) ** 2).item()
|
| 18 |
+
|
| 19 |
+
def get_grads(data, a, b, alpha):
|
| 20 |
+
n = data.size()[0]
|
| 21 |
+
y_pred = a * data[:,0] + b
|
| 22 |
+
#grad_a = (alpha / n) * ((y_pred - data[:,-1])* data[:,0]).sum().item()
|
| 23 |
+
grad_a = alpha * torch.mean((y_pred - data[:,-1])* data[:,0]).item()
|
| 24 |
+
#grad_b = (alpha / n) * (y_pred - data[:,-1]).sum().item()
|
| 25 |
+
grad_b = alpha * torch.mean((y_pred - data[:,-1])* data[:,0]).item()
|
| 26 |
+
return (grad_a, grad_b)
|
| 27 |
+
|
| 28 |
+
def train(data, alpha=0.0001, epochs=500, test_data=[1,2,3]):
|
| 29 |
+
tensors = torch.from_numpy(data.astype(np.float64))
|
| 30 |
+
print(f"dataset:{tensors}, size:{tensors.size()}")
|
| 31 |
+
a = 1
|
| 32 |
+
b = 1
|
| 33 |
+
errors = []
|
| 34 |
+
updated_a = []
|
| 35 |
+
updated_b = []
|
| 36 |
+
for i in tqdm_notebook(range(int(epochs)), desc=f"Epoch: "):
|
| 37 |
+
errors.append(get_error(tensors, a, b))
|
| 38 |
+
grad_a_temp, grad_b_temp = get_grads(tensors, a, b, alpha)
|
| 39 |
+
updated_a.append(a)
|
| 40 |
+
updated_b.append(b)
|
| 41 |
+
a -= grad_a_temp
|
| 42 |
+
b -= grad_b_temp
|
| 43 |
+
|
| 44 |
+
#print(f"Epoch: {i} -> Theta0: {updated_b[i]} Theta1: {updated_a[i]}, Error: {errors[i]} \n")
|
| 45 |
+
y_pred = a * tensors[:,0] + b
|
| 46 |
+
fig1 = px.scatter(x=tensors[:, 0], y=tensors[:, -1])
|
| 47 |
+
fig2 = px.line(x=tensors[:, 0], y=y_pred)
|
| 48 |
+
fig3 = go.Figure(data=fig1.data + fig2.data)
|
| 49 |
+
fig3.update_layout(
|
| 50 |
+
#title="Best Fit Line",
|
| 51 |
+
xaxis_title="X",
|
| 52 |
+
yaxis_title="Y",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
fig4 = px.line(errors)
|
| 56 |
+
fig4.update_traces(line_color='orange')
|
| 57 |
+
fig4.update_layout(
|
| 58 |
+
#title="Learning Curve",
|
| 59 |
+
xaxis_title="Epoch",
|
| 60 |
+
yaxis_title="Error",
|
| 61 |
+
showlegend=False
|
| 62 |
+
)
|
| 63 |
+
y_pred_test = a * test_data.astype(np.float64) + b
|
| 64 |
+
return f"Best Model: {round(updated_a[-1],3)} * X + {round(updated_b[-1],3)}",fig3, fig4, np.concatenate((test_data.astype(np.float64), y_pred_test), axis=1)
|
| 65 |
+
|
| 66 |
+
# gradio app
|
| 67 |
+
|
| 68 |
+
input_elements = [
|
| 69 |
+
gr.Numpy(
|
| 70 |
+
value=[[40,9],[30,8.5],[25,8],[20,7],[10,6],[5,8]],
|
| 71 |
+
datatype="number",
|
| 72 |
+
row_count=(6,"dynamic"),
|
| 73 |
+
col_count=(2, "fixed"),
|
| 74 |
+
label="Dataset",
|
| 75 |
+
interactive=True
|
| 76 |
+
),
|
| 77 |
+
gr.Number(label="Learning Rate", value=0.0001, interactive=True),
|
| 78 |
+
gr.Number(label="Number of epochs", value=500, interactive=True),
|
| 79 |
+
gr.Numpy(
|
| 80 |
+
value=[[10],[20],[30]],
|
| 81 |
+
datatype="number",
|
| 82 |
+
row_count=3,
|
| 83 |
+
col_count=(1,"fixed"),
|
| 84 |
+
label="Test Dataset",
|
| 85 |
+
interactive=True
|
| 86 |
+
)
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
output_elements = [
|
| 90 |
+
gr.Textbox(label="Generated Model", placeholder="Model Equation: a * X + b"),
|
| 91 |
+
gr.Plot(label="Best Fit Line"),
|
| 92 |
+
gr.Plot(label="Learning Curve"),
|
| 93 |
+
gr.Numpy(label="Model Predictions")
|
| 94 |
+
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
app = gr.Interface(
|
| 98 |
+
title="Linear Regression using Pytorch",
|
| 99 |
+
#description="a simple app to demonstrate linear regression",
|
| 100 |
+
fn=train,
|
| 101 |
+
inputs=input_elements,
|
| 102 |
+
outputs=output_elements,
|
| 103 |
+
allow_flagging="never"
|
| 104 |
+
)
|
| 105 |
+
app.launch(debug=True, share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
torch
|
| 3 |
+
matplotlib
|
| 4 |
+
plotly
|
| 5 |
+
gradio
|
| 6 |
+
seaborn
|
| 7 |
+
tqdm
|