Shamima commited on
Commit
b8055f6
1 Parent(s): f16c0c0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """effect of eta and iterations on sgd.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1Lso8y1XapdHGJOHnY0pL4ZeSGaa-6lOz
8
+ """
9
+
10
+ # Commented out IPython magic to ensure Python compatibility.
11
+ import numpy as np
12
+ import matplotlib.pyplot as plt
13
+ # %matplotlib inline
14
+ plt.rcParams['figure.figsize'] = (10, 5)
15
+
16
+ def generate_data():
17
+ X = 2 * np.random.rand(100, 1)
18
+ y = 4 + 3 * X + np.random.randn(100, 1)
19
+ return X, y
20
+
21
+ def get_norm_eqn(X, y):
22
+ X_b = np.c_[np.ones((100, 1)), X]
23
+ theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
24
+ y_norm = X_b.dot(theta_best)
25
+ return X_b, y_norm
26
+
27
+ def generate_sgd_plot(eta, n_iterations):
28
+ #initialize parameters
29
+ m = 100
30
+ theta = np.random.randn(2,1)
31
+ X, y = generate_data()
32
+ X_b, y_norm = get_norm_eqn(X, y)
33
+
34
+ # plot how the parameters change wrt normal line
35
+ # as the algorithm learns
36
+ plt.scatter(X,y, c='#7678ed', label="data points")
37
+ plt.axis([0, 2.0, 0, 14])
38
+ for iteration in range(n_iterations):
39
+ gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
40
+ theta = theta - eta * gradients
41
+ y_new = X_b.dot(theta)
42
+ plt.plot(X, y_new, color='#f18701', linestyle='dashed', linewidth=0.2)
43
+ plt.plot(X, y_norm, '#3d348b', label="Normal Eqation line")
44
+ plt.xlabel('X')
45
+ plt.ylabel('Y')
46
+ plt.legend(loc='best')
47
+ return plt
48
+
49
+
50
+
51
+ import gradio as gr
52
+
53
+ demo = gr.Blocks()
54
+
55
+ with demo:
56
+ gr.Markdown(
57
+ """
58
+ # How learning rate and number of iterations affect SGD
59
+ Move sliders to change the values of eta and number of iterations to see how it affects the convergance rate of algorithm.
60
+ """
61
+ )
62
+ inputs = [gr.Slider(0.02, 0.5, label="learning rate, eta"), gr.Slider(500, 1000, 200, label="number of iterations")]
63
+ output = gr.Plot()
64
+
65
+ btn = gr.Button("Run")
66
+ btn.click(fn=generate_sgd_plot, inputs=inputs, outputs=output)
67
+
68
+ demo.launch()
69
+