File size: 4,840 Bytes
2c15317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
041815a
2c15317
 
 
 
 
 
 
8a66143
041815a
2c15317
 
8a66143
2c15317
 
 
 
 
 
 
 
8a66143
2c15317
 
 
 
 
 
 
 
 
 
 
 
 
 
8a66143
041815a
2c15317
 
8a66143
2c15317
 
 
 
 
8a66143
2c15317
 
 
 
 
 
 
8a66143
2c15317
 
 
 
 
8a66143
 
2c15317
8aedf74
2c15317
 
 
 
8a66143
 
 
2c15317
8aedf74
2c15317
8a66143
2c15317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e352689
 
2c15317
 
 
ec9ee8e
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
def homework04_solution(theta0, theta1, theta2, learning_rate):
  import numpy as np
  import pandas as pd

  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'])
  

  ### (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.inputs.Number()
set_theta1 = gr.inputs.Number()
set_theta2 = gr.inputs.Number()
set_ita = gr.inputs.Number()


### configure outputs
set_output_q3a = gr.outputs.Dataframe(type='pandas', label ='Question 3a')
set_output_q3b = gr.outputs.Textbox(label ='Question 3b: Initial MSE loss')
set_output_q4a0 = gr.outputs.Textbox(label ='Question 4 (2): theta0_grad')
set_output_q4a1 = gr.outputs.Textbox(label ='Question 4 (2): theta1_grad')
set_output_q4a2 = gr.outputs.Textbox(label ='Question 4 (2): theta2_grad')

set_output_q4b0 = gr.outputs.Textbox(label ='Question 4 (3): theta0_new: updated by gradient descent')
set_output_q4b1 = gr.outputs.Textbox(label ='Question 4 (3): theta1_new: updated by gradient descent')
set_output_q4b2 = gr.outputs.Textbox(label ='Question 4 (3): theta2_new: updated by gradient descent')

set_output_q4b4 = gr.outputs.Textbox(label ='Question 4 (4): 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],
                         examples_per_page = 2,
                         examples=[
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                              np.round(np.random.uniform(0, 1, (3,)),2).tolist()+[0.001],
                          ],
                         title="CSCI4750/5750(hw04): Linear Regression/Optimization", 
                         description= "Click examples below for a quick demo",
                         theme = 'huggingface',
                         layout = 'horizontal',
                         live=True
                         )


interface.launch(debug=True)