shubham680 commited on
Commit
faae43d
·
verified ·
1 Parent(s): 7ebefa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -379,7 +379,7 @@ from keras.optimizers import SGD
379
  from keras.regularizers import l1, l2, l1_l2
380
  from keras.callbacks import EarlyStopping
381
  from mlxtend.plotting import plot_decision_regions
382
- from tensorflow.keras.utils import plot_model
383
 
384
 
385
  # ========== Custom CSS ==========
@@ -459,8 +459,8 @@ with st.sidebar:
459
  with col2:
460
  lr = st.selectbox("Learning Rate", [0.1, 0.01, 0.02, 0.2])
461
 
462
- reg = st.selectbox("Regularizer", ["None", "L1", "L2", "ElasticNet"])
463
- if reg != "None":
464
  reg_rate = st.slider("Regularization rate", 0.0, 0.1, 0.01)
465
 
466
  es = st.selectbox("Early Stopping", ["No", "Yes"], index=0)
@@ -504,14 +504,15 @@ x_train = std.fit_transform(x_train)
504
  x_test = std.transform(x_test)
505
 
506
  # Regularizer setup
507
- if reg == "L1":
 
 
508
  reg = l1(reg_rate)
509
  elif reg == "L2":
510
  reg = l2(reg_rate)
511
  elif reg == "ElasticNet":
512
  reg = l1_l2(l1=reg_rate, l2=reg_rate)
513
- else:
514
- reg = None
515
 
516
  # Build model
517
  model = Sequential()
@@ -535,6 +536,23 @@ if st.sidebar.button("🚀 Train Model"):
535
  hist = model.fit(x_train, y_train, epochs=epochs, validation_data=(x_test, y_test),
536
  batch_size=32, verbose=0, callbacks=callbacks)
537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538
  # Decision boundary
539
  st.subheader("Decision Boundary")
540
  fig, ax = plt.subplots()
@@ -549,22 +567,4 @@ if st.sidebar.button("🚀 Train Model"):
549
  ax2.legend()
550
  st.pyplot(fig2)
551
 
552
- # Neural network diagram
553
- plot_model(model, to_file="model.png", show_shapes=True, show_layer_names=True)
554
- st.image("model.png", caption="Neural Network Architecture")
555
- # st.subheader("Neural Network Architecture")
556
- # fig3, ax3 = plt.subplots()
557
- # layer_sizes = [2] + nn + [1]
558
- # for i, size in enumerate(layer_sizes):
559
- # for j in range(size):
560
- # ax3.scatter(i, j, s=800, color="skyblue", edgecolors="black")
561
- # if i < len(layer_sizes) - 1:
562
- # for j in range(size):
563
- # for k in range(layer_sizes[i + 1]):
564
- # ax3.plot([i, i + 1], [j, k], color="gray", alpha=0.3)
565
- # ax3.axis("off")
566
- # st.pyplot(fig3)
567
-
568
-
569
-
570
-
 
379
  from keras.regularizers import l1, l2, l1_l2
380
  from keras.callbacks import EarlyStopping
381
  from mlxtend.plotting import plot_decision_regions
382
+ from io import StringIO
383
 
384
 
385
  # ========== Custom CSS ==========
 
459
  with col2:
460
  lr = st.selectbox("Learning Rate", [0.1, 0.01, 0.02, 0.2])
461
 
462
+ reg_choice = st.selectbox("Regularizer", ["None", "L1", "L2", "ElasticNet"])
463
+ if reg_choice != "None":
464
  reg_rate = st.slider("Regularization rate", 0.0, 0.1, 0.01)
465
 
466
  es = st.selectbox("Early Stopping", ["No", "Yes"], index=0)
 
504
  x_test = std.transform(x_test)
505
 
506
  # Regularizer setup
507
+ if reg_choice == "None":
508
+ reg = None
509
+ elif reg == "L1":
510
  reg = l1(reg_rate)
511
  elif reg == "L2":
512
  reg = l2(reg_rate)
513
  elif reg == "ElasticNet":
514
  reg = l1_l2(l1=reg_rate, l2=reg_rate)
515
+
 
516
 
517
  # Build model
518
  model = Sequential()
 
536
  hist = model.fit(x_train, y_train, epochs=epochs, validation_data=(x_test, y_test),
537
  batch_size=32, verbose=0, callbacks=callbacks)
538
 
539
+ # Neural Network
540
+ # Capture summary
541
+ stringlist = []
542
+ model.summary(print_fn=lambda x: stringlist.append(x))
543
+ summary_str = "\n".join(stringlist)
544
+
545
+ # Process into a dataframe (split by whitespace properly)
546
+ data = []
547
+ for line in stringlist[2:-4]: # skip the header and footer
548
+ parts = [p for p in line.split(" ") if p != ""]
549
+ data.append(parts)
550
+
551
+ df = pd.DataFrame(data)
552
+ st.subheader("Neural Network Architecture")
553
+ st.table(df)
554
+
555
+
556
  # Decision boundary
557
  st.subheader("Decision Boundary")
558
  fig, ax = plt.subplots()
 
567
  ax2.legend()
568
  st.pyplot(fig2)
569
 
570
+