Update app.py
Browse files1234567897 unmodified lines17181917202122237 unmodified lines31323331343533363738395 unmodified lines454647454849505152535452555657585960616263646566636467686970714 unmodified lines76777879808182838 unmodified lines929394899596979839 unmodified lines1381391401411421431441451461471481491501511521531541551561 unmodified line158159160161162163164165166### CSCI 4750/5750: regression models and gradient descent
### CSCI 4750/5750: regression models
import gradio as grimport matplotlibimport matplotlib.pyplot as plt7 unmodified lines mse = np.mean((y_predict-y)**2) return mse
def gradient_descent(intercept=4, slope=3, intercept_random=4, slope_random=3, gradient_descent=False, learning_rate= 0.01, iteration=1000):def gradient_descent(intercept=4, slope=3, intercept_random=4, slope_random=3, gradient_descent=False, learning_rate= 0.01, iteration=100): ### (1) generate simulated data points X = 2 * np.random.rand(100, 1) y = intercept + slope * X + np.random.randn(100, 1)7 unmodified lines y_predict
### (4) Draw baseline linear Line fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(12,18))
plt.subplot(2,1,1) plt.subplot(3,1,1) plt.plot(X, y_predict, "r-", linewidth=2, label = "Line of best fit") plt.plot(X, y, "b.")
5 unmodified lines y_predict = X_new_b.dot(np.array([intercept_random, slope_random])) plt.plot(X_new, y_predict, "g-", linewidth=2, label = "Random line")
### (4.3) Apply gradient desc if gradient_descent: b = intercept_random w = slope_random
lr = learning_rate # learning rate iteration = 1000 iteration = iteration
# Store initial values for plotting. b_history = [b] w_history = [w]
train_mse = [] # Iterations for i in range(iteration): b_grad = 0.0 w_grad = 0.0 for n in range(len(X)): b_grad = b_grad - (y[n] - b - w*X[n])*1.0 w_grad = w_grad - (y[n] - b - w*X[n])*X[n] b_grad = b_grad - (y[n,0] - b - w*X[n,0])*1.0 w_grad = w_grad - (y[n,0] - b - w*X[n,0])*X[n,0] b_grad /= len(X) w_grad /= len(X) 4 unmodified lines # Store parameters for plotting b_history.append(b) w_history.append(w) train_mse.append(cal_mse(X,y,b,w)) plt.xlabel("$x_1$", fontsize=22) plt.ylabel("$y$", rotation=0, fontsize=22)8 unmodified lines
### (5) Visualize loss function plt.subplot(2,1,2) plt.subplot(3,1,2)
### (5.1) generate grid of parameters b = np.arange(-10,10,0.1) #bias39 unmodified lines plt.yticks(fontsize=18) plt.xlim(-10,10) plt.ylim(-10,10)
### 6. Visualize the learning curves if gradient_descent: plt.subplot(3,1,3) plt.plot(train_mse,label="train_loss (lr="+str(learning_rate)+")") plt.xlabel('Iteration',fontweight="bold",fontsize = 22) plt.ylabel('Loss',fontweight="bold",fontsize = 22) plt.title("Learning curve: Loss VS Epochs",fontweight="bold",fontsize = 22) plt.legend(fontsize=18) plt.xticks(fontsize=18) plt.yticks(fontsize=18)
#plt.show() fig.tight_layout() plt.savefig('plot_line.png', dpi=300)1 unmodified line
#### Define input componentinput_intercept = gr.inputs.Slider(1, 8, step=0.5, default=4, label='(Baseline) Intercept')input_slope = gr.inputs.Slider(-8, 8, step=0.5, default=2.8, label='(Baseline) Slope')app.py+30-8