Update app.py
Browse files1234567 unmodified lines141516171819207 unmodified lines2829303132333435365 unmodified lines42434445464748495051525354555657585960616263646566674 unmodified lines7273747576778 unmodified lines8687888990919239 unmodified lines1321331341351361371 unmodified line139140141142143144### CSCI 4750/5750: regression models and gradient descentimport gradio as grimport matplotlibimport matplotlib.pyplot as plt7 unmodified linesmse = np.mean((y_predict-y)**2)return msedef gradient_descent(intercept=4, slope=3, intercept_random=4, slope_random=3, gradient_descent=False, learning_rate= 0.01, iteration=1000):### (1) generate simulated data pointsX = 2 * np.random.rand(100, 1)y = intercept + slope * X + np.random.randn(100, 1)7 unmodified linesy_predict### (4) Draw baseline linear Linefig = plt.figure(figsize=(12,12))plt.subplot(2,1,1)plt.plot(X, y_predict, "r-", linewidth=2, label = "Line of best fit")plt.plot(X, y, "b.")5 unmodified linesy_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 descif gradient_descent:b = intercept_randomw = slope_randomlr = learning_rate # learning rateiteration = 1000# Store initial values for plotting.b_history = [b]w_history = [w]# Iterationsfor i in range(iteration):b_grad = 0.0w_grad = 0.0for n in range(len(X)):b_grad = b_grad - (y[n] - b - w*X[n])*1.0w_grad = w_grad - (y[n] - b - w*X[n])*X[n]b_grad /= len(X)w_grad /= len(X)4 unmodified lines# Store parameters for plottingb_history.append(b)w_history.append(w)plt.xlabel("$x_1$", fontsize=22)plt.ylabel("$y$", rotation=0, fontsize=22)8 unmodified lines### (5) Visualize loss functionplt.subplot(2,1,2)### (5.1) generate grid of parametersb = np.arange(-10,10,0.1) #bias39 unmodified linesplt.yticks(fontsize=18)plt.xlim(-10,10)plt.ylim(-10,10)#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')1234567897 unmodified lines171819202122237 unmodified lines3132333435363738395 unmodified lines4546474849505152535455565758596061626364656667686970714 unmodified lines76777879808182838 unmodified lines9293949596979839 unmodified lines1381391401411421431441451461471481491501511521531541551561 unmodified line158159160161162163164165166### CSCI 4750/5750: regression models and gradient descent### CSCI 4750/5750: regression modelsimport gradio as grimport matplotlibimport matplotlib.pyplot as plt7 unmodified linesmse = np.mean((y_predict-y)**2)return msedef 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 pointsX = 2 * np.random.rand(100, 1)y = intercept + slope * X + np.random.randn(100, 1)7 unmodified linesy_predict### (4) Draw baseline linear Linefig = plt.figure(figsize=(12,18))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 linesy_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 descif gradient_descent:b = intercept_randomw = slope_randomlr = learning_rate # learning rateiteration = iteration# Store initial values for plotting.b_history = [b]w_history = [w]train_mse = []# Iterationsfor i in range(iteration):b_grad = 0.0w_grad = 0.0for n in range(len(X)):b_grad = b_grad - (y[n,0] - b - w*X[n,0])*1.0w_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 plottingb_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 functionplt.subplot(3,1,2)### (5.1) generate grid of parametersb = np.arange(-10,10,0.1) #bias39 unmodified linesplt.yticks(fontsize=18)plt.xlim(-10,10)plt.ylim(-10,10)### 6. Visualize the learning curvesif 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