Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,13 @@ def homework04_solution(theta0, theta1, theta2, learning_rate):
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
-
print("check1")
|
| 6 |
print(theta0, theta1, theta2, learning_rate)
|
| 7 |
|
| 8 |
def linear_predict(b0, b1, b2, x1, x2):
|
| 9 |
y_hat = b0 + b1*x1 + b2*x2
|
| 10 |
return y_hat
|
| 11 |
|
| 12 |
-
|
| 13 |
def get_linear_results(data, theta0, theta1, theta2):
|
| 14 |
## (2) make linear prediction
|
| 15 |
y_hat_list = []
|
|
@@ -37,14 +36,12 @@ def homework04_solution(theta0, theta1, theta2, learning_rate):
|
|
| 37 |
data['(y-y_hat)^2'] = data['y-y_hat']*data['y-y_hat']
|
| 38 |
return data, theta0_grad, theta1_grad, theta2_grad
|
| 39 |
|
| 40 |
-
print("check3")
|
| 41 |
## (1) load data
|
| 42 |
X = np.array([[15,20], [30,16], [12,6.5], [13,20], [18,18]])
|
| 43 |
y = [4.9, 5.8,6.5,7.3,7.2]
|
| 44 |
data = pd.DataFrame(X, columns=['X1','X2'])
|
| 45 |
data['y'] = y
|
| 46 |
|
| 47 |
-
print("check4")
|
| 48 |
## (2) get regression table, gradients
|
| 49 |
data, theta0_grad, theta1_grad, theta2_grad = get_linear_results(data, theta0, theta1, theta2)
|
| 50 |
|
|
@@ -55,25 +52,21 @@ def homework04_solution(theta0, theta1, theta2, learning_rate):
|
|
| 55 |
|
| 56 |
data_t = data_t.round(2)
|
| 57 |
|
| 58 |
-
print("check5")
|
| 59 |
data_t.insert(loc=0, column='Name', value=['X1', 'X2', 'y', 'y_hat', 'y-y_hat', '(y-y_hat)^2'])
|
| 60 |
|
| 61 |
-
|
| 62 |
-
print(data_t.columns)
|
| 63 |
|
| 64 |
|
| 65 |
### (4) summarize gradient results for question 3b
|
| 66 |
|
| 67 |
MSE = data['(y-y_hat)^2'].mean()
|
| 68 |
|
| 69 |
-
print("check6")
|
| 70 |
q3_mse = MSE
|
| 71 |
|
| 72 |
### summarize gradient results for question 4 (2)
|
| 73 |
|
| 74 |
### update parameter using gradient descent 4 (3)
|
| 75 |
|
| 76 |
-
print("check7")
|
| 77 |
theta0_new = theta0 - learning_rate*theta0_grad
|
| 78 |
theta1_new = theta1 - learning_rate*theta1_grad
|
| 79 |
theta2_new = theta2 - learning_rate*theta2_grad
|
|
@@ -98,21 +91,20 @@ import gradio as gr
|
|
| 98 |
|
| 99 |
|
| 100 |
### configure inputs
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
print("check8")
|
| 107 |
-
set_theta0 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 108 |
-
set_theta1 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 109 |
-
set_theta2 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 110 |
-
set_ita = gr.Slider(minimum = 0, maximum=10, step=0.01, value=0.01)
|
| 111 |
|
| 112 |
|
| 113 |
-
print("check9")
|
| 114 |
### configure outputs
|
| 115 |
-
set_output_q3a = gr.Dataframe(type='pandas',
|
| 116 |
set_output_q3b = gr.Textbox(label ='Question: What\'s Initial MSE loss')
|
| 117 |
set_output_q4a0 = gr.Textbox(label ='Question: What\'s theta0_grad')
|
| 118 |
set_output_q4a1 = gr.Textbox(label ='Question: What\'s theta1_grad')
|
|
@@ -125,7 +117,6 @@ set_output_q4b2 = gr.Textbox(label ='Question: What\'s theta2_new: updated by gr
|
|
| 125 |
set_output_q4b4 = gr.Textbox(label ='Question: What\'s New MSE after update the parameters using gradient descent')
|
| 126 |
|
| 127 |
|
| 128 |
-
print("check10")
|
| 129 |
### configure Gradio
|
| 130 |
interface = gr.Interface(fn=homework04_solution,
|
| 131 |
inputs=[set_theta0, set_theta1, set_theta2, set_ita],
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
|
|
|
|
| 5 |
print(theta0, theta1, theta2, learning_rate)
|
| 6 |
|
| 7 |
def linear_predict(b0, b1, b2, x1, x2):
|
| 8 |
y_hat = b0 + b1*x1 + b2*x2
|
| 9 |
return y_hat
|
| 10 |
|
| 11 |
+
|
| 12 |
def get_linear_results(data, theta0, theta1, theta2):
|
| 13 |
## (2) make linear prediction
|
| 14 |
y_hat_list = []
|
|
|
|
| 36 |
data['(y-y_hat)^2'] = data['y-y_hat']*data['y-y_hat']
|
| 37 |
return data, theta0_grad, theta1_grad, theta2_grad
|
| 38 |
|
|
|
|
| 39 |
## (1) load data
|
| 40 |
X = np.array([[15,20], [30,16], [12,6.5], [13,20], [18,18]])
|
| 41 |
y = [4.9, 5.8,6.5,7.3,7.2]
|
| 42 |
data = pd.DataFrame(X, columns=['X1','X2'])
|
| 43 |
data['y'] = y
|
| 44 |
|
|
|
|
| 45 |
## (2) get regression table, gradients
|
| 46 |
data, theta0_grad, theta1_grad, theta2_grad = get_linear_results(data, theta0, theta1, theta2)
|
| 47 |
|
|
|
|
| 52 |
|
| 53 |
data_t = data_t.round(2)
|
| 54 |
|
|
|
|
| 55 |
data_t.insert(loc=0, column='Name', value=['X1', 'X2', 'y', 'y_hat', 'y-y_hat', '(y-y_hat)^2'])
|
| 56 |
|
| 57 |
+
data_t.columns = ['Name', '0', '1', '2', '3', '4']
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
### (4) summarize gradient results for question 3b
|
| 61 |
|
| 62 |
MSE = data['(y-y_hat)^2'].mean()
|
| 63 |
|
|
|
|
| 64 |
q3_mse = MSE
|
| 65 |
|
| 66 |
### summarize gradient results for question 4 (2)
|
| 67 |
|
| 68 |
### update parameter using gradient descent 4 (3)
|
| 69 |
|
|
|
|
| 70 |
theta0_new = theta0 - learning_rate*theta0_grad
|
| 71 |
theta1_new = theta1 - learning_rate*theta1_grad
|
| 72 |
theta2_new = theta2 - learning_rate*theta2_grad
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
### configure inputs
|
| 94 |
+
set_theta0 = gr.Number(value=0.1)
|
| 95 |
+
set_theta1 = gr.Number(value=0.1)
|
| 96 |
+
set_theta2 = gr.Number(value=0.1)
|
| 97 |
+
set_ita = gr.Number(value=0.1)
|
| 98 |
|
| 99 |
+
#print("check8")
|
| 100 |
+
#set_theta0 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 101 |
+
#set_theta1 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 102 |
+
#set_theta2 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
|
| 103 |
+
#set_ita = gr.Slider(minimum = 0, maximum=10, step=0.01, value=0.01)
|
| 104 |
|
| 105 |
|
|
|
|
| 106 |
### configure outputs
|
| 107 |
+
set_output_q3a = gr.Dataframe(type='pandas', label ='Question 3a')
|
| 108 |
set_output_q3b = gr.Textbox(label ='Question: What\'s Initial MSE loss')
|
| 109 |
set_output_q4a0 = gr.Textbox(label ='Question: What\'s theta0_grad')
|
| 110 |
set_output_q4a1 = gr.Textbox(label ='Question: What\'s theta1_grad')
|
|
|
|
| 117 |
set_output_q4b4 = gr.Textbox(label ='Question: What\'s New MSE after update the parameters using gradient descent')
|
| 118 |
|
| 119 |
|
|
|
|
| 120 |
### configure Gradio
|
| 121 |
interface = gr.Interface(fn=homework04_solution,
|
| 122 |
inputs=[set_theta0, set_theta1, set_theta2, set_ita],
|