Niharmahesh commited on
Commit
f776208
·
verified ·
1 Parent(s): a69eba5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -44
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
- selected_lr = st.select_slider("LR", options=learning_rates)
 
 
 
 
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
- selected_bs = st.select_slider("BS", options=filtered_bs)
 
 
 
 
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
- selected_epochs = st.select_slider("Epochs", options=filtered_epochs)
 
 
 
 
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
- history_filename = f"mnist_model_lr{selected_lr}_bs{selected_bs}_epochs{selected_epochs}.json"
104
- history_path = os.path.join(model_dir, history_filename)
105
-
106
- if os.path.exists(history_path):
107
- history = load_history(history_path)
108
-
109
- # Plot training & validation accuracy values
110
- fig, ax = plt.subplots()
111
- accuracy = history['accuracy']
112
- val_accuracy = history['val_accuracy']
113
- if selected_epochs > 20 and smoothing_window > 1:
114
- accuracy = smooth_data(accuracy, smoothing_window * 4)
115
- val_accuracy = smooth_data(val_accuracy, smoothing_window * 4)
116
- sns.lineplot(x=range(len(accuracy)), y=accuracy, ax=ax, label='Train Accuracy')
117
- sns.lineplot(x=range(len(val_accuracy)), y=val_accuracy, ax=ax, label='Validation Accuracy')
118
- ax.set_title('Model Accuracy', fontsize=15)
119
- ax.set_ylabel('Accuracy', fontsize=12)
120
- ax.set_xlabel('Epoch', fontsize=12)
121
- ax.legend(loc='upper left', fontsize=10)
122
- if enable_grid:
123
- ax.grid(True)
124
- st.pyplot(fig)
125
-
126
- # Plot training & validation loss values
127
- fig, ax = plt.subplots()
128
- loss = history['loss']
129
- val_loss = history['val_loss']
130
- if selected_epochs > 20 and smoothing_window > 1:
131
- loss = smooth_data(loss, smoothing_window * 4)
132
- val_loss = smooth_data(val_loss, smoothing_window * 4)
133
- sns.lineplot(x=range(len(loss)), y=loss, ax=ax, label='Train Loss')
134
- sns.lineplot(x=range(len(val_loss)), y=val_loss, ax=ax, label='Validation Loss')
135
- ax.set_title('Model Loss', fontsize=15)
136
- ax.set_ylabel('Loss', fontsize=12)
137
- ax.set_xlabel('Epoch', fontsize=12)
138
- ax.legend(loc='upper left', fontsize=10)
139
- if enable_grid:
140
- ax.grid(True)
141
- st.pyplot(fig)
 
 
 
142
  else:
143
- st.error(f"History file not found: {history_path}")
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