Create pages/Random_data.py
Browse files- pages/Random_data.py +104 -0
pages/Random_data.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
from sklearn.datasets import make_circles, make_moons, make_classification
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from keras.models import Sequential
|
| 8 |
+
from keras.layers import Dense
|
| 9 |
+
from keras.optimizers import SGD
|
| 10 |
+
from mlxtend.plotting import plot_decision_regions
|
| 11 |
+
import numpy as np
|
| 12 |
+
import tensorflow as tf
|
| 13 |
+
|
| 14 |
+
# Page title with new theme
|
| 15 |
+
st.markdown(
|
| 16 |
+
"<h1 style='text-align: center; color: #FF6347;'>๐ค Neural Network Playground</h1>",
|
| 17 |
+
unsafe_allow_html=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Sidebar configuration with new theme
|
| 21 |
+
st.sidebar.title("โ๏ธ Model Configuration")
|
| 22 |
+
|
| 23 |
+
# User input options in sidebar with theme
|
| 24 |
+
num_points = st.sidebar.slider("Number of Data Points", 100, 10000, 1000, step=100)
|
| 25 |
+
noise = st.sidebar.slider("Noise", 0.01, 0.9, 0.1)
|
| 26 |
+
batch_size = st.sidebar.slider("Batch Size", 1, 512, 32)
|
| 27 |
+
epochs = st.sidebar.slider("Epochs", 1, 100, 10)
|
| 28 |
+
learning_rate = st.sidebar.slider("Learning Rate", 0.0001, 1.0, 0.01, step=0.0001, format="%.4f")
|
| 29 |
+
hidden_layers = st.sidebar.slider("Hidden Layers", 1, 5, 2)
|
| 30 |
+
neurons_per_layer = st.sidebar.slider("Neurons per Layer", 1, 512, 32)
|
| 31 |
+
activation_name = st.sidebar.selectbox("Activation Function", ["relu", "tanh", "sigmoid", "linear"])
|
| 32 |
+
|
| 33 |
+
# Dataset selection with new theme
|
| 34 |
+
st.subheader("๐ Dataset Selection")
|
| 35 |
+
dataset_option = st.selectbox("Choose the dataset", ("circle", "moons", "classification"))
|
| 36 |
+
|
| 37 |
+
# Dataset generation based on user selection
|
| 38 |
+
if dataset_option == "circle":
|
| 39 |
+
x, y = make_circles(n_samples=num_points, noise=noise, factor=0.5, random_state=42)
|
| 40 |
+
elif dataset_option == "moons":
|
| 41 |
+
x, y = make_moons(n_samples=num_points, noise=noise, random_state=42)
|
| 42 |
+
else:
|
| 43 |
+
x, y = make_classification(n_samples=num_points, n_features=2, n_informative=2,
|
| 44 |
+
n_redundant=0, n_clusters_per_class=1, random_state=42)
|
| 45 |
+
|
| 46 |
+
# Submit button
|
| 47 |
+
if st.button("๐ Submit"):
|
| 48 |
+
st.subheader("๐ Input Data")
|
| 49 |
+
fig, ax = plt.subplots()
|
| 50 |
+
sns.scatterplot(x=x[:, 0], y=x[:, 1], hue=y, palette='Set2', ax=ax)
|
| 51 |
+
st.pyplot(fig)
|
| 52 |
+
|
| 53 |
+
# Train button with a fresh theme for model training
|
| 54 |
+
if st.button("๐ง Train the model"):
|
| 55 |
+
with st.spinner("โณ Training the model..."):
|
| 56 |
+
# Data split and scale
|
| 57 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1, stratify=y)
|
| 58 |
+
scaler = StandardScaler()
|
| 59 |
+
x_train = scaler.fit_transform(x_train)
|
| 60 |
+
x_test = scaler.transform(x_test)
|
| 61 |
+
|
| 62 |
+
# Model architecture
|
| 63 |
+
model = Sequential()
|
| 64 |
+
model.add(Dense(neurons_per_layer, input_shape=(2,), activation=activation_name))
|
| 65 |
+
for _ in range(hidden_layers - 1):
|
| 66 |
+
model.add(Dense(neurons_per_layer, activation=activation_name))
|
| 67 |
+
model.add(Dense(1, activation='sigmoid'))
|
| 68 |
+
|
| 69 |
+
# Compile and train
|
| 70 |
+
sgd = SGD(learning_rate=learning_rate)
|
| 71 |
+
model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
|
| 72 |
+
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2, verbose=0)
|
| 73 |
+
|
| 74 |
+
st.success("โ
Training Complete!")
|
| 75 |
+
|
| 76 |
+
# Show training plots with a fresh look
|
| 77 |
+
st.subheader("๐ Training Progress")
|
| 78 |
+
fig, ax = plt.subplots()
|
| 79 |
+
ax.plot(history.history['loss'], label='Training Loss')
|
| 80 |
+
ax.plot(history.history['val_loss'], label='Validation Loss')
|
| 81 |
+
ax.set_title("Training vs Validation Loss")
|
| 82 |
+
ax.set_xlabel("Epoch")
|
| 83 |
+
ax.legend()
|
| 84 |
+
st.pyplot(fig)
|
| 85 |
+
|
| 86 |
+
# Display final loss metrics
|
| 87 |
+
final_loss = history.history['loss'][-1]
|
| 88 |
+
final_val_loss = history.history['val_loss'][-1]
|
| 89 |
+
st.write(f"๐งฎ Final Training Loss: **{final_loss:.4f}**")
|
| 90 |
+
st.write(f"โ
Final Validation Loss: **{final_val_loss:.4f}**")
|
| 91 |
+
|
| 92 |
+
# Decision boundary visualization with a fresh UI
|
| 93 |
+
class KerasClassifierWrapper:
|
| 94 |
+
def __init__(self, model):
|
| 95 |
+
self.model = model
|
| 96 |
+
|
| 97 |
+
def predict(self, X):
|
| 98 |
+
return (self.model.predict(X) > 0.5).astype("int32")
|
| 99 |
+
|
| 100 |
+
with st.spinner("๐ฎ Generating decision boundary..."):
|
| 101 |
+
st.subheader("๐ Decision Boundary (Training Data)")
|
| 102 |
+
fig, ax = plt.subplots()
|
| 103 |
+
plot_decision_regions(X=x_train, y=y_train, clf=KerasClassifierWrapper(model), ax=ax)
|
| 104 |
+
st.pyplot(fig)
|