File size: 9,016 Bytes
22b4736
bb19d90
 
 
 
 
 
22b4736
bb19d90
 
 
 
 
 
 
 
22b4736
b56db27
22b4736
 
bb19d90
22b4736
bb19d90
 
 
22b4736
bb19d90
22b4736
bb19d90
 
 
 
0f0d590
 
 
bb19d90
 
 
 
 
22b4736
bb19d90
 
 
 
 
22b4736
bb19d90
 
 
 
 
22b4736
 
bb19d90
22b4736
bb19d90
 
22b4736
 
 
 
 
 
 
 
 
 
3fb743d
8b2c8ef
 
 
 
 
 
 
22b4736
 
 
 
 
 
bb19d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22b4736
 
 
 
bb19d90
 
22b4736
be3c4eb
 
e28e5fe
be3c4eb
 
 
 
22b4736
6ee0429
22b4736
 
 
 
 
 
 
 
bb19d90
 
22b4736
 
 
bb19d90
 
 
43ecb4e
22b4736
 
15ad1d6
 
22b4736
 
15ad1d6
 
bb19d90
 
 
22b4736
bb19d90
 
 
 
 
 
 
 
 
 
 
 
22b4736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb19d90
22b4736
 
 
 
bb19d90
22b4736
 
 
 
 
bb19d90
22b4736
 
 
 
bb19d90
 
 
 
 
 
 
22b4736
 
 
 
bb19d90
 
 
 
 
22b4736
 
f3d0df7
22b4736
 
 
bb19d90
f3d0df7
22b4736
 
 
 
 
 
 
 
 
 
 
 
 
 
bb19d90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Import required libraries
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import io
from sklearn.datasets import make_classification, make_moons, make_circles, make_regression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import InputLayer, Dense
from keras.regularizers import L1, L2
from mlxtend.plotting import plot_decision_regions
import warnings
warnings.filterwarnings("ignore")
from io import StringIO

# Main Title
st.title('Tensor Flow Playground')

# Sidebar Title
st.sidebar.title('Tensorflow Playground')

# Problem Type
problem_type = st.sidebar.selectbox('Problem Type', ['None', 'Classification', 'Regression', 'Moons', 'Circles'])

# Choose Dataset
st.sidebar.title('Choose Dataset')

# Datasets
data_set = st.sidebar.selectbox('Datasets', [
    '1.ushape.csv', '2.concerticcir1.csv', '3.concertriccir2.csv', 
    '4.linearsep.csv', '5.outlier.csv', '6.overlap.csv', 
    '7.xor.csv', '8.twospirals.csv', '9.random.csv', 'None'
])

