Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from tensorflow.keras import layers
|
|
| 12 |
st.title("Neural Network Playground")
|
| 13 |
|
| 14 |
# Navigation Bar
|
| 15 |
-
col1, col2, col3, col4, col5, col6 = st.columns(
|
| 16 |
|
| 17 |
with col1:
|
| 18 |
epochs = st.selectbox("Epochs", [100, 200, 500, 800, 1000, 1500, 2000])
|
|
@@ -27,6 +27,8 @@ with col5:
|
|
| 27 |
problem_type = st.selectbox("Problem Type", ["Classification", "Regression"])
|
| 28 |
with col6:
|
| 29 |
play = st.button("Train Model")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Dataset Selection & Preprocessing
|
| 32 |
st.subheader("Dataset Selection & Preprocessing")
|
|
@@ -34,78 +36,69 @@ dataset_type = st.selectbox("Select Dataset", ["Binary Classification", "XOR", "
|
|
| 34 |
test_ratio = st.slider("Train-Test Split Ratio", 0.1, 0.5, 0.2, step=0.05)
|
| 35 |
batch_size = st.slider("Batch Size", 4, 64, 16, step=2)
|
| 36 |
|
| 37 |
-
|
| 38 |
X_train, X_test, y_train, y_test = None, None, None, None
|
|
|
|
| 39 |
if st.button("Generate Dataset"):
|
| 40 |
if dataset_type == "Binary Classification":
|
| 41 |
-
|
| 42 |
elif dataset_type == "XOR":
|
| 43 |
-
|
| 44 |
elif dataset_type == "Binary Spiral":
|
| 45 |
-
|
| 46 |
elif dataset_type == "Regression 1":
|
| 47 |
-
|
| 48 |
elif dataset_type == "Regression 2":
|
| 49 |
X = np.linspace(-1, 1, 1000).reshape(-1, 1)
|
| 50 |
y = X ** 3 + 0.1 * np.random.randn(1000, 1).flatten()
|
| 51 |
-
data = (X, y)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.
|
| 63 |
-
|
| 64 |
-
hidden_layers = []
|
| 65 |
-
for i in range(num_layers):
|
| 66 |
-
neurons = st.slider(f"Neurons in Layer {i+1}", 2, 20, 5)
|
| 67 |
-
hidden_layers.append(neurons)
|
| 68 |
-
|
| 69 |
-
def draw_nn(hidden_layers):
|
| 70 |
-
G = nx.DiGraph()
|
| 71 |
-
layer_sizes = [2] + hidden_layers + [1]
|
| 72 |
-
pos = {}
|
| 73 |
-
|
| 74 |
-
for i, size in enumerate(layer_sizes):
|
| 75 |
-
for j in range(size):
|
| 76 |
-
G.add_node(f"L{i}_N{j}", layer=i, pos=(i, -j))
|
| 77 |
-
if i > 0:
|
| 78 |
-
for k in range(layer_sizes[i-1]):
|
| 79 |
-
G.add_edge(f"L{i-1}_N{k}", f"L{i}_N{j}")
|
| 80 |
-
|
| 81 |
-
pos = nx.get_node_attributes(G, 'pos')
|
| 82 |
-
plt.figure(figsize=(8, 5))
|
| 83 |
-
nx.draw(G, pos, with_labels=False, node_size=600, node_color="lightblue", edge_color="gray")
|
| 84 |
-
st.pyplot(plt)
|
| 85 |
-
|
| 86 |
-
draw_nn(hidden_layers)
|
| 87 |
|
| 88 |
# Model Training
|
| 89 |
-
if play:
|
| 90 |
model = keras.Sequential()
|
| 91 |
-
model.add(layers.InputLayer(input_shape=(
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
model.add(layers.Dense(1, activation="sigmoid" if problem_type == "Classification" else "linear"))
|
| 95 |
-
model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate),
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
st.success("Model Training Complete")
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
st.title("Neural Network Playground")
|
| 13 |
|
| 14 |
# Navigation Bar
|
| 15 |
+
col1, col2, col3, col4, col5, col6, col7 = st.columns(7)
|
| 16 |
|
| 17 |
with col1:
|
| 18 |
epochs = st.selectbox("Epochs", [100, 200, 500, 800, 1000, 1500, 2000])
|
|
|
|
| 27 |
problem_type = st.selectbox("Problem Type", ["Classification", "Regression"])
|
| 28 |
with col6:
|
| 29 |
play = st.button("Train Model")
|
| 30 |
+
with col7:
|
| 31 |
+
epoch_counter = st.empty()
|
| 32 |
|
| 33 |
# Dataset Selection & Preprocessing
|
| 34 |
st.subheader("Dataset Selection & Preprocessing")
|
|
|
|
| 36 |
test_ratio = st.slider("Train-Test Split Ratio", 0.1, 0.5, 0.2, step=0.05)
|
| 37 |
batch_size = st.slider("Batch Size", 4, 64, 16, step=2)
|
| 38 |
|
| 39 |
+
data_generated = False
|
| 40 |
X_train, X_test, y_train, y_test = None, None, None, None
|
| 41 |
+
|
| 42 |
if st.button("Generate Dataset"):
|
| 43 |
if dataset_type == "Binary Classification":
|
| 44 |
+
X, y = make_classification(n_samples=1000, n_features=2, n_classes=2, random_state=42)
|
| 45 |
elif dataset_type == "XOR":
|
| 46 |
+
X, y = make_moons(n_samples=1000, noise=0.2, random_state=42)
|
| 47 |
elif dataset_type == "Binary Spiral":
|
| 48 |
+
X, y = make_circles(n_samples=1000, noise=0.2, factor=0.5, random_state=42)
|
| 49 |
elif dataset_type == "Regression 1":
|
| 50 |
+
X, y = make_regression(n_samples=1000, n_features=1, noise=5, random_state=42)
|
| 51 |
elif dataset_type == "Regression 2":
|
| 52 |
X = np.linspace(-1, 1, 1000).reshape(-1, 1)
|
| 53 |
y = X ** 3 + 0.1 * np.random.randn(1000, 1).flatten()
|
|
|
|
| 54 |
|
| 55 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_ratio, random_state=42)
|
| 56 |
+
scaler = StandardScaler()
|
| 57 |
+
X_train = scaler.fit_transform(X_train)
|
| 58 |
+
X_test = scaler.transform(X_test)
|
| 59 |
+
data_generated = True
|
| 60 |
+
st.success("Dataset Generated Successfully")
|
| 61 |
+
st.subheader("Dataset Preview")
|
| 62 |
+
st.write("Features:")
|
| 63 |
+
st.write(X[:5])
|
| 64 |
+
st.write("Labels:")
|
| 65 |
+
st.write(y[:5])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Model Training
|
| 68 |
+
if play and data_generated:
|
| 69 |
model = keras.Sequential()
|
| 70 |
+
model.add(layers.InputLayer(input_shape=(X_train.shape[1],)))
|
| 71 |
+
|
| 72 |
+
for i in range(3): # Defaulting to 3 layers
|
| 73 |
+
model.add(layers.Dense(10, activation=activation.lower(),
|
| 74 |
+
kernel_regularizer=keras.regularizers.l1(reg_rate) if reg_type == "L1" else
|
| 75 |
+
(keras.regularizers.l2(reg_rate) if reg_type == "L2" else None)))
|
| 76 |
+
model.add(layers.Dropout(0.2))
|
| 77 |
+
|
| 78 |
model.add(layers.Dense(1, activation="sigmoid" if problem_type == "Classification" else "linear"))
|
| 79 |
+
model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate),
|
| 80 |
+
loss="binary_crossentropy" if problem_type == "Classification" else "mse",
|
| 81 |
+
metrics=["accuracy"] if problem_type == "Classification" else [])
|
| 82 |
+
|
| 83 |
+
history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=0, validation_data=(X_test, y_test))
|
| 84 |
st.success("Model Training Complete")
|
| 85 |
+
|
| 86 |
+
# Plot Training History
|
| 87 |
+
st.subheader("Training Progress")
|
| 88 |
+
fig, ax = plt.subplots(1, 2, figsize=(15, 6))
|
| 89 |
+
ax[0].plot(history.history['loss'], label='Training Loss')
|
| 90 |
+
ax[0].plot(history.history['val_loss'], label='Validation Loss')
|
| 91 |
+
ax[0].set_title("Loss Curve")
|
| 92 |
+
ax[0].set_xlabel("Epochs")
|
| 93 |
+
ax[0].set_ylabel("Loss")
|
| 94 |
+
ax[0].legend()
|
| 95 |
+
|
| 96 |
+
if problem_type == "Classification":
|
| 97 |
+
ax[1].plot(history.history['accuracy'], label='Training Accuracy')
|
| 98 |
+
ax[1].plot(history.history['val_accuracy'], label='Validation Accuracy')
|
| 99 |
+
ax[1].set_title("Accuracy Curve")
|
| 100 |
+
ax[1].set_xlabel("Epochs")
|
| 101 |
+
ax[1].set_ylabel("Accuracy")
|
| 102 |
+
ax[1].legend()
|
| 103 |
+
|
| 104 |
+
st.pyplot(fig)
|