CoolFace
Apppublic

Gova823/Tensorflow_Playground

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py233 linesDownload Raw Back to root
1# Import required libraries2import streamlit as st3import pandas as pd4import numpy as np5import seaborn as sns6import matplotlib.pyplot as plt7import io8from sklearn.datasets import make_classification, make_moons, make_circles, make_regression9from sklearn.model_selection import train_test_split10from sklearn.preprocessing import StandardScaler11from keras.models import Sequential12from keras.layers import InputLayer, Dense13from keras.regularizers import L1, L214from mlxtend.plotting import plot_decision_regions15import warnings16warnings.filterwarnings("ignore")17from io import StringIO18 19# Main Title20st.title('Tensor Flow Playground')21 22# Sidebar Title23st.sidebar.title('Tensorflow Playground')24 25# Problem Type26problem_type = st.sidebar.selectbox('Problem Type', ['None', 'Classification', 'Regression', 'Moons', 'Circles'])27 28# Choose Dataset29st.sidebar.title('Choose Dataset')30 31# Datasets32data_set = st.sidebar.selectbox('Datasets', [33    '1.ushape.csv', '2.concerticcir1.csv', '3.concertriccir2.csv', 34    '4.linearsep.csv', '5.outlier.csv', '6.overlap.csv', 35    '7.xor.csv', '8.twospirals.csv', '9.random.csv', 'None'36])37 38# Learning Rate39learning_rate = st.sidebar.selectbox('Learning Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])40 41# Activation Function42activation_func = st.sidebar.selectbox('Activation', ['tanh', 'Sigmoid', 'linear', 'relu', 'softmax'])43 44# Regularization Rate45regularization_rate = st.sidebar.selectbox('Regularization Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])46 47# Regularization Type48regularization = st.sidebar.selectbox('Regularization', ['None', 'L1', 'L2'])49 50# Epochs51epochs = st.sidebar.select_slider("Select number of Epochs", options=[i for i in range(1, 1001)])52 53# Test Size54test_size = st.sidebar.slider("Test Size (%)", min_value=10, max_value=90, value=25, step=1) / 10055 56# Hidden Layers57hidden_layers = st.sidebar.select_slider('Hidden Layers', options=[i for i in range(1, 51)])58 59# Neurons in Each Layer60neurons_per_layer = []61for i in range(1, hidden_layers + 1):62    n = st.sidebar.text_input(f'No of Neurons in Layer {i}', '2')63    try:64        neurons_per_layer.append(int(n))65    except ValueError:66        st.error(f"Invalid input for the number of neurons in Layer {i}. Please enter an integer.")67 68# Batch Size69if data_set != 'None':70    try:71        # Read the CSV file using the provided file name72        df = pd.read_csv(data_set)73    except FileNotFoundError:74        st.error(f"File '{data_set}' not found. Please check the file name and try again.")75    except Exception as e:76        st.error(f"An error occurred: {e}")77    X = df.iloc[:, :2].values78    y = df.iloc[:, -1].values79    batch_size = st.sidebar.select_slider("Batch Size", options=[i for i in range(1, X.shape[0] + 1)])80else:81    batch_size = st.sidebar.select_slider("Batch Size", options=[i for i in range(1, 10001)])82 83# Regularization configuration84kernel_regularizer = None85bias_regularizer = None86if regularization == 'L1':87    kernel_regularizer = L1(regularization_rate)88    bias_regularizer = L1(regularization_rate)89elif regularization == 'L2':90    kernel_regularizer = L2(regularization_rate)91    bias_regularizer = L2(regularization_rate)92 93# Initialize session state94if 'model' not in st.session_state:95    st.session_state.model = None96if 'history' not in st.session_state:97    st.session_state.history = None98if 'X_train' not in st.session_state:99    st.session_state.X_train = None100if 'y_train' not in st.session_state:101    st.session_state.y_train = None102if 'X_test' not in st.session_state:103    st.session_state.X_test = None104if 'y_test' not in st.session_state:105    st.session_state.y_test = None106 107if st.sidebar.button('Submit'):108    if data_set != "None":109        try:110        # Read the CSV file using the provided file name111            df = pd.read_csv(data_set)112        except FileNotFoundError:113            st.error(f"File '{data_set}' not found. Please check the file name and try again.")114        except Exception as e:115            st.error(f"An error occurred: {e}")116        X = df.iloc[:, :2].values117        y = df.iloc[:, -1].values 118        problem_type = 'Classification'  # Treat as classification if dataset is chosen119    elif problem_type in ['Classification', 'Moons', 'Circles']:120        if problem_type == 'Classification':121            X, y = make_classification(n_samples=10000, n_features=2, n_informative=2, n_redundant=0, n_repeated=0, n_classes=2, class_sep=2.5, random_state=10)122        elif problem_type == 'Moons':123            X, y = make_moons(n_samples=10000, noise=0.1, random_state=20)124        elif problem_type == 'Circles':125            X, y = make_circles(n_samples=10000, noise=0.05, random_state=20)126    elif problem_type == 'Regression':127        X, y = make_regression(n_samples=10000, n_features=2, n_informative=2, n_targets=1, noise=0.05, random_state=20)128    else:129        st.write("Please select a valid dataset or problem type.")130        st.stop()131 132    # Data visualization133    st.subheader("Visualization of Data Points with Class Labels")134    fig, ax = plt.subplots(figsize=(10, 6))135    if problem_type in ['Classification', 'Moons', 'Circles']:136        sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y, ax=ax)137        ax.set_xlabel('Feature 1')138        ax.set_ylabel('Feature 2')139    else:140        sns.scatterplot(x=X[:, 0], y=X[:, 1], ax=ax)141        ax.set_xlabel('Feature 1')142        ax.set_ylabel('Feature 2')143    st.pyplot(fig)144 145    # Split train/test146    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)147 148    # Standardize149    scaler = StandardScaler()150    X_train = scaler.fit_transform(X_train)151    X_test = scaler.transform(X_test)152 153    # Save model and training data in session state154    st.session_state.X_train = X_train155    st.session_state.y_train = y_train156    st.session_state.X_test = X_test157    st.session_state.y_test = y_test158 159    # Build the model160    model = Sequential()161    model.add(InputLayer(input_shape=(2,)))162    for neurons in neurons_per_layer:163        model.add(Dense(units=neurons, activation=activation_func, use_bias=True, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer))164    165    # Final layer configuration based on problem type166    if problem_type == 'Regression':167        model.add(Dense(units=1, activation='linear', use_bias=True))168        loss_function = 'mse'169        metrics = ['mse', 'mae']170    else: 171        model.add(Dense(units=1, activation='sigmoid', use_bias=True))172        loss_function = 'binary_crossentropy'173        metrics = ['accuracy']174 175    # Compile the model176    model.compile(optimizer='sgd', loss=loss_function, metrics=metrics)177        178    # Display model summary179    st.subheader("Model Summary")180    summary_str = StringIO()181    model.summary(print_fn=lambda x: summary_str.write(x + '\n'))182    st.text(summary_str.getvalue())183 184    # Training the model185    history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.2)186 187    # Save history in session state188    st.session_state.history = history189 190    # Plot loss and validation loss191    fig, ax = plt.subplots(figsize=(10, 6))192    ax.plot(range(1, epochs + 1), history.history['loss'], label='Train loss')193    ax.plot(range(1, epochs + 1), history.history['val_loss'], label='Val loss')194    ax.set_title("Training and Validation Loss Analysis")195    ax.set_xlabel('Epochs')196    ax.set_ylabel('Loss')197    ax.legend()198    st.pyplot(fig)199 200    if problem_type != 'Regression':201        # Plot accuracy and validation accuracy202        fig, ax = plt.subplots(figsize=(10, 6))203        ax.plot(range(1, epochs + 1), history.history['accuracy'], label='Train Accuracy')204        ax.plot(range(1, epochs + 1), history.history['val_accuracy'], label='Val Accuracy')205        ax.set_title('Training and Validation Accuracy Analysis')206        ax.set_xlabel('Epochs')207        ax.set_ylabel('Accuracy')208        ax.legend()209        st.pyplot(fig)210        211        # Plot decision surface212        st.subheader('Decision Boundary on Training Data')213        fig, ax = plt.subplots(figsize=(10, 6))214        plot_decision_regions(X=st.session_state.X_train, y=st.session_state.y_train.astype(int), clf=model)215        st.pyplot(fig)216 217        st.subheader('Decision Boundary on Test Data')218        fig, ax = plt.subplots(figsize=(10, 6))219        plot_decision_regions(X=st.session_state.X_test, y=st.session_state.y_test.astype(int), clf=model)220        st.pyplot(fig)221    222    elif problem_type == 'Regression':223        # Plot accuracy and validation accuracy224        fig, ax = plt.subplots(figsize=(10, 6))225        ax.plot(range(1, epochs + 1), history.history['mae'], label='Train mae')226        ax.plot(range(1, epochs + 1), history.history['val_mae'], label='Val mae')227        ax.set_title('Training and Validation MAE Analysis')228        ax.set_xlabel('Epochs')229        ax.set_ylabel('MAE')230        ax.legend()231        st.pyplot(fig)232 233