Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ def cal_mse(X,y,b,w):
|
|
| 14 |
mse = np.mean((y_predict-y)**2)
|
| 15 |
return mse
|
| 16 |
|
| 17 |
-
def gradient_descent(n_samples=100, intercept=4, slope=3, intercept_random=4, slope_random=3, gradient_descent='False', gradient_descent_type = 'Batch GradientDescent' , learning_rate= 0.01, iteration=100):
|
| 18 |
### (1) generate simulated data points
|
| 19 |
X = 2 * np.random.rand(n_samples, 1)
|
| 20 |
y = intercept + slope * X + np.random.randn(n_samples, 1)
|
|
@@ -105,7 +105,7 @@ def gradient_descent(n_samples=100, intercept=4, slope=3, intercept_random=4, sl
|
|
| 105 |
|
| 106 |
train_mse = []
|
| 107 |
# Iterations
|
| 108 |
-
minibatch_size =
|
| 109 |
for i in range(iteration):
|
| 110 |
# shuffle dataset
|
| 111 |
shuffled_indices = np.random.permutation(len(X))
|
|
@@ -227,8 +227,11 @@ input_gradients = gr.inputs.Checkbox(label="Apply Gradient Descent")
|
|
| 227 |
#input_gradients_type = gr.inputs.CheckboxGroup(['Batch GradientDescient', 'Stochastic GradientDescent', 'Mini-Batch GradientDescent'],label="Type of Gradient Descent")
|
| 228 |
input_gradients_type = gr.inputs.Dropdown(['Batch GradientDescent', 'Stochastic GradientDescent', 'Mini-Batch GradientDescent'],label="Type of Gradient Descent")
|
| 229 |
|
|
|
|
|
|
|
|
|
|
| 230 |
input_learningrate = gr.inputs.Slider(0,2, step=0.0001, default=0.001, label='Learning Rate')
|
| 231 |
-
|
| 232 |
|
| 233 |
|
| 234 |
#### Define output component
|
|
@@ -237,7 +240,7 @@ output_plot1 = gr.outputs.Image(label="Regression plot")
|
|
| 237 |
|
| 238 |
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
|
| 239 |
interface = gr.Interface(fn=gradient_descent,
|
| 240 |
-
inputs=[input_sample, input_intercept, input_slope, input_intercept_random, input_slope_random, input_gradients, input_gradients_type, input_learningrate,
|
| 241 |
outputs=[output_plot1],
|
| 242 |
examples_per_page = 2,
|
| 243 |
#examples = [[4, 3, -7, -5, True, 0.0001, 100], [1, 2, -7, -8, False, 0.0001, 100]],
|
|
|
|
| 14 |
mse = np.mean((y_predict-y)**2)
|
| 15 |
return mse
|
| 16 |
|
| 17 |
+
def gradient_descent(n_samples=100, intercept=4, slope=3, intercept_random=4, slope_random=3, gradient_descent='False', gradient_descent_type = 'Batch GradientDescent' , learning_rate= 0.01, iteration=100, mini_batchsize = 32):
|
| 18 |
### (1) generate simulated data points
|
| 19 |
X = 2 * np.random.rand(n_samples, 1)
|
| 20 |
y = intercept + slope * X + np.random.randn(n_samples, 1)
|
|
|
|
| 105 |
|
| 106 |
train_mse = []
|
| 107 |
# Iterations
|
| 108 |
+
minibatch_size = mini_batchsize
|
| 109 |
for i in range(iteration):
|
| 110 |
# shuffle dataset
|
| 111 |
shuffled_indices = np.random.permutation(len(X))
|
|
|
|
| 227 |
#input_gradients_type = gr.inputs.CheckboxGroup(['Batch GradientDescient', 'Stochastic GradientDescent', 'Mini-Batch GradientDescent'],label="Type of Gradient Descent")
|
| 228 |
input_gradients_type = gr.inputs.Dropdown(['Batch GradientDescent', 'Stochastic GradientDescent', 'Mini-Batch GradientDescent'],label="Type of Gradient Descent")
|
| 229 |
|
| 230 |
+
|
| 231 |
+
input_batchsize = gr.inputs.Slider(1, 64, step=1, default=100, label='Batch size for Mini-BatchGD')
|
| 232 |
+
|
| 233 |
input_learningrate = gr.inputs.Slider(0,2, step=0.0001, default=0.001, label='Learning Rate')
|
| 234 |
+
input_iteration = gr.inputs.Slider(1, 1000, step=2, default=100, label='Iteration')
|
| 235 |
|
| 236 |
|
| 237 |
#### Define output component
|
|
|
|
| 240 |
|
| 241 |
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
|
| 242 |
interface = gr.Interface(fn=gradient_descent,
|
| 243 |
+
inputs=[input_sample, input_intercept, input_slope, input_intercept_random, input_slope_random, input_gradients, input_gradients_type, input_learningrate, input_iteration, input_batchsize],
|
| 244 |
outputs=[output_plot1],
|
| 245 |
examples_per_page = 2,
|
| 246 |
#examples = [[4, 3, -7, -5, True, 0.0001, 100], [1, 2, -7, -8, False, 0.0001, 100]],
|