-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (131 loc) · 6.08 KB
/
Copy pathapp.py
File metadata and controls
146 lines (131 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from utils import *
from nn_keras import NN_Keras
from knn import *
from locally_weighted_regression import *
# Set page layout to wide
#st.set_page_config(layout="wide")
# Title of the page
st.title('Non-Linear Function Fit')
# Create sidebar: Dataset settings
st.sidebar.title('Data set')
x_start = st.sidebar.number_input('X - min', value=0.0)
x_end = st.sidebar.number_input('X - max', value=10.0)
x_step = st.sidebar.number_input('X - step', value=0.1)
m = st.sidebar.number_input('Number of samples', value=200)
noise_mu = st.sidebar.number_input('Gaussian noise - mean', value=0.0)
noise_sigma = st.sidebar.number_input('Gaussian noise - standard deviation', value=0.2)
# Text-input: allows to specify the function we want to approximate
str_fx = st.text_input('Function to approximate (must be correct python syntax using numpy as np)', '0.5*x + np.sin(0.5*x)')
X, y, X_linspace = create_dataset(x_start, x_end, x_step, noise_mu, noise_sigma, m, str_fx)
# Select approach to approximate
option = st.selectbox('Select approach',
('Locally Weighted Regression', 'k-Nearest Neighbours', 'Neural Network'))
# Initial option - locally weighted regression
if option == 'Locally Weighted Regression':
st.header('Locally Weighted Regression')
st.write('For more information, please have a look at: Locally Weighted Learning by Peter Englert')
# Parameter: tau - the kernel width
tau = st.number_input('Tau - kernel width', value=0.2)
# Run model fit
yhat = predict_weighted_regression(X, X_linspace, y, tau)
# Plot results
fig = go.Figure()
fig.add_trace(go.Scatter(x=X,
y=y,
mode='markers',
marker_color='rgba(0,0,0,0.5)',
name='Training data'))
fig.add_trace(go.Scatter(x=X_linspace,
y=yhat.squeeze(),
mode='markers',
marker_color='rgba(255,0,0,1)',
name='Predictions'))
fig.add_trace(go.Scatter(x=X_linspace,
y=eval_fx(X_linspace, str_fx),
mode='lines',
line_color='rgba(51,255,255,0.7)',
name='Underlying function'))
st.plotly_chart(fig)
elif option == 'k-Nearest Neighbours':
st.header('k-Nearest Neighbours')
st.write('For each test point we look for the k-nearest neighbours in the dataset and predict its output value by the mean over all output values of the neighbours.')
# Parameter: tau - the kernel width
k = st.number_input('K - Number of neighbours', value=5)
# Run model fit
yhat = knn_regression(X, X_linspace, y, k)
# Plot results
fig = go.Figure()
fig.add_trace(go.Scatter(x=X,
y=y,
mode='markers',
marker_color='rgba(0,0,0,0.5)',
name='Training data'))
fig.add_trace(go.Scatter(x=X_linspace,
y=yhat.squeeze(),
mode='markers',
marker_color='rgba(255,0,0,1)',
name='Predictions'))
fig.add_trace(go.Scatter(x=X_linspace,
y=eval_fx(X_linspace, str_fx),
mode='lines',
line_color='rgba(51,255,255,0.7)',
name='Underlying function'))
st.plotly_chart(fig)
# Second option - neural network
elif option == 'Neural Network':
st.header('Neural Network')
st.write('We will use a neural network with one hidden layer (tanh activation to model non-linearities) to approximate the function.')
# create an NN_Keras object to help training the network
nn_helper = NN_Keras()
# scale data
X_scaled, X_linspace_scaled = nn_helper.scale_data(X, X_linspace)
# Network parameters
col1, col2 = st.columns(2)
with col1:
epochs = st.number_input('Training epochs', value=200)
num_neurons = st.number_input('Number of neurons', value=50)
with col2:
learning_rate = st.number_input('Adam - Initial learning rate', value=0.1)
# Train the model
model_trained = False
if st.button('Train model'):
st.write('Model is being trained...')
# Delete any old checkpoints
nn_helper.delete_existing_model_checkpoints()
# Initialize the model
nn_helper.init_model(X_scaled, num_neurons, learning_rate)
# Fit the model with the data
nn_helper.fit(X_scaled, y, epochs)
# predict the final function fit
yhat = nn_helper.predict(X_linspace_scaled)
model_trained = True
st.write('Model finished training!')
# if model has been successfully trained, show the plot
if model_trained:
# Generate model predictions at different checkpoints
df = nn_helper.predict_at_checkpoints(X_linspace)
# show plot
fig = px.scatter(df,
x='X',
y='Predictions',
animation_frame='epoch',
labels='Predictions',
color_discrete_sequence=['red'])
fig['data'][0]['name'] = 'Predictions'
fig['data'][0]['showlegend'] = True
fig.add_trace(go.Scatter(x=X,
y=y,
mode='markers',
marker_color='rgba(0,0,0,0.5)',
name='Training data'))
fig.add_trace(go.Scatter(x=X_linspace,
y=eval_fx(X_linspace, str_fx),
mode='lines',
line_color='rgba(51,255,255,0.7)',
name='Underlying function'))
st.plotly_chart(fig)
st.write('Press the play button to animate the model predictions at different epochs of the training process.')