Spaces:
Sleeping
Sleeping
Update Home.py
Browse files
Home.py
CHANGED
|
@@ -1,192 +1,267 @@
|
|
| 1 |
-
pip install scikit-learn
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from
|
| 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 |
-
reg = regularizers.l1(reg_v)
|
| 120 |
-
elif reg_t == "L2":
|
| 121 |
-
reg = regularizers.l2(reg_v)
|
| 122 |
-
else:
|
| 123 |
-
reg = regularizers.l1_l2(reg_v, reg_v)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# Add hidden layers
|
| 130 |
-
for _ in range(n_hidden):
|
| 131 |
-
model.add(
|
| 132 |
-
layers.Dense(
|
| 133 |
-
n_neurons,
|
| 134 |
-
activation=activation,
|
| 135 |
-
kernel_regularizer=reg
|
| 136 |
-
)
|
| 137 |
-
)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
if st.button("Train"):
|
| 154 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import graphviz
|
| 6 |
+
import time
|
| 7 |
+
from sklearn.datasets import make_moons, make_circles, make_classification
|
| 8 |
+
from sklearn.datasets import make_regression
|
| 9 |
+
|
| 10 |
+
# Set Streamlit page title
|
| 11 |
+
st.set_page_config(page_title="Neural Network Trainer", layout="wide")
|
| 12 |
+
|
| 13 |
+
# ================= Session State for Training Controls =================
|
| 14 |
+
if "epoch" not in st.session_state:
|
| 15 |
+
st.session_state.epoch = 0
|
| 16 |
+
if "running" not in st.session_state:
|
| 17 |
+
st.session_state.running = False
|
| 18 |
+
|
| 19 |
+
# ================= TRAINING CONTROL PANEL (Top) =================
|
| 20 |
+
st.markdown("### Training Controls")
|
| 21 |
+
col1, col2, col3, col4, col5, col6, col7, col8, col9 = st.columns(9)
|
| 22 |
+
|
| 23 |
+
with col1:
|
| 24 |
+
if st.button("↩️ Reset"):
|
| 25 |
+
st.session_state.epoch = 0
|
| 26 |
+
st.session_state.running = False
|
| 27 |
+
with col2:
|
| 28 |
+
if st.button("▶️ Train"):
|
| 29 |
+
st.session_state.running = True
|
| 30 |
+
with col3:
|
| 31 |
+
if st.button("⏸️ Pause"):
|
| 32 |
+
st.session_state.running = False
|
| 33 |
+
with col4:
|
| 34 |
+
activation = st.selectbox("Activation", ["ReLU", "Sigmoid", "Tanh", "LeakyReLU"])
|
| 35 |
+
with col5:
|
| 36 |
+
regularization = st.selectbox("Regularization", ["None", "L1", "L2"])
|
| 37 |
+
with col6:
|
| 38 |
+
reg_rate = st.selectbox("Regularization Rate", [0.0001, 0.001, 0.01, 0.1]) if regularization in ["L1", "L2"] else 0
|
| 39 |
+
with col7:
|
| 40 |
+
problem_type = st.selectbox("Problem Type", ["Classification", "Regression"])
|
| 41 |
+
with col8:
|
| 42 |
+
learning_rate = st.selectbox("Learning Rate", [0.0001, 0.001, 0.01, 0.03, 0.1])
|
| 43 |
+
with col9:
|
| 44 |
+
st.write(f"Epoch: {st.session_state.epoch}")
|
| 45 |
+
|
| 46 |
+
# 🚀 Fix: Run training loop without breaking Streamlit
|
| 47 |
+
if st.session_state.running:
|
| 48 |
+
time.sleep(1) # Simulating training
|
| 49 |
+
st.session_state.epoch += 1
|
| 50 |
+
|
| 51 |
+
# ================= MAIN LAYOUT =================
|
| 52 |
+
col_features, col_hidden, col_output = st.columns([2, 2, 2])
|
| 53 |
+
|
| 54 |
+
# ========== FEATURE SELECTION MOVED TO MIDDLE ==========
|
| 55 |
+
with col_features:
|
| 56 |
+
st.header("FEATURE SELECTION")
|
| 57 |
+
feature_dict = {
|
| 58 |
+
"X₁": st.checkbox("X₁", value=True),
|
| 59 |
+
"X₂": st.checkbox("X₂", value=True),
|
| 60 |
+
"X₁²": st.checkbox("X₁²"),
|
| 61 |
+
"X₂²": st.checkbox("X₂²"),
|
| 62 |
+
"X₁X₂": st.checkbox("X₁X₂"),
|
| 63 |
+
"sin(X₁)": st.checkbox("sin(X₁)"),
|
| 64 |
+
"sin(X₂)": st.checkbox("sin(X₂)"),
|
| 65 |
+
}
|
| 66 |
+
selected_features = [f for f, v in feature_dict.items() if v]
|
| 67 |
+
|
| 68 |
+
# ========== HIDDEN LAYERS PANEL (Middle) ========== #
|
| 69 |
+
with col_hidden:
|
| 70 |
+
st.header("HIDDEN LAYERS")
|
| 71 |
+
hidden_layers = st.slider("Number of Hidden Layers", 1, 7, 2)
|
| 72 |
+
|
| 73 |
+
neurons = []
|
| 74 |
+
for i in range(hidden_layers):
|
| 75 |
+
neurons.append(st.slider(f"Neurons in Layer {i+1}", 1, 20, 4))
|
| 76 |
+
|
| 77 |
+
# ========== OUTPUT PANEL (Right) ========== #
|
| 78 |
+
with col_output:
|
| 79 |
+
st.header("OUTPUT")
|
| 80 |
+
st.write("Test Loss: 0.501")
|
| 81 |
+
st.write("Training Loss: 0.507")
|
| 82 |
+
|
| 83 |
+
# Spiral Plot with Updated Color Palette
|
| 84 |
+
x = np.linspace(-6, 6, 300)
|
| 85 |
+
y = np.sin(x) + np.random.normal(0, 0.1, x.shape)
|
| 86 |
+
|
| 87 |
+
fig, ax = plt.subplots()
|
| 88 |
+
sns.scatterplot(x=x, y=y, hue=x, palette="plasma", ax=ax)
|
| 89 |
+
st.pyplot(fig)
|
| 90 |
+
|
| 91 |
+
show_test_data = st.checkbox("Show test data")
|
| 92 |
+
discretize_output = st.checkbox("Discretize output")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
|
| 96 |
+
# Sidebar for dataset selection
|
| 97 |
+
st.sidebar.header("Dataset Selection")
|
| 98 |
+
data_type = st.sidebar.radio("Choose Data Type", ["Classification", "Regression"])
|
| 99 |
+
|
| 100 |
+
# Generate classification data
|
| 101 |
+
def generate_classification_data():
|
| 102 |
+
st.sidebar.subheader("Classification Settings")
|
| 103 |
+
dataset_type = st.sidebar.selectbox("Dataset Type", ["Moons", "Circles", "Classification"])
|
| 104 |
+
noise = st.sidebar.slider("Noise Level", 0.0, 1.0, 0.2, step=0.05)
|
| 105 |
+
samples = st.sidebar.slider("Number of Samples", 100, 1000, 500, step=50)
|
| 106 |
+
|
| 107 |
+
if dataset_type == "Moons":
|
| 108 |
+
X, y = make_moons(n_samples=samples, noise=noise)
|
| 109 |
+
elif dataset_type == "Circles":
|
| 110 |
+
X, y = make_circles(n_samples=samples, noise=noise, factor=0.5)
|
| 111 |
+
else:
|
| 112 |
+
X, y = make_classification(n_samples=samples, n_features=2, n_classes=2, n_clusters_per_class=1, flip_y=noise)
|
| 113 |
+
|
| 114 |
+
return X, y
|
| 115 |
+
|
| 116 |
+
# Generate regression data
|
| 117 |
+
def generate_regression_data():
|
| 118 |
+
st.sidebar.subheader("Regression Settings")
|
| 119 |
+
samples = st.sidebar.slider("Number of Samples", 100, 1000, 500, step=50)
|
| 120 |
+
noise = st.sidebar.slider("Noise Level", 0.0, 10.0, 2.0, step=0.5)
|
| 121 |
+
|
| 122 |
+
X, y = make_regression(n_samples=samples, n_features=1, noise=noise)
|
| 123 |
+
return X, y
|
| 124 |
+
|
| 125 |
+
# Select dataset type
|
| 126 |
+
if data_type == "Classification":
|
| 127 |
+
X, y = generate_classification_data()
|
| 128 |
+
cmap = "coolwarm"
|
| 129 |
+
title = "Classification Data"
|
| 130 |
+
is_classification = True
|
| 131 |
+
else:
|
| 132 |
+
X, y = generate_regression_data()
|
| 133 |
+
cmap = "plasma"
|
| 134 |
+
title = "Regression Data"
|
| 135 |
+
is_classification = False
|
| 136 |
+
|
| 137 |
+
# 🎯 Reduced Size of the Plot
|
| 138 |
+
fig, ax = plt.subplots(figsize=(4, 2)) # Smaller size (width=4, height=2)
|
| 139 |
+
|
| 140 |
+
if is_classification:
|
| 141 |
+
scatter = ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap, edgecolors="white", alpha=0.8)
|
| 142 |
+
ax.set_xlabel("Feature 1", fontsize=8)
|
| 143 |
+
ax.set_ylabel("Feature 2", fontsize=8)
|
| 144 |
+
else:
|
| 145 |
+
scatter = ax.scatter(X[:, 0], y, c=y, cmap=cmap, edgecolors="white", alpha=0.8)
|
| 146 |
+
sns.kdeplot(x=X[:, 0], y=y, fill=True, cmap=cmap, alpha=0.3, ax=ax)
|
| 147 |
+
ax.set_xlabel("Feature 1", fontsize=8)
|
| 148 |
+
ax.set_ylabel("Target", fontsize=8)
|
| 149 |
+
|
| 150 |
+
ax.set_title(title, fontsize=10)
|
| 151 |
+
ax.tick_params(axis='both', labelsize=7)
|
| 152 |
+
ax.grid(True, linewidth=0.5)
|
| 153 |
+
|
| 154 |
+
# Display in Streamlit
|
| 155 |
+
st.pyplot(fig)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# ================= NEURAL NETWORK VISUALIZATION =================
|
| 159 |
+
def draw_neural_network():
|
| 160 |
+
graph = graphviz.Digraph(engine="dot")
|
| 161 |
+
|
| 162 |
+
# Input Layer (Features)
|
| 163 |
+
input_nodes = []
|
| 164 |
+
for feature in selected_features:
|
| 165 |
+
graph.node(feature, feature, shape="circle", style="filled", fillcolor="lightblue", width="0.6", height="0.6")
|
| 166 |
+
input_nodes.append(feature)
|
| 167 |
+
|
| 168 |
+
# Hidden Layers
|
| 169 |
+
prev_layer = input_nodes
|
| 170 |
+
hidden_layers_nodes = []
|
| 171 |
+
|
| 172 |
+
for i, num_neurons in enumerate(neurons):
|
| 173 |
+
layer_nodes = [f"H{i+1}_{j+1}" for j in range(num_neurons)]
|
| 174 |
+
hidden_layers_nodes.append(layer_nodes)
|
| 175 |
+
|
| 176 |
+
for node in layer_nodes:
|
| 177 |
+
graph.node(node, node, shape="circle", style="filled", fillcolor="lightyellow", width="0.6", height="0.6")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
# Connect previous layer to this hidden layer
|
| 180 |
+
for prev in prev_layer:
|
| 181 |
+
for curr in layer_nodes:
|
| 182 |
+
graph.edge(prev, curr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
prev_layer = layer_nodes # Update previous layer for next iteration
|
| 185 |
+
|
| 186 |
+
# Output Layer
|
| 187 |
+
graph.node("Output", "Output", shape="circle", style="filled", fillcolor="lightgreen", width="0.6", height="0.6")
|
| 188 |
+
|
| 189 |
+
# Connect last hidden layer to output
|
| 190 |
+
for last_hidden in prev_layer:
|
| 191 |
+
graph.edge(last_hidden, "Output")
|
| 192 |
+
|
| 193 |
+
graph.attr(rankdir="LR") # Make it horizontal (Left to Right)
|
| 194 |
+
|
| 195 |
+
return graph
|
| 196 |
+
|
| 197 |
+
# =================== DISPLAY NEURAL NETWORK ===================
|
| 198 |
+
st.graphviz_chart(draw_neural_network())
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
# =================== DISPLAY DATA PLOT ===================
|
| 202 |
+
st.sidebar.subheader("Dataset Visualization")
|
| 203 |
+
fig, ax = plt.subplots()
|
| 204 |
+
ax.scatter(X[:, 0], X[:, 1], c=y, cmap="plasma", edgecolors="k")
|
| 205 |
+
st.sidebar.pyplot(fig)
|
| 206 |
+
import streamlit as st
|
| 207 |
+
import numpy as np
|
| 208 |
+
import matplotlib.pyplot as plt
|
| 209 |
+
import time
|
| 210 |
+
|
| 211 |
+
# Initialize session state
|
| 212 |
+
if "epoch" not in st.session_state:
|
| 213 |
+
st.session_state.epoch = 0
|
| 214 |
+
if "running" not in st.session_state:
|
| 215 |
+
st.session_state.running = False
|
| 216 |
+
if "loss_history" not in st.session_state:
|
| 217 |
+
st.session_state.loss_history = []
|
| 218 |
+
|
| 219 |
+
# Training controls
|
| 220 |
+
col1, col2, col3 = st.columns(3)
|
| 221 |
+
with col1:
|
| 222 |
+
if st.button("Reset"):
|
| 223 |
+
st.session_state.epoch = 0
|
| 224 |
+
st.session_state.running = False
|
| 225 |
+
st.session_state.loss_history = []
|
| 226 |
+
with col2:
|
| 227 |
if st.button("Train"):
|
| 228 |
+
st.session_state.running = True
|
| 229 |
+
with col3:
|
| 230 |
+
if st.button("Pause"):
|
| 231 |
+
st.session_state.running = False
|
| 232 |
+
|
| 233 |
+
# Training loop simulation
|
| 234 |
+
if st.session_state.running:
|
| 235 |
+
for _ in range(10):
|
| 236 |
+
time.sleep(0.5)
|
| 237 |
+
st.session_state.epoch += 1
|
| 238 |
+
simulated_loss = np.exp(-0.1 * st.session_state.epoch) + np.random.normal(0, 0.02)
|
| 239 |
+
st.session_state.loss_history.append(simulated_loss)
|
| 240 |
+
|
| 241 |
+
# Epoch vs Training Loss Plot (Smaller Size)
|
| 242 |
+
st.header("Epoch vs Training Loss")
|
| 243 |
+
fig, ax = plt.subplots(figsize=(4, 2)) # Reduce plot size (width=4, height=2)
|
| 244 |
+
ax.plot(range(1, len(st.session_state.loss_history) + 1), st.session_state.loss_history, marker="o", linestyle="-", color="blue")
|
| 245 |
+
ax.set_xlabel("Epoch")
|
| 246 |
+
ax.set_ylabel("Training Loss")
|
| 247 |
+
ax.set_title("Training Loss Over Epochs", fontsize=10)
|
| 248 |
+
ax.tick_params(axis='both', labelsize=8)
|
| 249 |
+
ax.grid(True, linewidth=0.5)
|
| 250 |
+
|
| 251 |
+
st.pyplot(fig)
|
| 252 |
+
|
| 253 |
+
# Display current epoch and training loss below the plot
|
| 254 |
+
if st.session_state.loss_history:
|
| 255 |
+
st.write(f"Epoch: {st.session_state.epoch}")
|
| 256 |
+
st.write(f"Training Loss: {st.session_state.loss_history[-1]:.4f}")
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
# Display current epoch and training loss below the plot
|
| 260 |
+
if st.session_state.loss_history:
|
| 261 |
+
st.write(f"Epoch: {st.session_state.epoch}")
|
| 262 |
+
st.write(f"Training Loss: {st.session_state.loss_history[-1]:.4f}")
|
| 263 |
+
# =================== TRAINING STATUS ===================
|
| 264 |
+
if st.session_state.running:
|
| 265 |
+
st.write("🚀 Training started...")
|
| 266 |
+
elif not st.session_state.running and st.session_state.epoch > 0:
|
| 267 |
+
st.write("⏸️ Training paused.")
|