CoolFace
Apppublic

Ayesha188/Optimized_Neural_Network_Framework

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py232 linesDownload Raw Back to root
1# Importing required libraries2import streamlit as st3import pandas as pd4import numpy as np5import seaborn as sns6import matplotlib.pyplot as plt7import tensorflow as tf8from keras.models import Sequential9from keras.layers import InputLayer, Dense, Dropout, LeakyReLU, PReLU, BatchNormalization10from keras.regularizers import L1, L2, L1L211from sklearn.datasets import make_classification, make_regression, make_moons, make_circles12from sklearn.model_selection import train_test_split13from sklearn.preprocessing import StandardScaler14import io15import warnings16warnings.filterwarnings("ignore")17 18# Title19st.title('Deep Neural Network Explorer')20st.sidebar.title('Deep Neural Network Explorer')21 22# Problem Type23problem_type = st.sidebar.selectbox('Problem Type', ['Classification', 'Regression', 'Moons', 'Circles'])24 25# Learning Rate26learning_rate = st.sidebar.selectbox('Learning Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])27 28# Activation Functions29activation_func = st.sidebar.selectbox('Activation', ['tanh', 'sigmoid', 'linear', 'relu', 'softmax', 'leaky_relu', 'prelu'])30 31# Regularization Rate32regularization_rate = st.sidebar.selectbox('Regularization Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])33 34# Regularization35regularization = st.sidebar.selectbox('Regularization', ['None', 'L1', 'L2', 'Elastic Net'])36 37# Define Regularizers38if regularization == 'None':39    kernel_regularizer = None40    bias_regularizer = None41elif regularization == 'L1':42    kernel_regularizer = L1(regularization_rate)43    bias_regularizer = L1(regularization_rate)44elif regularization == 'L2':45    kernel_regularizer = L2(regularization_rate)46    bias_regularizer = L2(regularization_rate)47elif regularization == 'Elastic Net':48    kernel_regularizer = L1L2(l1=regularization_rate, l2=regularization_rate)49    bias_regularizer = L1L2(l1=regularization_rate, l2=regularization_rate)50 51# Epochs52epochs = st.sidebar.number_input("Select number of Epochs", min_value=1, max_value=1000, value=50)53 54# Split Train/Test55test_size = st.sidebar.slider("Test Size (%)", min_value=10, max_value=90, value=40, step=1) / 10056 57# Hidden Layers58hidden_layers = st.sidebar.slider('Number of Hidden Layers', 1, 10, 1)59 60# Batch Normalization and Dropout Options61apply_bn = st.sidebar.multiselect('Batch Normalization on Layers', [f'Layer {i+1}' for i in range(hidden_layers)])62apply_dropout = st.sidebar.multiselect('Dropout on Layers', [f'Layer {i+1}' for i in range(hidden_layers)])63dropout_rate = st.sidebar.slider("Dropout Rate", 0.0, 1.0, 0.5)64 65# Early Stopping66early_stopping = st.sidebar.checkbox('Use Early Stopping')67patience = st.sidebar.number_input("Patience for Early Stopping", min_value=1, max_value=50, value=10)68 69# Weight Initialization70weight_init = st.sidebar.selectbox('Weight Initialization', ['Glorot Normal', 'Glorot Uniform', 'He Normal', 'He Uniform', 'Zeros', 'Constant', 'LeCun Normal', 'LeCun Uniform'])71if weight_init in ['Zeros', 'Constant']:72    st.warning("Using zeros or constant initialization means weights will not update effectively during training, leading to poor performance.")73 74# Build the model75model = Sequential()76model.add(InputLayer(input_shape=(2,)))77 78# Add hidden layers based on user input79for i in range(hidden_layers):80    neurons = st.sidebar.number_input(f'No of Neurons in Layer {i+1}', min_value=1, max_value=100, value=5)81    layer_activation = activation_func if activation_func in ['tanh', 'sigmoid', 'linear', 'relu', 'softmax'] else None82    83    if activation_func == 'leaky_relu':84        model.add(Dense(units=neurons, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer))85        model.add(LeakyReLU())86    elif activation_func == 'prelu':87        model.add(Dense(units=neurons, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer))88        model.add(PReLU())89    else:90        model.add(Dense(units=neurons, activation=layer_activation, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer))91 92    # Apply Batch Normalization93    if f'Layer {i+1}' in apply_bn:94        model.add(BatchNormalization())95    96    # Apply Dropout97    if f'Layer {i+1}' in apply_dropout:98        model.add(Dropout(rate=dropout_rate))99 100# Final Layer101if problem_type == 'Regression':102    model.add(Dense(units=1, activation='linear'))103else:104    if problem_type in ['Moons', 'Circles']:105        model.add(Dense(units=1, activation='sigmoid'))106    else:107        model.add(Dense(units=1, activation='relu'))108 109# Select optimizer110optimizer = st.sidebar.selectbox('Optimizer', ['SGD', 'Adam', 'RMSprop', 'Adagrad', 'Adamax', 'Nadam'])111 112# Batch Size113batch_size = st.sidebar.slider("Batch Size", 1, 256, 32)114 115# Dataset Generation and Visualization116if st.sidebar.button('Submit'):117    # Generate dataset based on the problem type118    if problem_type == 'Classification':119        X, y = make_classification(n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, n_classes=2, class_sep=2.5, random_state=10)120        st.subheader("Actual Data (Classification)")121    elif problem_type == 'Moons':122        X, y = make_moons(n_samples=1000, noise=0.1, random_state=20)123        st.subheader("Actual Data (Moons)")124    elif problem_type == 'Circles':125        X, y = make_circles(n_samples=1000, noise=0.05, random_state=20)126        st.subheader("Actual Data (Circles)")127    else:128        X, y = make_regression(n_samples=1000, n_features=2, noise=0.1, random_state=20)129        st.subheader("Actual Data (Regression)")130 131    # Plot the data132    fig, ax = plt.subplots(figsize=(8, 4))133    sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y, ax=ax)134    st.pyplot(fig)135 136    # Train/Test Split137    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=20, stratify=y if problem_type != 'Regression' else None)138 139    # Standardize Data140    scaler = StandardScaler()141    X_train = scaler.fit_transform(X_train)142    X_test = scaler.transform(X_test)143 144    # Compile the Model145    loss_function = 'mse' if problem_type == 'Regression' else 'binary_crossentropy'146    metrics = ['mse', 'mae'] if problem_type == 'Regression' else ['accuracy']147    model.compile(optimizer=optimizer.lower(), loss=loss_function, metrics=metrics)148 149    # Model Summary150    buffer = io.StringIO()151    model.summary(print_fn=lambda x: buffer.write(x + '\n'))152    st.text("Model Summary:")153    st.text(buffer.getvalue())154    buffer.close()155 156    # Early Stopping Callback157    early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience) if early_stopping else None158 159    # Training the Model160    history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.2, callbacks=[early_stopping_cb] if early_stopping else None)161 162    # Plot Loss and Accuracy163    fig, ax = plt.subplots(figsize=(8, 4))164    ax.plot(history.history['loss'], label='Training Loss')165    ax.plot(history.history['val_loss'], label='Validation Loss')166    ax.set_title('Loss over Epochs')167    ax.set_xlabel('Epochs')168    ax.set_ylabel('Loss')169    ax.legend()170    st.pyplot(fig)171 172    if problem_type != 'Regression':173        fig, ax = plt.subplots(figsize=(8, 4))174        ax.plot(history.history['accuracy'], label='Training Accuracy')175        ax.plot(history.history['val_accuracy'], label='Validation Accuracy')176        ax.set_title('Accuracy over Epochs')177        ax.set_xlabel('Epochs')178        ax.set_ylabel('Accuracy')179        ax.legend()180        st.pyplot(fig)181 182    # Evaluate the Model183    if problem_type == 'Regression':184        loss = model.evaluate(X_test, y_test, verbose=0)185        st.text(f"Test Loss: {loss}")186    else:187        accuracy = model.evaluate(X_test, y_test, verbose=0)[1]  # Assuming accuracy is the second element188        st.text(f"Test Accuracy: {accuracy}")189 190    # Decision Surface Plot191    def plot_decision_boundary(X, y):192        x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1193        y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1194        xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),195                             np.arange(y_min, y_max, 0.01))196        Z = model.predict(np.c_[xx.ravel(), yy.ravel()])197        Z = Z.reshape(xx.shape)198        return xx, yy, Z199 200    # Plot for training data201    xx_train, yy_train, Z_train = plot_decision_boundary(X_train, y_train)202    fig, ax = plt.subplots(figsize=(8, 4))203    ax.contourf(xx_train, yy_train, Z_train, alpha=0.8)204    scatter = ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, edgecolors='k', marker='o')205    ax.set_title('Decision Surface - Training Data')206    st.pyplot(fig)207 208    # Plot for testing data209    xx_test, yy_test, Z_test = plot_decision_boundary(X_test, y_test)210    fig, ax = plt.subplots(figsize=(8, 4))211    ax.contourf(xx_test, yy_test, Z_test, alpha=0.8)212    scatter = ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, edgecolors='k', marker='o')213    ax.set_title('Decision Surface - Testing Data')214    st.pyplot(fig)215 216    # Analyzing Overfitting and Underfitting217    train_score = model.evaluate(X_train, y_train, verbose=0)218    test_score = model.evaluate(X_test, y_test, verbose=0)219 220    st.text(f"Training Score: {train_score}")221    st.text(f"Testing Score: {test_score}")222 223    # Interpretation of Overfitting224    if problem_type == 'Regression':225        st.text("Evaluate loss scores to analyze overfitting.")226    else:227        st.text("Evaluate accuracy scores to analyze overfitting.")228        if train_score[1] > test_score[1]:  # Assuming accuracy is the second element229            st.text("The model may be overfitting, as training accuracy is higher than testing accuracy.")230        else:231            st.text("The model appears to be generalizing well.")232