CosmickVisions commited on
Commit
ff877e4
·
verified ·
1 Parent(s): d3ea99a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -23
app.py CHANGED
@@ -246,33 +246,69 @@ def build_autoencoder(input_shape, encoding_dim, layers_config, autoencoder_type
246
  autoencoder.compile(optimizer=optimizer, loss='mse', metrics=['mse'])
247
  return autoencoder, encoder, decoder
248
 
249
- def train_model(model, X_train, y_train, X_test, y_test, epochs, batch_size, problem_type, input_data=None, target_data=None, do_grid_search=False, params=None, grid_params=None, training_placeholder=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  start_time = time.time()
251
  history = None
252
- if input_data is None:
253
- input_data = X_train
254
- if target_data is None:
255
- target_data = y_train if y_train is not None else X_train
256
  if isinstance(model, keras.Model):
257
- class StreamlitCallback(keras.callbacks.Callback):
258
- def __init__(self, placeholder):
259
- super().__init__()
260
- self.placeholder = placeholder
261
- self.epoch_data = []
262
-
263
- def on_epoch_end(self, epoch, logs=None):
264
- self.epoch_data.append(logs)
265
- df = pd.DataFrame(self.epoch_data)
266
- fig = px.line(df, x=df.index, y=['loss', 'val_loss'], labels={'index': 'Epoch', 'value': 'Loss'})
267
- metric_name = 'mse' if 'mse' in logs else 'accuracy'
268
- fig.add_trace(go.Scatter(x=df.index, y=df[metric_name], mode='lines', name=metric_name))
269
- fig.add_trace(go.Scatter(x=df.index, y=df[f'val_{metric_name}'], mode='lines', name=f'val_{metric_name}'))
270
- self.placeholder.plotly_chart(fig)
271
-
272
  streamlit_callback = StreamlitCallback(training_placeholder)
273
- history = model.fit(input_data, target_data, epochs=epochs, batch_size=batch_size,
274
- validation_data=(X_test, y_test if y_test is not None else X_test), verbose=0,
275
- callbacks=[streamlit_callback])
 
 
 
 
 
 
276
  else:
277
  if do_grid_search and grid_params:
278
  grid_search = GridSearchCV(model, grid_params, cv=3, n_jobs=-1, scoring='accuracy' if problem_type in ["Binary Classification", "Multi-Class"] else 'neg_mean_squared_error')
@@ -282,6 +318,7 @@ def train_model(model, X_train, y_train, X_test, y_test, epochs, batch_size, pro
282
  else:
283
  model.set_params(**params)
284
  model.fit(X_train, y_train)
 
285
  training_time = time.time() - start_time
286
  return history, model, training_time
287
 
 
246
  autoencoder.compile(optimizer=optimizer, loss='mse', metrics=['mse'])
247
  return autoencoder, encoder, decoder
248
 
249
+ class StreamlitCallback(keras.callbacks.Callback):
250
+ def __init__(self, placeholder):
251
+ super().__init__()
252
+ self.placeholder = placeholder
253
+ self.epoch_data = []
254
+
255
+ def on_epoch_end(self, epoch, logs=None):
256
+ # Append the logs for the current epoch
257
+ self.epoch_data.append(logs)
258
+
259
+ # Create a DataFrame from the logs
260
+ df = pd.DataFrame(self.epoch_data)
261
+
262
+ # Create the Plotly figure
263
+ fig = go.Figure()
264
+
265
+ # Add training loss trace
266
+ fig.add_trace(go.Scatter(
267
+ x=df.index, y=df['loss'], mode='lines', name='Training Loss'
268
+ ))
269
+
270
+ # Add validation loss trace (if available)
271
+ if 'val_loss' in df.columns:
272
+ fig.add_trace(go.Scatter(
273
+ x=df.index, y=df['val_loss'], mode='lines', name='Validation Loss'
274
+ ))
275
+
276
+ # Add metric trace (e.g., accuracy or MSE)
277
+ metric_name = 'accuracy' if 'accuracy' in df.columns else 'mse'
278
+ if metric_name in df.columns:
279
+ fig.add_trace(go.Scatter(
280
+ x=df.index, y=df[metric_name], mode='lines', name=metric_name.capitalize()
281
+ ))
282
+
283
+ # Update the layout
284
+ fig.update_layout(
285
+ title="Training Progress",
286
+ xaxis_title="Epoch",
287
+ yaxis_title="Value",
288
+ legend_title="Metrics"
289
+ )
290
+
291
+ # Update the placeholder with the new figure
292
+ self.placeholder.plotly_chart(fig, use_container_width=True)
293
+
294
+ def train_model(model, X_train, y_train, X_test, y_test, epochs, batch_size, problem_type, input_data=None, target_data=None, do_grid_search=False, params=None, grid_params=None):
295
  start_time = time.time()
296
  history = None
297
+
298
+ # Create a placeholder for the training progress chart
299
+ training_placeholder = st.empty()
300
+
301
  if isinstance(model, keras.Model):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  streamlit_callback = StreamlitCallback(training_placeholder)
303
+ history = model.fit(
304
+ input_data if input_data is not None else X_train,
305
+ target_data if target_data is not None else y_train,
306
+ epochs=epochs,
307
+ batch_size=batch_size,
308
+ validation_data=(X_test, y_test if y_test is not None else X_test),
309
+ verbose=0,
310
+ callbacks=[streamlit_callback]
311
+ )
312
  else:
313
  if do_grid_search and grid_params:
314
  grid_search = GridSearchCV(model, grid_params, cv=3, n_jobs=-1, scoring='accuracy' if problem_type in ["Binary Classification", "Multi-Class"] else 'neg_mean_squared_error')
 
318
  else:
319
  model.set_params(**params)
320
  model.fit(X_train, y_train)
321
+
322
  training_time = time.time() - start_time
323
  return history, model, training_time
324