Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,10 @@ from keras.losses import MeanSquaredError, BinaryCrossentropy
|
|
| 15 |
from keras.regularizers import l2, l1
|
| 16 |
from keras.callbacks import Callback
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Set wide layout and TensorFlow Playground CSS
|
| 19 |
st.set_page_config(layout="wide")
|
| 20 |
st.markdown("""
|
|
@@ -298,67 +302,68 @@ with col_right:
|
|
| 298 |
self.X, self.y = X, y
|
| 299 |
self.losses = {"Epoch": [], "Train Loss": [], "Val Loss": []}
|
| 300 |
self.placeholder = st.empty()
|
| 301 |
-
self.model = None
|
| 302 |
|
| 303 |
def on_train_begin(self, logs=None):
|
| 304 |
-
self.model = self.
|
| 305 |
|
| 306 |
def on_epoch_end(self, epoch, logs=None):
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
y_pred_proba = self.model.predict(X_2d, verbose=0)
|
| 319 |
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
|
|
|
|
|
|
| 354 |
|
| 355 |
if st.session_state.training:
|
| 356 |
try:
|
| 357 |
model = create_model(len(selected_features), st.session_state.hidden_layer_neurons)
|
| 358 |
callback = OutputCallback(selected_data, cv)
|
| 359 |
-
callback.
|
| 360 |
-
model.fit(selected_data, cv, epochs=100, # Reduced
|
| 361 |
-
batch_size=batch_size, validation_split=1-train_ratio,
|
| 362 |
callbacks=[callback], verbose=0)
|
| 363 |
except Exception as e:
|
| 364 |
st.error(f"Training failed: {e}")
|
|
|
|
| 15 |
from keras.regularizers import l2, l1
|
| 16 |
from keras.callbacks import Callback
|
| 17 |
|
| 18 |
+
# Debug versions
|
| 19 |
+
st.write(f"TensorFlow version: {tf.__version__}")
|
| 20 |
+
st.write(f"Keras version: {tf.keras.__version__}")
|
| 21 |
+
|
| 22 |
# Set wide layout and TensorFlow Playground CSS
|
| 23 |
st.set_page_config(layout="wide")
|
| 24 |
st.markdown("""
|
|
|
|
| 302 |
self.X, self.y = X, y
|
| 303 |
self.losses = {"Epoch": [], "Train Loss": [], "Val Loss": []}
|
| 304 |
self.placeholder = st.empty()
|
| 305 |
+
self.model = None
|
| 306 |
|
| 307 |
def on_train_begin(self, logs=None):
|
| 308 |
+
self.model = self.model # Use the model passed implicitly by Keras
|
| 309 |
|
| 310 |
def on_epoch_end(self, epoch, logs=None):
|
| 311 |
+
try:
|
| 312 |
+
self.losses["Epoch"].append(epoch + 1)
|
| 313 |
+
self.losses["Train Loss"].append(logs["loss"])
|
| 314 |
+
self.losses["Val Loss"].append(logs.get("val_loss", logs["loss"]))
|
| 315 |
+
with self.placeholder.container():
|
| 316 |
+
col1, col2 = st.columns(2)
|
| 317 |
+
with col1:
|
| 318 |
+
plt.figure(figsize=(3, 3))
|
| 319 |
+
if problem_type == "Classification":
|
| 320 |
+
X_2d = self.X[:, :2] # Use only first two features for 2D
|
| 321 |
+
y_pred_proba = self.model.predict(X_2d, verbose=0) if self.model else np.zeros((len(X_2d), 1))
|
|
|
|
| 322 |
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 323 |
+
try:
|
| 324 |
+
plot_decision_regions(X_2d, self.y, clf=self.model, legend=2, colors='blue,red')
|
| 325 |
+
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap='coolwarm', edgecolors='k', alpha=0.7)
|
| 326 |
+
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 327 |
+
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 328 |
+
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 329 |
+
Z = self.model.predict(grid, verbose=0) if self.model else np.zeros((len(grid), 1))
|
| 330 |
+
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 331 |
+
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 332 |
+
except Exception as e:
|
| 333 |
+
st.warning(f"Decision region plot failed: {e}")
|
| 334 |
+
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 335 |
+
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 336 |
+
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 337 |
+
Z = self.model.predict(grid, verbose=0) if self.model else np.zeros((len(grid), 1))
|
| 338 |
+
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 339 |
+
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 340 |
+
plt.contourf(xx, yy, Z, alpha=0.3, cmap="coolwarm")
|
| 341 |
+
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap="coolwarm", edgecolors="k", alpha=0.7)
|
| 342 |
+
else:
|
| 343 |
+
y_pred = self.model.predict(self.X, verbose=0) if self.model else np.zeros_like(self.X[:, 0])
|
| 344 |
+
plt.scatter(self.X[:, 0], self.y, c="blue", alpha=0.5)
|
| 345 |
+
plt.plot(self.X[:, 0], y_pred, "r-", linewidths=2)
|
| 346 |
+
plt.gca().set_facecolor("#333")
|
| 347 |
+
plt.xticks([])
|
| 348 |
+
plt.yticks([])
|
| 349 |
+
st.pyplot(plt)
|
| 350 |
+
with col2:
|
| 351 |
+
fig, ax = plt.subplots(figsize=(3, 3))
|
| 352 |
+
ax.plot(self.losses["Epoch"], self.losses["Train Loss"], "b-", label="Train")
|
| 353 |
+
ax.plot(self.losses["Epoch"], self.losses["Val Loss"], "r--", label="Val")
|
| 354 |
+
ax.legend()
|
| 355 |
+
ax.set_facecolor("#333")
|
| 356 |
+
st.pyplot(fig)
|
| 357 |
+
except Exception as e:
|
| 358 |
+
st.error(f"Error in epoch end: {e}")
|
| 359 |
|
| 360 |
if st.session_state.training:
|
| 361 |
try:
|
| 362 |
model = create_model(len(selected_features), st.session_state.hidden_layer_neurons)
|
| 363 |
callback = OutputCallback(selected_data, cv)
|
| 364 |
+
callback.model = model # Explicitly set the model for the callback
|
| 365 |
+
model.fit(selected_data, cv, epochs=100, # Reduced for Spaces
|
| 366 |
+
batch_size=batch_size, validation_split=1-train_ratio,
|
| 367 |
callbacks=[callback], verbose=0)
|
| 368 |
except Exception as e:
|
| 369 |
st.error(f"Training failed: {e}")
|