Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from sklearn.datasets import make_moons, make_circles, make_blobs
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow
|
7 |
+
from tensorflow import keras
|
8 |
+
# from tensorflow.keras.models import Sequential
|
9 |
+
# from tensorflow.keras.layers import Dense
|
10 |
+
# from tensorflow.keras.optimizers import Adam
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
|
13 |
+
|
14 |
+
st.title("Neural Network Hyperparameters")
|
15 |
+
|
16 |
+
# Dataset selection
|
17 |
+
dataset = st.selectbox("Select Dataset", ["moons", "circles", "blobs"])
|
18 |
+
|
19 |
+
# Learning rate
|
20 |
+
learning_rate = st.number_input("Learning Rate", value=0.01, format="%.5f")
|
21 |
+
|
22 |
+
# Activation function
|
23 |
+
activation = st.selectbox("Activation Function", ["relu", "sigmoid", "tanh"])
|
24 |
+
|
25 |
+
# Train-test split
|
26 |
+
split_ratio = st.slider("Train-Test Split Ratio", min_value=0.1, max_value=0.9, value=0.8)
|
27 |
+
|
28 |
+
# Batch size
|
29 |
+
batch_size = st.number_input("Batch Size", min_value=1, value=32)
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
# Generate dataset
|
34 |
+
def generate_data(dataset):
|
35 |
+
if dataset == "moons":
|
36 |
+
return make_moons(n_samples=1000, noise=0.2, random_state=42)
|
37 |
+
elif dataset == "circles":
|
38 |
+
return make_circles(n_samples=1000, noise=0.2, factor=0.5, random_state=42)
|
39 |
+
elif dataset == "blobs":
|
40 |
+
return make_blobs(n_samples=1000, centers=2, random_state=42, cluster_std=1.5)
|
41 |
+
|
42 |
+
X, y = generate_data(dataset)
|
43 |
+
X = StandardScaler().fit_transform(X)
|
44 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=(1 - split_ratio), random_state=42)
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
# Build model
|
49 |
+
model = keras.Sequential([
|
50 |
+
keras.layers.Dense(10, input_shape=(2,), activation=activation),
|
51 |
+
keras.layers.Dense(5, activation=activation),
|
52 |
+
keras.layers.Dense(1, activation="sigmoid") # binary classification
|
53 |
+
])
|
54 |
+
|
55 |
+
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
|
56 |
+
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
|
57 |
+
|
58 |
+
# Train model
|
59 |
+
history = model.fit(X_train, y_train, epochs=100, batch_size=batch_size,
|
60 |
+
validation_data=(X_test, y_test), verbose=0)
|
61 |
+
|
62 |
+
|
63 |
+
#4. Training vs Testing Error Plot
|
64 |
+
def plot_loss(history):
|
65 |
+
plt.figure(figsize=(8, 4))
|
66 |
+
plt.plot(history.history['loss'], label='Train Loss')
|
67 |
+
plt.plot(history.history['val_loss'], label='Test Loss')
|
68 |
+
plt.xlabel("Epochs")
|
69 |
+
plt.ylabel("Loss")
|
70 |
+
plt.legend()
|
71 |
+
plt.title("Training vs Testing Loss")
|
72 |
+
st.pyplot(plt)
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
def plot_decision_boundary(model, X, y):
|
77 |
+
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
|
78 |
+
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
|
79 |
+
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 300),
|
80 |
+
np.linspace(y_min, y_max, 300))
|
81 |
+
grid = np.c_[xx.ravel(), yy.ravel()]
|
82 |
+
preds = model.predict(grid)
|
83 |
+
preds = preds.reshape(xx.shape)
|
84 |
+
|
85 |
+
plt.figure(figsize=(8, 6))
|
86 |
+
plt.contourf(xx, yy, preds, alpha=0.7, cmap=plt.cm.RdBu)
|
87 |
+
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, edgecolors='white')
|
88 |
+
plt.title("Decision Boundary")
|
89 |
+
st.pyplot(plt)
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
if st.button("Train Model"):
|
94 |
+
st.title("Neural Network Training Visualizer")
|
95 |
+
with st.spinner("Training the model..."):
|
96 |
+
# Call training functions
|
97 |
+
plot_loss(history)
|
98 |
+
plot_decision_boundary(model, X, y)
|