Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -321,35 +321,44 @@ with col_right:
|
|
| 321 |
self.X, self.y = X, y
|
| 322 |
self.losses = {"Epoch": [], "Train Loss": [], "Val Loss": []}
|
| 323 |
self.placeholder = st.empty()
|
|
|
|
| 324 |
|
| 325 |
def on_train_begin(self, logs=None):
|
| 326 |
self.model = self.model # Use the model passed implicitly by Keras
|
|
|
|
| 327 |
|
| 328 |
def on_epoch_end(self, epoch, logs=None):
|
| 329 |
try:
|
| 330 |
-
self.
|
|
|
|
| 331 |
self.losses["Train Loss"].append(logs["loss"])
|
| 332 |
self.losses["Val Loss"].append(logs.get("val_loss", logs["loss"]))
|
| 333 |
with self.placeholder.container():
|
| 334 |
# Single column for vertical stacking
|
| 335 |
st.subheader("Decision Region & Loss")
|
| 336 |
-
#
|
|
|
|
|
|
|
| 337 |
fig1, ax1 = plt.subplots(figsize=(3, 3)) # Match dataset scatterplot size
|
| 338 |
if problem_type == "Classification":
|
| 339 |
X_2d = self.X[:, :2] # Use only first two features for 2D
|
| 340 |
-
|
|
|
|
| 341 |
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 342 |
try:
|
|
|
|
| 343 |
plot_decision_regions(X_2d, self.y, clf=self.model, legend=2, colors='blue,red')
|
| 344 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap='coolwarm', edgecolors='k', alpha=0.7)
|
|
|
|
| 345 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 346 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 347 |
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 348 |
-
Z = self.model.predict(grid, verbose=0)
|
| 349 |
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 350 |
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 351 |
except Exception as e:
|
| 352 |
st.warning(f"Decision region plot failed: {e}")
|
|
|
|
| 353 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 354 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 355 |
grid = np.c_[xx.ravel(), yy.ravel()]
|
|
|
|
| 321 |
self.X, self.y = X, y
|
| 322 |
self.losses = {"Epoch": [], "Train Loss": [], "Val Loss": []}
|
| 323 |
self.placeholder = st.empty()
|
| 324 |
+
self.current_epoch = 0 # Track current epoch
|
| 325 |
|
| 326 |
def on_train_begin(self, logs=None):
|
| 327 |
self.model = self.model # Use the model passed implicitly by Keras
|
| 328 |
+
self.current_epoch = 0
|
| 329 |
|
| 330 |
def on_epoch_end(self, epoch, logs=None):
|
| 331 |
try:
|
| 332 |
+
self.current_epoch = epoch + 1 # Update current epoch
|
| 333 |
+
self.losses["Epoch"].append(self.current_epoch)
|
| 334 |
self.losses["Train Loss"].append(logs["loss"])
|
| 335 |
self.losses["Val Loss"].append(logs.get("val_loss", logs["loss"]))
|
| 336 |
with self.placeholder.container():
|
| 337 |
# Single column for vertical stacking
|
| 338 |
st.subheader("Decision Region & Loss")
|
| 339 |
+
# Display epoch count above decision region
|
| 340 |
+
st.write(f"Epoch: {self.current_epoch}")
|
| 341 |
+
# Decision region plot (3x3 size, improved accuracy)
|
| 342 |
fig1, ax1 = plt.subplots(figsize=(3, 3)) # Match dataset scatterplot size
|
| 343 |
if problem_type == "Classification":
|
| 344 |
X_2d = self.X[:, :2] # Use only first two features for 2D
|
| 345 |
+
# Ensure model prediction for decision boundary
|
| 346 |
+
y_pred_proba = self.model.predict(X_2d, verbose=0)
|
| 347 |
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 348 |
try:
|
| 349 |
+
# Use mlxtend for decision regions
|
| 350 |
plot_decision_regions(X_2d, self.y, clf=self.model, legend=2, colors='blue,red')
|
| 351 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap='coolwarm', edgecolors='k', alpha=0.7)
|
| 352 |
+
# Add precise decision boundary using contour
|
| 353 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 354 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 355 |
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 356 |
+
Z = self.model.predict(grid, verbose=0)
|
| 357 |
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 358 |
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 359 |
except Exception as e:
|
| 360 |
st.warning(f"Decision region plot failed: {e}")
|
| 361 |
+
# Fallback: Use contourf for decision regions
|
| 362 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 363 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 364 |
grid = np.c_[xx.ravel(), yy.ravel()]
|