Anshini commited on
Commit
69105f1
·
verified ·
1 Parent(s): bc69d98

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # UI Inputs
4
+ dataset = st.selectbox("Choose a Dataset", ["moons", "circles", "blobs"])
5
+ learning_rate = st.number_input("Learning Rate", value=0.01, format="%.4f")
6
+ activation = st.selectbox("Activation Function", ["relu", "sigmoid", "tanh"])
7
+ split_ratio = st.slider("Train-Test Split Ratio", 0.5, 0.9, 0.7)
8
+ batch_size = st.number_input("Batch Size", value=32, step=16)
9
+
10
+ if st.button("Train Model"):
11
+ # Call your training and plotting function
12
+ train_and_visualize(dataset, learning_rate, activation, split_ratio, batch_size)
13
+
14
+ from sklearn.datasets import make_moons, make_circles, make_blobs
15
+ from sklearn.model_selection import train_test_split
16
+ from sklearn.preprocessing import StandardScaler
17
+ import tensorflow as tf
18
+ from tensorflow.keras import models, layers
19
+ import matplotlib.pyplot as plt
20
+ import numpy as np
21
+
22
+ def generate_data(dataset, test_size):
23
+ if dataset == "moons":
24
+ X, y = make_moons(n_samples=1000, noise=0.2, random_state=42)
25
+ elif dataset == "circles":
26
+ X, y = make_circles(n_samples=1000, noise=0.2, factor=0.5, random_state=42)
27
+ else:
28
+ X, y = make_blobs(n_samples=1000, centers=2, cluster_std=1.5, random_state=42)
29
+
30
+ X = StandardScaler().fit_transform(X)
31
+ return train_test_split(X, y, test_size=1-test_size, random_state=42)
32
+
33
+ def build_model(activation, learning_rate):
34
+ model = models.Sequential([
35
+ layers.Dense(10, input_shape=(2,), activation=activation),
36
+ layers.Dense(10, activation=activation),
37
+ layers.Dense(1, activation="sigmoid")
38
+ ])
39
+ optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
40
+ model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
41
+ return model
42
+
43
+ def plot_decision_boundary(model, X, y):
44
+ x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
45
+ y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
46
+ xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200),
47
+ np.linspace(y_min, y_max, 200))
48
+ preds = model.predict(np.c_[xx.ravel(), yy.ravel()])
49
+ preds = preds.reshape(xx.shape)
50
+
51
+ plt.contourf(xx, yy, preds, alpha=0.5)
52
+ plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu)
53
+ plt.title("Decision Boundary")
54
+ st.pyplot(plt.gcf())
55
+ plt.clf()
56
+
57
+ def plot_loss(history):
58
+ plt.plot(history.history['loss'], label='Train Loss')
59
+ plt.plot(history.history['val_loss'], label='Test Loss')
60
+ plt.legend()
61
+ plt.title("Training vs Testing Error")
62
+ st.pyplot(plt.gcf())
63
+ plt.clf()
64
+
65
+ def train_and_visualize(dataset, lr, act, split, batch):
66
+ X_train, X_test, y_train, y_test = generate_data(dataset, split)
67
+ model = build_model(act, lr)
68
+ history = model.fit(X_train, y_train, epochs=50, batch_size=batch,
69
+ validation_data=(X_test, y_test), verbose=0)
70
+ plot_decision_boundary(model, np.vstack((X_train, X_test)), np.hstack((y_train, y_test)))
71
+ plot_loss(history)