# Learning Rate
learning_rate = st.sidebar.selectbox('Learning Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])

# Activation Function
activation_func = st.sidebar.selectbox('Activation', ['tanh', 'Sigmoid', 'linear', 'relu', 'softmax'])

# Regularization Rate
regularization_rate = st.sidebar.selectbox('Regularization Rate', [0.00001, 0.0001, 0.001, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])

# Regularization Type
regularization = st.sidebar.selectbox('Regularization', ['None', 'L1', 'L2'])

# Epochs
epochs = st.sidebar.select_slider("Select number of Epochs", options=[i for i in range(1, 1001)])

# Test Size
test_size = st.sidebar.slider("Test Size (%)", min_value=10, max_value=90, value=25, step=1) / 100

# Hidden Layers
hidden_layers = st.sidebar.select_slider('Hidden Layers', options=[i for i in range(1, 51)])

# Neurons in Each Layer
neurons_per_layer = []
for i in range(1, hidden_layers + 1):
    n = st.sidebar.text_input(f'No of Neurons in Layer {i}', '2')
    try:
        neurons_per_layer.append(int(n))
    except ValueError:
        st.error(f"Invalid input for the number of neurons in Layer {i}. Please enter an integer.")

# Batch Size
if data_set != 'None':
    try:
        # Read the CSV file using the provided file name
        df = pd.read_csv(data_set)
    except FileNotFoundError:
        st.error(f"File '{data_set}' not found. Please check the file name and try again.")
    except Exception as e:
        st.error(f"An error occurred: {e}")
    X = df.iloc[:, :2].values
    y = df.iloc[:, -1].values
    batch_size = st.sidebar.select_slider("Batch Size", options=[i for i in range(1, X.shape[0] + 1)])
else:
    batch_size = st.sidebar.select_slider("Batch Size", options=[i for i in range(1, 10001)])

# Regularization configuration
kernel_regularizer = None
bias_regularizer = None
if regularization == 'L1':
    kernel_regularizer = L1(regularization_rate)
    bias_regularizer = L1(regularization_rate)
elif regularization == 'L2':
    kernel_regularizer = L2(regularization_rate)
    bias_regularizer = L2(regularization_rate)

# Initialize session state
if 'model' not in st.session_state:
    st.session_state.model = None
if 'history' not in st.session_state:
    st.session_state.history = None
if 'X_train' not in st.session_state:
    st.session_state.X_train = None
if 'y_train' not in st.session_state:
    st.session_state.y_train = None
if 'X_test' not in st.session_state:
    st.session_state.X_test = None
if 'y_test' not in st.session_state:
    st.session_state.y_test = None

if st.sidebar.button('Submit'):
    if data_set != "None":
        try:
        # Read the CSV file using the provided file name
            df = pd.read_csv(data_set)
        except FileNotFoundError:
            st.error(f"File '{data_set}' not found. Please check the file name and try again.")
        except Exception as e:
            st.error(f"An error occurred: {e}")
        X = df.iloc[:, :2].values
        y = df.iloc[:, -1].values 
        problem_type = 'Classification'  # Treat as classification if dataset is chosen
    elif problem_type in ['Classification', 'Moons', 'Circles']:
        if problem_type == 'Classification':
            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)
        elif problem_type == 'Moons':
            X, y = make_moons(n_samples=10000, noise=0.1, random_state=20)
        elif problem_type == 'Circles':
            X, y = make_circles(n_samples=10000, noise=0.05, random_state=20)
    elif problem_type == 'Regression':
        X, y = make_regression(n_samples=10000, n_features=2, n_informative=2, n_targets=1, noise=0.05, random_state=20)
    else:
        st.write("Please select a valid dataset or problem type.")
        st.stop()

    # Data visualization
    st.subheader("Visualization of Data Points with Class Labels")
    fig, ax = plt.subplots(figsize=(10, 6))
    if problem_type in ['Classification', 'Moons', 'Circles']:
        sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y, ax=ax)
        ax.set_xlabel('Feature 1')
        ax.set_ylabel('Feature 2')
    else:
        sns.scatterplot(x=X[:, 0], y=X[:, 1], ax=ax)
        ax.set_xlabel('Feature 1')
        ax.set_ylabel('Feature 2')
    st.pyplot(fig)

    # Split train/test
    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)

    # Standardize
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)

    # Save model and training data in session state
    st.session_state.X_train = X_train
    st.session_state.y_train = y_train
    st.session_state.X_test = X_test
    st.session_state.y_test = y_test

    # Build the model
    model = Sequential()
    model.add(InputLayer(input_shape=(2,)))
    for neurons in neurons_per_layer:
        model.add(Dense(units=neurons, activation=activation_func, use_bias=True, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer))
    
    # Final layer configuration based on problem type
    if problem_type == 'Regression':
        model.add(Dense(units=1, activation='linear', use_bias=True))
        loss_function = 'mse'
        metrics = ['mse', 'mae']
    else: 
        model.add(Dense(units=1, activation='sigmoid', use_bias=True))
        loss_function = 'binary_crossentropy'
        metrics = ['accuracy']

    # Compile the model
    model.compile(optimizer='sgd', loss=loss_function, metrics=metrics)
        
    # Display model summary
    st.subheader("Model Summary")
    summary_str = StringIO()
    model.summary(print_fn=lambda x: summary_str.write(x + '\n'))
    st.text(summary_str.getvalue())

    # Training the model
    history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.2)

    # Save history in session state
    st.session_state.history = history

    # Plot loss and validation loss
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.plot(range(1, epochs + 1), history.history['loss'], label='Train loss')
    ax.plot(range(1, epochs + 1), history.history['val_loss'], label='Val loss')
    ax.set_title("Training and Validation Loss Analysis")
    ax.set_xlabel('Epochs')
    ax.set_ylabel('Loss')
    ax.legend()
    st.pyplot(fig)

    if problem_type != 'Regression':
        # Plot accuracy and validation accuracy
        fig, ax = plt.subplots(figsize=(10, 6))
        ax.plot(range(1, epochs + 1), history.history['accuracy'], label='Train Accuracy')
        ax.plot(range(1, epochs + 1), history.history['val_accuracy'], label='Val Accuracy')
        ax.set_title('Training and Validation Accuracy Analysis')
        ax.set_xlabel('Epochs')
        ax.set_ylabel('Accuracy')
        ax.legend()
        st.pyplot(fig)
        
        # Plot decision surface
        st.subheader('Decision Boundary on Training Data')
        fig, ax = plt.subplots(figsize=(10, 6))
        plot_decision_regions(X=st.session_state.X_train, y=st.session_state.y_train.astype(int), clf=model)
        st.pyplot(fig)

        st.subheader('Decision Boundary on Test Data')
        fig, ax = plt.subplots(figsize=(10, 6))
        plot_decision_regions(X=st.session_state.X_test, y=st.session_state.y_test.astype(int), clf=model)
        st.pyplot(fig)
    
    elif problem_type == 'Regression':
        # Plot accuracy and validation accuracy
        fig, ax = plt.subplots(figsize=(10, 6))
        ax.plot(range(1, epochs + 1), history.history['mae'], label='Train mae')
        ax.plot(range(1, epochs + 1), history.history['val_mae'], label='Val mae')
        ax.set_title('Training and Validation MAE Analysis')
        ax.set_xlabel('Epochs')
        ax.set_ylabel('MAE')
        ax.legend()
        st.pyplot(fig)