Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
from sklearn.datasets import make_circles | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.layers import Dense | |
from tensorflow.keras.optimizers import Adam | |
import matplotlib.pyplot as plt | |
# Set Streamlit page config | |
st.set_page_config(page_title="ML Playground", layout="centered") | |
st.title("π§ TensorFlow Playground Clone") | |
st.markdown("Train a simple neural network on a synthetic dataset like circles") | |
# Sidebar controls | |
st.sidebar.header("Model Settings") | |
# Dataset | |
dataset = st.sidebar.selectbox("Dataset", ["Circle"]) | |
n_samples = st.sidebar.slider("Number of Samples", 100, 1000, 300) | |
# Model settings | |
layers = st.sidebar.text_input("Network Shape (comma-separated)", "4,2") | |
activation = st.sidebar.selectbox("Activation", ["relu", "tanh", "sigmoid"]) | |
learning_rate = st.sidebar.slider("Learning Rate", 0.001, 0.1, 0.03, step=0.001) | |
epochs = st.sidebar.slider("Epochs", 10, 200, 50) | |
# Generate dataset | |
X, y = make_circles(n_samples=n_samples, factor=0.5, noise=0.05, random_state=0) | |
X = StandardScaler().fit_transform(X) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42) | |
# Build model | |
model = Sequential() | |
layer_sizes = [int(n.strip()) for n in layers.split(",") if n.strip().isdigit()] | |
input_dim = X.shape[1] | |
# Input + hidden layers | |
model.add(Dense(layer_sizes[0], input_dim=input_dim, activation=activation)) | |
for size in layer_sizes[1:]: | |
model.add(Dense(size, activation=activation)) | |
# Output layer | |
model.add(Dense(1, activation='sigmoid')) | |
optimizer = Adam(learning_rate=learning_rate) | |
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) | |
# Training | |
with st.spinner("Training model..."): | |
history = model.fit(X_train, y_train, epochs=epochs, verbose=0, validation_data=(X_test, y_test)) | |
# Plotting | |
fig, ax = plt.subplots() | |
ax.plot(history.history['accuracy'], label='Train Accuracy') | |
ax.plot(history.history['val_accuracy'], label='Val Accuracy') | |
ax.set_title("Training Progress") | |
ax.set_xlabel("Epoch") | |
ax.set_ylabel("Accuracy") | |
ax.legend() | |
st.pyplot(fig) | |
# Final Accuracy | |
train_acc = model.evaluate(X_train, y_train, verbose=0)[1] | |
test_acc = model.evaluate(X_test, y_test, verbose=0)[1] | |
st.success(f"β Final Training Accuracy: {train_acc:.2f}") | |
st.success(f"β Final Testing Accuracy: {test_acc:.2f}") | |