Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -246,7 +246,7 @@ with col_center:
|
|
| 246 |
st.session_state.hidden_layer_neurons.pop()
|
| 247 |
|
| 248 |
def increase_neurons(i):
|
| 249 |
-
if st.session_state.
|
| 250 |
st.session_state.hidden_layer_neurons[i] += 1
|
| 251 |
|
| 252 |
def decrease_neurons(i):
|
|
@@ -298,15 +298,15 @@ 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 # Store
|
| 302 |
|
| 303 |
def on_train_begin(self, logs=None):
|
| 304 |
-
self.model = self.
|
| 305 |
|
| 306 |
def on_epoch_end(self, epoch, logs=None):
|
| 307 |
self.losses["Epoch"].append(epoch + 1)
|
| 308 |
self.losses["Train Loss"].append(logs["loss"])
|
| 309 |
-
self.losses["Val Loss"].append(logs
|
| 310 |
with self.placeholder.container():
|
| 311 |
col1, col2 = st.columns(2)
|
| 312 |
with col1:
|
|
@@ -314,24 +314,30 @@ with col_right:
|
|
| 314 |
if problem_type == "Classification":
|
| 315 |
# Use only the first two features for 2D plotting
|
| 316 |
X_2d = self.X[:, :2] # Ensure 2D input
|
| 317 |
-
y_pred_proba = self.model.predict(X_2d, verbose=0)
|
| 318 |
-
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 319 |
try:
|
|
|
|
|
|
|
| 320 |
plot_decision_regions(X_2d, self.y, clf=self.model, legend=2, colors='blue,red')
|
| 321 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap='coolwarm', edgecolors='k', alpha=0.7)
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
except Exception as e:
|
| 324 |
st.warning(f"Decision region plot failed: {e}")
|
| 325 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 326 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 327 |
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 328 |
-
Z = self.model.predict(grid, verbose=0)
|
| 329 |
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 330 |
-
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 331 |
plt.contourf(xx, yy, Z, alpha=0.3, cmap="coolwarm")
|
| 332 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap="coolwarm", edgecolors="k", alpha=0.7)
|
| 333 |
else:
|
| 334 |
-
y_pred = self.model.predict(self.X, verbose=0)
|
| 335 |
plt.scatter(self.X[:, 0], self.y, c="blue", alpha=0.5)
|
| 336 |
plt.plot(self.X[:, 0], y_pred, "r-", linewidths=2)
|
| 337 |
plt.gca().set_facecolor("#333")
|
|
@@ -347,7 +353,14 @@ with col_right:
|
|
| 347 |
st.pyplot(fig)
|
| 348 |
|
| 349 |
if st.session_state.training:
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
| 246 |
st.session_state.hidden_layer_neurons.pop()
|
| 247 |
|
| 248 |
def increase_neurons(i):
|
| 249 |
+
if st.session_state.hidden_layer_neurons[i] < 8:
|
| 250 |
st.session_state.hidden_layer_neurons[i] += 1
|
| 251 |
|
| 252 |
def decrease_neurons(i):
|
|
|
|
| 298 |
self.X, self.y = X, y
|
| 299 |
self.losses = {"Epoch": [], "Train Loss": [], "Val Loss": []}
|
| 300 |
self.placeholder = st.empty()
|
| 301 |
+
self.model = None # Store model instance explicitly
|
| 302 |
|
| 303 |
def on_train_begin(self, logs=None):
|
| 304 |
+
self.model = self.model_ # Assign the model from the fit method
|
| 305 |
|
| 306 |
def on_epoch_end(self, epoch, logs=None):
|
| 307 |
self.losses["Epoch"].append(epoch + 1)
|
| 308 |
self.losses["Train Loss"].append(logs["loss"])
|
| 309 |
+
self.losses["Val Loss"].append(logs.get("val_loss", logs["loss"])) # Handle missing val_loss
|
| 310 |
with self.placeholder.container():
|
| 311 |
col1, col2 = st.columns(2)
|
| 312 |
with col1:
|
|
|
|
| 314 |
if problem_type == "Classification":
|
| 315 |
# Use only the first two features for 2D plotting
|
| 316 |
X_2d = self.X[:, :2] # Ensure 2D input
|
|
|
|
|
|
|
| 317 |
try:
|
| 318 |
+
y_pred_proba = self.model.predict(X_2d, verbose=0)
|
| 319 |
+
y_pred = (y_pred_proba > 0.5).astype(int).ravel()
|
| 320 |
plot_decision_regions(X_2d, self.y, clf=self.model, legend=2, colors='blue,red')
|
| 321 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap='coolwarm', edgecolors='k', alpha=0.7)
|
| 322 |
+
# Add decision boundary contour
|
| 323 |
+
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 324 |
+
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 325 |
+
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 326 |
+
Z = self.model.predict(grid, verbose=0)
|
| 327 |
+
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 328 |
+
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 329 |
except Exception as e:
|
| 330 |
st.warning(f"Decision region plot failed: {e}")
|
| 331 |
xx, yy = np.meshgrid(np.linspace(X_2d[:, 0].min(), X_2d[:, 0].max(), 100),
|
| 332 |
np.linspace(X_2d[:, 1].min(), X_2d[:, 1].max(), 100))
|
| 333 |
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 334 |
+
Z = self.model.predict(grid, verbose=0) if self.model else np.zeros_like(grid[:, 0])
|
| 335 |
Z = (Z > 0.5).astype(int).reshape(xx.shape)
|
| 336 |
+
plt.contour(xx, yy, Z, levels=[0.5], colors='black', linewidths=2)
|
| 337 |
plt.contourf(xx, yy, Z, alpha=0.3, cmap="coolwarm")
|
| 338 |
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=self.y, cmap="coolwarm", edgecolors="k", alpha=0.7)
|
| 339 |
else:
|
| 340 |
+
y_pred = self.model.predict(self.X, verbose=0) if self.model else np.zeros_like(self.X[:, 0])
|
| 341 |
plt.scatter(self.X[:, 0], self.y, c="blue", alpha=0.5)
|
| 342 |
plt.plot(self.X[:, 0], y_pred, "r-", linewidths=2)
|
| 343 |
plt.gca().set_facecolor("#333")
|
|
|
|
| 353 |
st.pyplot(fig)
|
| 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.model_ = model # Explicitly set the model for the callback
|
| 360 |
+
model.fit(selected_data, cv, epochs=100, # Reduced epochs for Spaces
|
| 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}")
|
| 365 |
+
|
| 366 |
st.markdown('</div>', unsafe_allow_html=True)
|