Spaces:
Build error
Build error
File size: 4,487 Bytes
a38e034 7565946 a38e034 792c809 a38e034 97ef1c3 792c809 a38e034 792c809 ee01add 7565946 792c809 a38e034 792c809 556b56a a38e034 556b56a a38e034 556b56a a38e034 7565946 a38e034 b128848 a38e034 033b3d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | def homework04_solution(theta0, theta1, theta2, learning_rate):
import numpy as np
import pandas as pd
print(theta0, theta1, theta2, learning_rate)
def linear_predict(b0, b1, b2, x1, x2):
y_hat = b0 + b1*x1 + b2*x2
return y_hat
def get_linear_results(data, theta0, theta1, theta2):
## (2) make linear prediction
y_hat_list = []
theta0_grad = 0
theta1_grad = 0
theta2_grad = 0
for i in range(len(data)):
x1 = data.iloc[i,0]
x2 = data.iloc[i,1]
y = data.iloc[i,2]
y_hat = linear_predict(theta0, theta1, theta2, x1, x2)
y_hat_list.append(y_hat)
## (3) calculate gradients
theta0_grad = theta0_grad - 2/len(data)*(y - theta0 - theta1*x1 - theta2*x2)*1.0
theta1_grad = theta1_grad - 2/len(data)*(y - theta0 - theta1*x1 - theta2*x2)*x1
theta2_grad = theta2_grad - 2/len(data)*(y - theta0 - theta1*x1 - theta2*x2)*x2
data['y_hat'] = y_hat_list
data['y-y_hat'] = data['y'] - data['y_hat']
data['(y-y_hat)^2'] = data['y-y_hat']*data['y-y_hat']
return data, theta0_grad, theta1_grad, theta2_grad
## (1) load data
X = np.array([[15,20], [30,16], [12,6.5], [13,20], [18,18]])
y = [4.9, 5.8,6.5,7.3,7.2]
data = pd.DataFrame(X, columns=['X1','X2'])
data['y'] = y
## (2) get regression table, gradients
data, theta0_grad, theta1_grad, theta2_grad = get_linear_results(data, theta0, theta1, theta2)
### (3) summarize gradient results for question 3a
data_t = data.T
data_t = data_t.round(2)
data_t.insert(loc=0, column='Name', value=['X1', 'X2', 'y', 'y_hat', 'y-y_hat', '(y-y_hat)^2'])
data_t.columns = ['Name', '0', '1', '2', '3', '4']
### (4) summarize gradient results for question 3b
MSE = data['(y-y_hat)^2'].mean()
q3_mse = MSE
### summarize gradient results for question 4 (2)
### update parameter using gradient descent 4 (3)
theta0_new = theta0 - learning_rate*theta0_grad
theta1_new = theta1 - learning_rate*theta1_grad
theta2_new = theta2 - learning_rate*theta2_grad
### (5) recalculate linear regression table using new gradients
data4,_,_,_ = get_linear_results(data, theta0_new, theta1_new, theta2_new)
### (6) summarize gradient results for question 4 (4)
MSE = data4['(y-y_hat)^2'].mean()
q4_mse = MSE
### (7) return all results for Gradio visualization
return data_t, q3_mse, theta0_grad, theta1_grad , theta2_grad, theta0_new, theta1_new, theta2_new, q4_mse
import numpy as np
import gradio as gr
### configure inputs
set_theta0 = gr.Number(value=0.1)
set_theta1 = gr.Number(value=0.1)
set_theta2 = gr.Number(value=0.1)
set_ita = gr.Number(value=0.001)
#print("check8")
#set_theta0 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
#set_theta1 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
#set_theta2 = gr.Slider(minimum = -10, maximum=10, step=0.01, value=0.1)
#set_ita = gr.Slider(minimum = 0, maximum=10, step=0.01, value=0.01)
### configure outputs
set_output_q3a = gr.Dataframe(type='pandas', label ='Question 3a')
set_output_q3b = gr.Textbox(label ='Question: What\'s Initial MSE loss')
set_output_q4a0 = gr.Textbox(label ='Question: What\'s theta0_grad')
set_output_q4a1 = gr.Textbox(label ='Question: What\'s theta1_grad')
set_output_q4a2 = gr.Textbox(label ='Question: What\'s theta2_grad')
set_output_q4b0 = gr.Textbox(label ='Question: What\'s theta0_new: updated by gradient descent')
set_output_q4b1 = gr.Textbox(label ='Question: What\'s theta1_new: updated by gradient descent')
set_output_q4b2 = gr.Textbox(label ='Question: What\'s theta2_new: updated by gradient descent')
set_output_q4b4 = gr.Textbox(label ='Question: What\'s New MSE after update the parameters using gradient descent')
### configure Gradio
interface = gr.Interface(fn=homework04_solution,
inputs=[set_theta0, set_theta1, set_theta2, set_ita],
outputs=[set_output_q3a, set_output_q3b,
set_output_q4a0, set_output_q4a1, set_output_q4a2,
set_output_q4b0, set_output_q4b1, set_output_q4b2,
set_output_q4b4],
title="CSCI4750/5750(gradient descent): Linear Regression/Optimization",
description= "Click examples below for a quick demo",
theme = 'huggingface'
)
interface.launch(debug=True) |