Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from sklearn.preprocessing import StandardScaler
|
|
| 8 |
from mlxtend.plotting import plot_decision_regions
|
| 9 |
import tensorflow as tf
|
| 10 |
from keras.models import Sequential
|
| 11 |
-
from keras.layers import
|
| 12 |
from keras.optimizers import SGD
|
| 13 |
from keras.losses import MeanSquaredError, BinaryCrossentropy
|
| 14 |
from keras.regularizers import l2, l1
|
|
@@ -62,6 +62,11 @@ st.markdown("""
|
|
| 62 |
border-radius: 5px;
|
| 63 |
margin: 10px 0;
|
| 64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
</style>
|
| 66 |
""", unsafe_allow_html=True)
|
| 67 |
|
|
@@ -80,41 +85,41 @@ def reset_session():
|
|
| 80 |
st.session_state.num_hidden_layers = 2
|
| 81 |
st.session_state.hidden_layer_neurons = [4, 2]
|
| 82 |
|
| 83 |
-
# Top control bar
|
| 84 |
with st.container():
|
| 85 |
st.markdown('<div class="control-bar">', unsafe_allow_html=True)
|
| 86 |
col1, col2, col3, col4, col5, col6, col7, col8, col9 = st.columns(9)
|
| 87 |
with col1:
|
| 88 |
-
problem_type = st.selectbox("Problem", ["Classification", "Regression"]
|
| 89 |
with col2:
|
| 90 |
dataset_options = {"Classification": ["Blobs", "Circles", "Spirals", "XOR"], "Regression": ["Sine Wave"]}
|
| 91 |
-
dataset_type = st.selectbox("Dataset", dataset_options[problem_type]
|
| 92 |
with col3:
|
| 93 |
-
learning_rate = st.selectbox("Learning Rate", [0.0001, 0.001, 0.03, 0.1, 0.3, 1], index=2
|
| 94 |
with col4:
|
| 95 |
-
activation = st.selectbox("Activation", ["ReLU", "Sigmoid", "Tanh"], index=2
|
| 96 |
with col5:
|
| 97 |
-
batch_size = st.slider("Batch Size", 1, 30, 10
|
| 98 |
with col6:
|
| 99 |
-
noise_level = st.slider("Noise", 0, 50, 0, step=5
|
| 100 |
with col7:
|
| 101 |
-
reg_type = st.selectbox("Regularization", ["None", "L1", "L2"], index=0
|
| 102 |
with col8:
|
| 103 |
-
reg_rate = st.selectbox("Reg Rate", [0.0, 0.001, 0.01, 0.1, 1], index=0
|
| 104 |
with col9:
|
| 105 |
-
train_ratio = st.slider("Train %", 10, 90, 50, 10
|
| 106 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 107 |
|
| 108 |
# Dataset generation
|
| 109 |
def generate_xor(n_samples):
|
| 110 |
-
X = np.random.rand(n_samples, 2) * 2 - 1
|
| 111 |
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0).astype(int)
|
| 112 |
return X, y
|
| 113 |
|
| 114 |
def generate_sine_wave(n_samples, noise):
|
| 115 |
X = np.linspace(-3, 3, n_samples).reshape(-1, 1)
|
| 116 |
y = np.sin(X) + np.random.normal(0, noise / 100, X.shape)
|
| 117 |
-
return np.hstack([X, X**2]), y.ravel()
|
| 118 |
|
| 119 |
if problem_type == "Classification":
|
| 120 |
if dataset_type == "Blobs":
|
|
@@ -125,7 +130,7 @@ if problem_type == "Classification":
|
|
| 125 |
fv, cv = make_moons(n_samples=800, noise=noise_level / 250)
|
| 126 |
elif dataset_type == "XOR":
|
| 127 |
fv, cv = generate_xor(800)
|
| 128 |
-
else:
|
| 129 |
fv, cv = generate_sine_wave(800, noise_level)
|
| 130 |
|
| 131 |
# Feature preprocessing
|
|
@@ -165,23 +170,57 @@ with col_center:
|
|
| 165 |
st.markdown('<div class="panel">', unsafe_allow_html=True)
|
| 166 |
st.subheader("Network")
|
| 167 |
|
| 168 |
-
def draw_nn(features,
|
| 169 |
G = nx.DiGraph()
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
node_colors = {}
|
| 172 |
-
for layer_idx, layer in enumerate(
|
| 173 |
for node in layer:
|
| 174 |
G.add_node(node, layer=layer_idx)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
fig, ax = plt.subplots(figsize=(8, 4))
|
| 182 |
ax.set_facecolor("#252830")
|
| 183 |
-
nx.draw(
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return fig
|
| 186 |
|
| 187 |
st.pyplot(draw_nn(selected_features, st.session_state.hidden_layer_neurons))
|
|
|
|
| 8 |
from mlxtend.plotting import plot_decision_regions
|
| 9 |
import tensorflow as tf
|
| 10 |
from keras.models import Sequential
|
| 11 |
+
from keras.layers import InputIY Dense
|
| 12 |
from keras.optimizers import SGD
|
| 13 |
from keras.losses import MeanSquaredError, BinaryCrossentropy
|
| 14 |
from keras.regularizers import l2, l1
|
|
|
|
| 62 |
border-radius: 5px;
|
| 63 |
margin: 10px 0;
|
| 64 |
}
|
| 65 |
+
.stSelectbox label, .stSlider label {
|
| 66 |
+
color: white;
|
| 67 |
+
font-size: 12px;
|
| 68 |
+
font-weight: bold;
|
| 69 |
+
}
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
| 72 |
|
|
|
|
| 85 |
st.session_state.num_hidden_layers = 2
|
| 86 |
st.session_state.hidden_layer_neurons = [4, 2]
|
| 87 |
|
| 88 |
+
# Top control bar with labeled dropdowns and sliders
|
| 89 |
with st.container():
|
| 90 |
st.markdown('<div class="control-bar">', unsafe_allow_html=True)
|
| 91 |
col1, col2, col3, col4, col5, col6, col7, col8, col9 = st.columns(9)
|
| 92 |
with col1:
|
| 93 |
+
problem_type = st.selectbox("Problem Type", ["Classification", "Regression"])
|
| 94 |
with col2:
|
| 95 |
dataset_options = {"Classification": ["Blobs", "Circles", "Spirals", "XOR"], "Regression": ["Sine Wave"]}
|
| 96 |
+
dataset_type = st.selectbox("Dataset", dataset_options[problem_type])
|
| 97 |
with col3:
|
| 98 |
+
learning_rate = st.selectbox("Learning Rate", [0.0001, 0.001, 0.03, 0.1, 0.3, 1], index=2)
|
| 99 |
with col4:
|
| 100 |
+
activation = st.selectbox("Activation", ["ReLU", "Sigmoid", "Tanh"], index=2)
|
| 101 |
with col5:
|
| 102 |
+
batch_size = st.slider("Batch Size", 1, 30, 10)
|
| 103 |
with col6:
|
| 104 |
+
noise_level = st.slider("Noise", 0, 50, 0, step=5)
|
| 105 |
with col7:
|
| 106 |
+
reg_type = st.selectbox("Regularization", ["None", "L1", "L2"], index=0)
|
| 107 |
with col8:
|
| 108 |
+
reg_rate = st.selectbox("Reg Rate", [0.0, 0.001, 0.01, 0.1, 1], index=0)
|
| 109 |
with col9:
|
| 110 |
+
train_ratio = st.slider("Train %", 10, 90, 50, 10) / 100
|
| 111 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 112 |
|
| 113 |
# Dataset generation
|
| 114 |
def generate_xor(n_samples):
|
| 115 |
+
X = np.random.rand(n_samples, 2) * 2 - 1
|
| 116 |
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0).astype(int)
|
| 117 |
return X, y
|
| 118 |
|
| 119 |
def generate_sine_wave(n_samples, noise):
|
| 120 |
X = np.linspace(-3, 3, n_samples).reshape(-1, 1)
|
| 121 |
y = np.sin(X) + np.random.normal(0, noise / 100, X.shape)
|
| 122 |
+
return np.hstack([X, X**2]), y.ravel()
|
| 123 |
|
| 124 |
if problem_type == "Classification":
|
| 125 |
if dataset_type == "Blobs":
|
|
|
|
| 130 |
fv, cv = make_moons(n_samples=800, noise=noise_level / 250)
|
| 131 |
elif dataset_type == "XOR":
|
| 132 |
fv, cv = generate_xor(800)
|
| 133 |
+
else:
|
| 134 |
fv, cv = generate_sine_wave(800, noise_level)
|
| 135 |
|
| 136 |
# Feature preprocessing
|
|
|
|
| 170 |
st.markdown('<div class="panel">', unsafe_allow_html=True)
|
| 171 |
st.subheader("Network")
|
| 172 |
|
| 173 |
+
def draw_nn(features, hidden_neurons):
|
| 174 |
G = nx.DiGraph()
|
| 175 |
+
|
| 176 |
+
# Define layers
|
| 177 |
+
input_layer = features
|
| 178 |
+
hidden_layers = [[f"H{i+1}_{j+1}" for j in range(n)] for i, n in enumerate(hidden_neurons)]
|
| 179 |
+
output_layer = ["Output"]
|
| 180 |
+
all_layers = [input_layer] + hidden_layers + [output_layer]
|
| 181 |
+
|
| 182 |
+
# Add nodes with layer attribute
|
| 183 |
node_colors = {}
|
| 184 |
+
for layer_idx, layer in enumerate(all_layers):
|
| 185 |
for node in layer:
|
| 186 |
G.add_node(node, layer=layer_idx)
|
| 187 |
+
if layer_idx == 0:
|
| 188 |
+
node_colors[node] = "#90EE90" # Input: Green
|
| 189 |
+
elif layer_idx == len(all_layers) - 1:
|
| 190 |
+
node_colors[node] = "#FFA07A" # Output: Orange
|
| 191 |
+
else:
|
| 192 |
+
node_colors[node] = "#87CEFA" # Hidden: Blue
|
| 193 |
+
|
| 194 |
+
# Add edges (fully connected)
|
| 195 |
+
for i in range(len(all_layers) - 1):
|
| 196 |
+
for node1 in all_layers[i]:
|
| 197 |
+
for node2 in all_layers[i + 1]:
|
| 198 |
+
G.add_edge(node1, node2)
|
| 199 |
+
|
| 200 |
+
# Position nodes using multipartite layout
|
| 201 |
+
pos = nx.multipartite_layout(G, subset_key="layer", align="horizontal")
|
| 202 |
+
|
| 203 |
+
# Adjust positions for better spacing
|
| 204 |
+
for node in pos:
|
| 205 |
+
pos[node][1] *= 2 # Increase vertical spacing
|
| 206 |
+
|
| 207 |
+
# Draw the network
|
| 208 |
fig, ax = plt.subplots(figsize=(8, 4))
|
| 209 |
ax.set_facecolor("#252830")
|
| 210 |
+
nx.draw(
|
| 211 |
+
G, pos,
|
| 212 |
+
with_labels=True,
|
| 213 |
+
node_color=[node_colors[node] for node in G.nodes()],
|
| 214 |
+
edge_color="white",
|
| 215 |
+
node_size=600,
|
| 216 |
+
font_size=8,
|
| 217 |
+
font_color="black",
|
| 218 |
+
font_weight="bold",
|
| 219 |
+
edgecolors="black",
|
| 220 |
+
width=0.4,
|
| 221 |
+
ax=ax
|
| 222 |
+
)
|
| 223 |
+
plt.title("Neural Network Structure", color="white", fontsize=12, pad=10)
|
| 224 |
return fig
|
| 225 |
|
| 226 |
st.pyplot(draw_nn(selected_features, st.session_state.hidden_layer_neurons))
|