eaglelandsonce commited on
Commit
05fae5d
·
verified ·
1 Parent(s): 2ead64f

Delete 3_SimpleRegression.py

Browse files
Files changed (1) hide show
  1. 3_SimpleRegression.py +0 -74
3_SimpleRegression.py DELETED
@@ -1,74 +0,0 @@
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(learning_rate=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, epochs=epochs)
19
- trained_weight = model.get_weights()[0][0]
20
- trained_bias = model.get_weights()[1]
21
- epochs = history.epoch
22
- hist = pd.DataFrame(history.history)
23
- rmse = hist["root_mean_squared_error"]
24
- return trained_weight, trained_bias, epochs, rmse
25
-
26
- # Function to plot the model
27
- def plot_the_model(trained_weight, trained_bias, feature, label):
28
- plt.figure(figsize=(10, 6))
29
- plt.xlabel('Feature')
30
- plt.ylabel('Label')
31
-
32
- # Plot the feature values vs. label values
33
- plt.scatter(feature, label, c='b')
34
-
35
- # Create a red line representing the model
36
- x0 = 0
37
- y0 = trained_bias
38
- x1 = feature[-1]
39
- y1 = trained_bias + (trained_weight * x1)
40
- plt.plot([x0, x1], [y0, y1], c='r')
41
-
42
- st.pyplot(plt)
43
-
44
- # Function to plot the loss curve
45
- def plot_the_loss_curve(epochs, rmse):
46
- plt.figure(figsize=(10, 6))
47
- plt.xlabel('Epoch')
48
- plt.ylabel('Root Mean Squared Error')
49
-
50
- plt.plot(epochs, rmse, label='Loss')
51
- plt.legend()
52
- plt.ylim([rmse.min()*0.97, rmse.max()])
53
- st.pyplot(plt)
54
-
55
- # Define the dataset
56
- 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], dtype=float).reshape(-1, 1)
57
- 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], dtype=float).reshape(-1, 1)
58
-
59
- # Streamlit interface
60
- st.title("Simple Linear Regression with Synthetic Data")
61
-
62
- learning_rate = st.sidebar.slider('Learning Rate', min_value=0.001, max_value=1.0, value=0.01, step=0.01)
63
- epochs = st.sidebar.slider('Epochs', min_value=1, max_value=1000, value=10, step=1)
64
- batch_size = st.sidebar.slider('Batch Size', min_value=1, max_value=len(my_feature), value=12, step=1)
65
-
66
- if st.sidebar.button('Run'):
67
- my_model = build_model(learning_rate)
68
- trained_weight, trained_bias, epochs, rmse = train_model(my_model, my_feature, my_label, epochs, batch_size)
69
-
70
- st.subheader('Model Plot')
71
- plot_the_model(trained_weight, trained_bias, my_feature, my_label)
72
-
73
- st.subheader('Loss Curve')
74
- plot_the_loss_curve(epochs, rmse)