| | import streamlit as st |
| | import pandas as pd |
| | import numpy as np |
| | import matplotlib.pyplot as plt |
| | import seaborn as sns |
| | import sklearn |
| | import keras |
| | import re |
| |
|
| | from sklearn.datasets import make_moons, make_circles |
| | from sklearn.model_selection import train_test_split |
| | from sklearn.preprocessing import StandardScaler |
| |
|
| | from keras.models import Sequential |
| | from keras.layers import InputLayer, Dense, Dropout |
| | from keras.optimizers import SGD |
| | from keras.regularizers import l1, l2 |
| | from keras.callbacks import EarlyStopping |
| |
|
| | from mlxtend.plotting import plot_decision_regions |
| |
|
| | st.title("Neural Network PlayGround") |
| |
|
| | |
| | with st.sidebar: |
| | dataset = st.selectbox("Select Dataset",["Moons","Circles","Upload ypur own file"]) |
| | noise = st.slider("Noise", 0.0, 0.5, 0.2) |
| | t_s = st.slider("Select test size", 0.1, 0.4, 0.01) |
| |
|
| | st.header("Parameters") |
| | epochs = st.number_input("Enter number of epochs",1,100000) |
| | hidden_layers = st.number_input("Enter number of hidden layers",0,7,1) |
| |
|
| | collect_numbers = lambda x: [int(i) for i in re.split("[^0-9]", x) if i != ""] |
| | numbers = st.text_input("Neurons in each hidden layer (comma separated)") |
| | nn = collect_numbers(numbers) |
| |
|
| | col1,col2,col3 = st.columns(3) |
| |
|
| | with col1: |
| | learning_rate = st.selectbox("Learning rate",[0.1,0.01,0.001]) |
| |
|
| | with col2: |
| | activation_fun = st.selectbox("Activation Function",['relu','tanh','sigmoid']) |
| |
|
| | with col3: |
| | Regularization = st.selectbox("Regularizer",['None','l1','l2']) |
| |
|
| | col4,col5 = st.columns(2) |
| |
|
| | with col4: |
| | reg_rate = st.selectbox( |
| | "Regularizer Rate", |
| | [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10] |
| | ) |
| |
|
| | with col5: |
| | early_stop_option = st.selectbox("Early Stopping", ["No", "Yes"], index=0) |
| |
|
| | if early_stop_option == "Yes": |
| | col6, col7 = st.columns(2) |
| | with col6: |
| | min_delta = st.selectbox( |
| | "Minimum Delta", |
| | [0, 0.00001, 0.0001, 0.0003, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 2, 3] |
| | ) |
| | with col7: |
| | patience = st.selectbox( |
| | "Patience", |
| | [10, 15, 20, 25, 30, 35, 40, 45, 50], |
| | index=0 |
| | ) |
| | |
| | |
| | upload_files = st.file_uploader("Upload Dataset",type = ["csv",'xlsx']) |
| | if upload_files: |
| | if upload_files.name.endswith(".csv"): |
| | df = pd.read_csv(upload_files) |
| | else: |
| | df = pd.read_excel(upload_files) |
| | columns = df.columns.tolist() |
| | class_var = st.selectbox("Select class variable (target)", columns) |
| | st.write(class_var) |
| | if st.button("start trainning"): |
| | if dataset: |
| | if dataset=="Circles": |
| | x,y=make_circles(n_samples=10000,noise=noise,random_state=1,factor=0.1) |
| | elif dataset=="Moons": |
| | x,y=make_moons(n_samples=10000,noise=noise,random_state=1) |
| | |
| | |
| | x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=t_s ,random_state=1,stratify=y) |
| | std = StandardScaler() |
| | x_train = std.fit_transform(x_train) |
| | x_test = std.transform(x_test) |
| |
|
| | if Regularization: |
| | if Regularization == 'l1': |
| | l1_ = l1(l1 = reg_rate) |
| | elif Regularization == 'l2': |
| | l2_=l2(l2 = reg_rate) |
| | else: |
| | Regularization = None |
| |
|
| | |
| | model = Sequential() |
| | model.add(InputLayer(shape = (x_train.shape[1],))) |
| | if hidden_layers == len(nn): |
| | for i in range(0,len(nn)): |
| | model.add(Dense(units = nn[i],activation = activation_fun,kernel_regularizer = Regularization)) |
| | model.add(Dense(units = 1,activation = 'sigmoid',kernel_regularizer = Regularization)) |
| | sgd = SGD(learning_rate = 0.01) |
| | model.compile(loss="binary_crossentropy", optimizer=sgd, metrics=["accuracy"]) |
| | a = round(x_train.shape[0] - x_train.shape[0]*0.2) |
| | |
| | callbacks = [] |
| | if early_stop_option == "Yes": |
| | es = EarlyStopping( |
| | monitor="val_loss", |
| | min_delta= min_delta if min_delta else 0.001, |
| | patience=patience, |
| | verbose=1, |
| | restore_best_weights=True, |
| | start_from_epoch=120 |
| | ) |
| | callbacks.append(es) |
| | hist=model.fit(x_train,y_train,epochs=epochs,batch_size=a,validation_data=(x_test, y_test),verbose=False) |
| | |
| | |
| | st.markdown("---") |
| | col1, col2 = st.columns(2) |
| | with col1: |
| | fig, ax = plt.subplots(figsize=(6, 5)) |
| | ax.scatter(x[:, 0], x[:, 1], c=y, cmap="bwr", edgecolor="k") |
| | ax.set_title("Dataset Visualization") |
| | st.pyplot(fig) |
| | |
| | |
| | with col2: |
| | st.markdown("### 🟢 Decision Boundary") |
| | fig1, ax1 = plt.subplots(figsize=(6, 5)) |
| | plot_decision_regions(X=x_train, y=y_train.astype(np.int_), clf=model, ax=ax1) |
| | ax1.set_title("Decision Regions", fontsize=12, weight="bold") |
| | st.pyplot(fig1, clear_figure=True) |
| | |
| | |
| | st.markdown("### 🔵 Training vs Validation Loss") |
| | fig2, ax2 = plt.subplots(figsize=(8, 8)) |
| | ax2.plot(range(1, epochs+1), hist.history["loss"], label="loss", linewidth=2) |
| | ax2.plot(range(1, epochs+1), hist.history["val_loss"], label="val_loss", linewidth=2, linestyle="--") |
| | ax2.set_xlabel("Epochs") |
| | ax2.set_ylabel("Loss") |
| | ax2.legend() |
| | ax2.grid(alpha=0.3) |
| | ax2.set_title("Training & Validation Loss", fontsize=12, weight="bold") |
| | st.pyplot(fig2, clear_figure=True) |
| | |
| | |
| |
|