eaglelandsonce commited on
Commit
46436db
1 Parent(s): 397a225

Create 3_SyntheticRegression.py

Browse files
Files changed (1) hide show
  1. pages/3_SyntheticRegression.py +77 -0
pages/3_SyntheticRegression.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import tensorflow as tf
5
+ from matplotlib import pyplot as plt
6
+
7
+ # Function to build the model
8
+ def build_model(my_learning_rate):
9
+ model = tf.keras.models.Sequential()
10
+ model.add(tf.keras.layers.Dense(units=1, input_shape=(1,)))
11
+ model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=my_learning_rate),
12
+ loss='mean_squared_error',
13
+ metrics=[tf.keras.metrics.RootMeanSquaredError()])
14
+ return model
15
+
16
+ # Function to train the model
17
+ def train_model(model, feature, label, epochs, batch_size):
18
+ history = model.fit(x=feature, y=label, batch_size=batch_size,
19
+ epochs=epochs)
20
+ trained_weight = model.get_weights()[0]
21
+ trained_bias = model.get_weights()[1]
22
+ epochs = history.epoch
23
+ hist = pd.DataFrame(history.history)
24
+ rmse = hist["root_mean_squared_error"]
25
+ return trained_weight, trained_bias, epochs, rmse
26
+
27
+ # Function to plot the model
28
+ def plot_the_model(trained_weight, trained_bias, feature, label):
29
+ plt.figure(figsize=(10, 6))
30
+ plt.xlabel('Feature')
31
+ plt.ylabel('Label')
32
+
33
+ # Plot the feature values vs. label values
34
+ plt.scatter(feature, label, c='b')
35
+
36
+ # Create a red line representing the model
37
+ x0 = 0
38
+ y0 = trained_bias
39
+ x1 = feature[-1]
40
+ y1 = trained_bias + (trained_weight * x1)
41
+ plt.plot([x0, x1], [y0, y1], c='r')
42
+
43
+ plt.show()
44
+
45
+ # Function to plot the loss curve
46
+ def plot_the_loss_curve(epochs, rmse):
47
+ plt.figure(figsize=(10, 6))
48
+ plt.xlabel('Epoch')
49
+ plt.ylabel('Root Mean Squared Error')
50
+
51
+ plt.plot(epochs, rmse, label='Loss')
52
+ plt.legend()
53
+ plt.ylim([rmse.min()*0.97, rmse.max()])
54
+ plt.show()
55
+
56
+ # Define the dataset
57
+ my_feature = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0])
58
+ my_label = np.array([5.0, 8.8, 9.6, 14.2, 18.8, 19.5, 21.4, 26.8, 28.9, 32.0, 33.8, 38.2])
59
+
60
+ # Streamlit interface
61
+ st.title("Simple Linear Regression with Synthetic Data")
62
+
63
+ learning_rate = st.sidebar.slider('Learning Rate', min_value=0.001, max_value=1.0, value=0.01, step=0.01)
64
+ epochs = st.sidebar.slider('Epochs', min_value=1, max_value=1000, value=10, step=1)
65
+ batch_size = st.sidebar.slider('Batch Size', min_value=1, max_value=len(my_feature), value=12, step=1)
66
+
67
+ if st.sidebar.button('Run'):
68
+ my_model = build_model(learning_rate)
69
+ trained_weight, trained_bias, epochs, rmse = train_model(my_model, my_feature, my_label, epochs, batch_size)
70
+
71
+ st.subheader('Model Plot')
72
+ plot_the_model(trained_weight, trained_bias, my_feature, my_label)
73
+ st.pyplot(plt)
74
+
75
+ st.subheader('Loss Curve')
76
+ plot_the_loss_curve(epochs, rmse)
77
+ st.pyplot(plt)