Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,6 +51,7 @@ def load_history(history_path):
|
|
| 51 |
def smooth_data(data, window_size):
|
| 52 |
return np.convolve(data, np.ones(window_size)/window_size, mode='valid')
|
| 53 |
|
|
|
|
| 54 |
# Streamlit app
|
| 55 |
st.markdown('<h1 class="big-font">TuNNe</h1>', unsafe_allow_html=True)
|
| 56 |
st.markdown('<h2 class="center-text">Tuning a Neural Network</h2>', unsafe_allow_html=True)
|
|
@@ -82,65 +83,80 @@ learning_rates = sorted(set(lr for lr, _, _ in hyperparameters))
|
|
| 82 |
|
| 83 |
# Select slider for learning rate
|
| 84 |
st.markdown('<p class="slider-label">Learning Rate</p>', unsafe_allow_html=True)
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Filter batch sizes based on selected learning rate
|
| 88 |
filtered_bs = sorted(set(bs for lr, bs, _ in hyperparameters if lr == selected_lr))
|
| 89 |
st.markdown('<p class="slider-label">Batch Size</p>', unsafe_allow_html=True)
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Filter epochs based on selected learning rate and batch size
|
| 93 |
filtered_epochs = sorted(set(epochs for lr, bs, epochs in hyperparameters if lr == selected_lr and bs == selected_bs))
|
| 94 |
st.markdown('<p class="slider-label">Epochs</p>', unsafe_allow_html=True)
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Options for grid and smoothing
|
| 98 |
enable_grid = st.checkbox("Enable Grid Lines")
|
| 99 |
-
if selected_epochs > 20:
|
| 100 |
smoothing_window = st.slider("Smoothing Window (every 4 epochs)", min_value=1, max_value=5, step=1, value=1)
|
| 101 |
|
| 102 |
# Find the corresponding history file
|
| 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 |
else:
|
| 143 |
-
st.error(
|
| 144 |
|
| 145 |
|
| 146 |
# Final message
|
|
|
|
| 51 |
def smooth_data(data, window_size):
|
| 52 |
return np.convolve(data, np.ones(window_size)/window_size, mode='valid')
|
| 53 |
|
| 54 |
+
# Streamlit app
|
| 55 |
# Streamlit app
|
| 56 |
st.markdown('<h1 class="big-font">TuNNe</h1>', unsafe_allow_html=True)
|
| 57 |
st.markdown('<h2 class="center-text">Tuning a Neural Network</h2>', unsafe_allow_html=True)
|
|
|
|
| 83 |
|
| 84 |
# Select slider for learning rate
|
| 85 |
st.markdown('<p class="slider-label">Learning Rate</p>', unsafe_allow_html=True)
|
| 86 |
+
if len(learning_rates) > 1:
|
| 87 |
+
selected_lr = st.select_slider("Learning Rate", options=learning_rates, label_visibility="collapsed")
|
| 88 |
+
else:
|
| 89 |
+
selected_lr = learning_rates[0] if learning_rates else None
|
| 90 |
+
st.write(f"Only one learning rate available: {selected_lr}")
|
| 91 |
|
| 92 |
# Filter batch sizes based on selected learning rate
|
| 93 |
filtered_bs = sorted(set(bs for lr, bs, _ in hyperparameters if lr == selected_lr))
|
| 94 |
st.markdown('<p class="slider-label">Batch Size</p>', unsafe_allow_html=True)
|
| 95 |
+
if len(filtered_bs) > 1:
|
| 96 |
+
selected_bs = st.select_slider("Batch Size", options=filtered_bs, label_visibility="collapsed")
|
| 97 |
+
else:
|
| 98 |
+
selected_bs = filtered_bs[0] if filtered_bs else None
|
| 99 |
+
st.write(f"Only one batch size available: {selected_bs}")
|
| 100 |
|
| 101 |
# Filter epochs based on selected learning rate and batch size
|
| 102 |
filtered_epochs = sorted(set(epochs for lr, bs, epochs in hyperparameters if lr == selected_lr and bs == selected_bs))
|
| 103 |
st.markdown('<p class="slider-label">Epochs</p>', unsafe_allow_html=True)
|
| 104 |
+
if len(filtered_epochs) > 1:
|
| 105 |
+
selected_epochs = st.select_slider("Epochs", options=filtered_epochs, label_visibility="collapsed")
|
| 106 |
+
else:
|
| 107 |
+
selected_epochs = filtered_epochs[0] if filtered_epochs else None
|
| 108 |
+
st.write(f"Only one epoch option available: {selected_epochs}")
|
| 109 |
|
| 110 |
# Options for grid and smoothing
|
| 111 |
enable_grid = st.checkbox("Enable Grid Lines")
|
| 112 |
+
if selected_epochs and selected_epochs > 20:
|
| 113 |
smoothing_window = st.slider("Smoothing Window (every 4 epochs)", min_value=1, max_value=5, step=1, value=1)
|
| 114 |
|
| 115 |
# Find the corresponding history file
|
| 116 |
+
if selected_lr is not None and selected_bs is not None and selected_epochs is not None:
|
| 117 |
+
history_filename = f"mnist_model_lr{selected_lr}_bs{selected_bs}_epochs{selected_epochs}.json"
|
| 118 |
+
history_path = os.path.join(model_dir, history_filename)
|
| 119 |
+
|
| 120 |
+
if os.path.exists(history_path):
|
| 121 |
+
history = load_history(history_path)
|
| 122 |
+
|
| 123 |
+
# Plot training & validation accuracy values
|
| 124 |
+
fig, ax = plt.subplots()
|
| 125 |
+
accuracy = history['accuracy']
|
| 126 |
+
val_accuracy = history['val_accuracy']
|
| 127 |
+
if selected_epochs > 20 and 'smoothing_window' in locals() and smoothing_window > 1:
|
| 128 |
+
accuracy = smooth_data(accuracy, smoothing_window * 4)
|
| 129 |
+
val_accuracy = smooth_data(val_accuracy, smoothing_window * 4)
|
| 130 |
+
sns.lineplot(x=range(len(accuracy)), y=accuracy, ax=ax, label='Train Accuracy')
|
| 131 |
+
sns.lineplot(x=range(len(val_accuracy)), y=val_accuracy, ax=ax, label='Validation Accuracy')
|
| 132 |
+
ax.set_title('Model Accuracy', fontsize=15)
|
| 133 |
+
ax.set_ylabel('Accuracy', fontsize=12)
|
| 134 |
+
ax.set_xlabel('Epoch', fontsize=12)
|
| 135 |
+
ax.legend(loc='upper left', fontsize=10)
|
| 136 |
+
if enable_grid:
|
| 137 |
+
ax.grid(True)
|
| 138 |
+
st.pyplot(fig)
|
| 139 |
+
|
| 140 |
+
# Plot training & validation loss values
|
| 141 |
+
fig, ax = plt.subplots()
|
| 142 |
+
loss = history['loss']
|
| 143 |
+
val_loss = history['val_loss']
|
| 144 |
+
if selected_epochs > 20 and 'smoothing_window' in locals() and smoothing_window > 1:
|
| 145 |
+
loss = smooth_data(loss, smoothing_window * 4)
|
| 146 |
+
val_loss = smooth_data(val_loss, smoothing_window * 4)
|
| 147 |
+
sns.lineplot(x=range(len(loss)), y=loss, ax=ax, label='Train Loss')
|
| 148 |
+
sns.lineplot(x=range(len(val_loss)), y=val_loss, ax=ax, label='Validation Loss')
|
| 149 |
+
ax.set_title('Model Loss', fontsize=15)
|
| 150 |
+
ax.set_ylabel('Loss', fontsize=12)
|
| 151 |
+
ax.set_xlabel('Epoch', fontsize=12)
|
| 152 |
+
ax.legend(loc='upper left', fontsize=10)
|
| 153 |
+
if enable_grid:
|
| 154 |
+
ax.grid(True)
|
| 155 |
+
st.pyplot(fig)
|
| 156 |
+
else:
|
| 157 |
+
st.error(f"History file not found: {history_path}")
|
| 158 |
else:
|
| 159 |
+
st.error("Unable to load model due to missing hyperparameters")
|
| 160 |
|
| 161 |
|
| 162 |
# Final message
|