shubham680 commited on
Commit
3ceca84
·
verified ·
1 Parent(s): e35803d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -71
app.py CHANGED
@@ -58,88 +58,90 @@ with st.sidebar:
58
 
59
  with col4:
60
  patience = st.number_input("Patience",3,20,step=1)
 
 
61
 
62
 
63
 
64
 
65
- if st.sidebar.button("Train"):
66
- if dataset:
67
- if dataset=="Circles":
68
- x,y=make_circles(n_samples=1000,noise=noise,random_state=42,factor=0.1)
69
- elif dataset=="moons":
70
- x,y=make_moons(n_samples=1000,noise=noise,random_state=42)
71
- elif dataset == "Blobs":
72
- x,y=make_blobs(n_samples=1000, centers=2, cluster_std=noise, random_state=42)
 
 
 
 
 
 
73
 
74
- x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=test_size,random_state=42,stratify=y)
75
 
76
- std = StandardScaler()
77
- x_train = std.fit_transform(x_train)
78
- x_test = std.transform(x_test)
 
 
 
 
 
79
 
80
 
81
- if reg == "L1":
82
- reg = l1(reg_rate)
83
- elif reg == "L2":
84
- reg = l2(reg_rate)
85
- elif reg == "ElasticNet":
86
- reg = l1_l2(l1=reg_rate, l2=reg_rate)
87
- else:
88
- reg=None
89
 
 
 
 
 
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- model = Sequential()
93
- model.add(InputLayer(shape=(2,)))
94
- if hl == len(nn):
95
- for i in range(0,len(nn)):
96
- model.add(Dense(units=nn[i],activation=af,kernel_regularizer=reg))
97
-
98
- model.add(Dense(units=1,activation="sigmoid",kernel_regularizer=reg))
99
- sgd=SGD(learning_rate=lr)
100
- model.compile(loss="binary_crossentropy",optimizer=sgd,metrics=["accuracy"])
101
- train_size=round(x_train.shape[0]-x_train.shape[0]*0.2)
102
-
103
- callbacks = []
104
- if es == "Yes":
105
- es = EarlyStopping(
106
- monitor="val_loss",
107
- #min_delta= min_delta if min_delta else 0.001,
108
- min_delta = min_delta,
109
- patience=patience,
110
- verbose=1,
111
- restore_best_weights=True,
112
- start_from_epoch=50
113
- )
114
- callbacks.append(es)
115
-
116
- hist=model.fit(x_train,y_train,epochs=epochs,batch_size=train_size,validation_data=(x_test, y_test),verbose=False)
117
-
118
- # --- Neural Network Diagram ---
119
- st.subheader("Neural Network Architecture")
120
- stringlist = []
121
- model.summary(print_fn=lambda x: stringlist.append(x))
122
- summary_str = "\n".join(stringlist)
123
- st.text(summary_str)
124
-
125
-
126
- # --- Plotting Decision region ---
127
- st.subheader("Decision Region")
128
- fig1, ax1 = plt.subplots(figsize=(6, 5))
129
- plot_decision_regions(X=x_test, y=y_test.astype(np.int_), clf=model, ax=ax1)
130
- ax1.set_title("Decision Regions", fontsize=12, weight="bold")
131
- st.pyplot(fig1)
132
-
133
- # --- Plot 2: Training vs Validation Loss ---
134
- st.subheader("Training vs Validation Loss")
135
- fig2, ax2 = plt.subplots(figsize=(6, 5))
136
- ax2.plot(hist.history["loss"], label="Training Loss", linewidth=2)
137
- ax2.plot(hist.history["val_loss"], label="Validation Loss", linewidth=2, linestyle="--")
138
- ax2.set_xlabel("Epochs")
139
- ax2.set_ylabel("Loss")
140
- ax2.legend()
141
- ax2.grid(alpha=0.3)
142
- st.pyplot(fig2)
143
 
