Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
from sklearn.datasets import make_circles
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
import networkx as nx
|
| 11 |
+
|
| 12 |
+
def generate_data():
|
| 13 |
+
X, y = make_circles(n_samples=500, factor=0.5, noise=0.05, random_state=42)
|
| 14 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)
|
| 15 |
+
scaler = StandardScaler()
|
| 16 |
+
X_train = scaler.fit_transform(X_train)
|
| 17 |
+
X_test = scaler.transform(X_test)
|
| 18 |
+
return X_train, X_test, y_train, y_test
|
| 19 |
+
|
| 20 |
+
def build_model(layers=[4, 2], activation='tanh', learning_rate=0.03):
|
| 21 |
+
model = keras.Sequential()
|
| 22 |
+
model.add(keras.layers.InputLayer(input_shape=(2,)))
|
| 23 |
+
|
| 24 |
+
for units in layers:
|
| 25 |
+
model.add(keras.layers.Dense(units, activation=activation))
|
| 26 |
+
|
| 27 |
+
model.add(keras.layers.Dense(1, activation='sigmoid'))
|
| 28 |
+
|
| 29 |
+
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
|
| 30 |
+
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
|
| 31 |
+
return model
|
| 32 |
+
|
| 33 |
+
def plot_decision_boundary(model, X, y):
|
| 34 |
+
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
| 35 |
+
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
| 36 |
+
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
|
| 37 |
+
grid = np.c_[xx.ravel(), yy.ravel()]
|
| 38 |
+
preds = model.predict(grid).reshape(xx.shape)
|
| 39 |
+
|
| 40 |
+
plt.figure(figsize=(8, 6))
|
| 41 |
+
plt.contourf(xx, yy, preds, alpha=0.3)
|
| 42 |
+
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='coolwarm', edgecolor='k')
|
| 43 |
+
plt.xlabel('X1')
|
| 44 |
+
plt.ylabel('X2')
|
| 45 |
+
plt.title('Decision Boundary')
|
| 46 |
+
st.pyplot(plt)
|
| 47 |
+
|
| 48 |
+
def plot_network(layers):
|
| 49 |
+
G = nx.DiGraph()
|
| 50 |
+
layer_sizes = [2] + layers + [1]
|
| 51 |
+
|
| 52 |
+
pos = {}
|
| 53 |
+
node_idx = 0
|
| 54 |
+
for i, size in enumerate(layer_sizes):
|
| 55 |
+
for j in range(size):
|
| 56 |
+
pos[node_idx] = (i, -j)
|
| 57 |
+
node_idx += 1
|
| 58 |
+
|
| 59 |
+
node_idx = 0
|
| 60 |
+
for i in range(len(layer_sizes) - 1):
|
| 61 |
+
for j in range(layer_sizes[i]):
|
| 62 |
+
for k in range(layer_sizes[i+1]):
|
| 63 |
+
G.add_edge(node_idx + j, node_idx + layer_sizes[i] + k)
|
| 64 |
+
node_idx += layer_sizes[i]
|
| 65 |
+
|
| 66 |
+
plt.figure(figsize=(8, 6))
|
| 67 |
+
nx.draw(G, pos, with_labels=False, node_size=500, edge_color='gray')
|
| 68 |
+
st.pyplot(plt)
|
| 69 |
+
|
| 70 |
+
st.title("TensorFlow Playground Replica with Streamlit")
|
| 71 |
+
|
| 72 |
+
layers = st.sidebar.text_input("Network Shape (comma-separated)", "4,2")
|
| 73 |
+
layers = list(map(int, layers.split(',')))
|
| 74 |
+
activation = st.sidebar.selectbox("Activation Function", ['tanh', 'relu', 'sigmoid'])
|
| 75 |
+
learning_rate = st.sidebar.slider("Learning Rate", 0.001, 0.1, 0.03, step=0.001)
|
| 76 |
+
batch_size = st.sidebar.slider("Batch Size", 5, 50, 10, step=5)
|
| 77 |
+
epochs = st.sidebar.slider("Epochs", 10, 100, 50, step=10)
|
| 78 |
+
|
| 79 |
+
X_train, X_test, y_train, y_test = generate_data()
|
| 80 |
+
|
| 81 |
+
model = build_model(layers, activation, learning_rate)
|
| 82 |
+
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=0)
|
| 83 |
+
|
| 84 |
+
plot_decision_boundary(model, X_test, y_test)
|
| 85 |
+
|
| 86 |
+
st.write("## Model Performance")
|
| 87 |
+
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
|
| 88 |
+
st.write(f"Test Accuracy: {accuracy:.4f}")
|
| 89 |
+
|
| 90 |
+
st.write("## Neural Network Structure")
|
| 91 |
+
plot_network(layers)
|