144
 
145
 
 
58
 
59
  with col4:
60
  patience = st.number_input("Patience",3,20,step=1)
61
+
62
+ btn=st.sidebar.button("Train")
63
 
64
 
65
 
66
 
67
+ if btn:
68
+ if dataset:
69
+ if dataset=="Circles":
70
+ x,y=make_circles(n_samples=1000,noise=noise,random_state=42,factor=0.1)
71
+ elif dataset=="moons":
72
+ x,y=make_moons(n_samples=1000,noise=noise,random_state=42)
73
+ elif dataset == "Blobs":
74
+ x,y=make_blobs(n_samples=1000, centers=2, cluster_std=noise, random_state=42)
75
+
76
+ x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=test_size,random_state=42,stratify=y)
77
+
78
+ std = StandardScaler()
79
+ x_train = std.fit_transform(x_train)
80
+ x_test = std.transform(x_test)
81
 
 
82
 
83
+ if reg == "L1":
84
+ reg = l1(reg_rate)
85
+ elif reg == "L2":
86
+ reg = l2(reg_rate)
87
+ elif reg == "ElasticNet":
88
+ reg = l1_l2(l1=reg_rate, l2=reg_rate)
89
+ else:
90
+ reg=None
91
 
92
 
 
 
 
 
 
 
 
 
93
 
94
+ model = Sequential()
95
+ model.add(InputLayer(shape=(2,)))
96
+ if hl == len(nn):
97
+ for i in range(0,len(nn)):
98
+ model.add(Dense(units=nn[i],activation=af,kernel_regularizer=reg))
99
 
100
+ model.add(Dense(units=1,activation="sigmoid",kernel_regularizer=reg))
101
+ sgd=SGD(learning_rate=lr)
102
+ model.compile(loss="binary_crossentropy",optimizer=sgd,metrics=["accuracy"])
103
+ train_size=round(x_train.shape[0]-x_train.shape[0]*0.2)
104
+
105
+ callbacks = []
106
+ if es == "Yes":
107
+ es = EarlyStopping(
108
+ monitor="val_loss",
109
+ #min_delta= min_delta if min_delta else 0.001,
110
+ min_delta = min_delta,
111
+ patience=patience,
112
+ verbose=1,
113
+ restore_best_weights=True,
114
+ start_from_epoch=50
115
+ )
116
+ callbacks.append(es)
117
 
118
+ hist=model.fit(x_train,y_train,epochs=epochs,batch_size=train_size,validation_data=(x_test, y_test),verbose=False)
119
+
120
+ # --- Neural Network Diagram ---
121
+ st.subheader("Neural Network Architecture")
122
+ stringlist = []
123
+ model.summary(print_fn=lambda x: stringlist.append(x))
124
+ summary_str = "\n".join(stringlist)
125
+ st.text(summary_str)
126
+
127
+
128
+ # --- Plotting Decision region ---
129
+ st.subheader("Decision Region")
130
+ fig1, ax1 = plt.subplots(figsize=(6, 5))
131
+ plot_decision_regions(X=x_test, y=y_test.astype(np.int_), clf=model, ax=ax1)
132
+ ax1.set_title("Decision Regions", fontsize=12, weight="bold")
133
+ st.pyplot(fig1)
134
+
135
+ # --- Plot 2: Training vs Validation Loss ---
136
+ st.subheader("Training vs Validation Loss")
137
+ fig2, ax2 = plt.subplots(figsize=(6, 5))
138
+ ax2.plot(hist.history["loss"], label="Training Loss", linewidth=2)
139
+ ax2.plot(hist.history["val_loss"], label="Validation Loss", linewidth=2, linestyle="--")
140
+ ax2.set_xlabel("Epochs")
141
+ ax2.set_ylabel("Loss")
142
+ ax2.legend()
143
+ ax2.grid(alpha=0.3)
144
+ st.pyplot(fig2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
 
147