Mpavan45 commited on
Commit
12f46ec
·
verified ·
1 Parent(s): 70de1d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ from tensorflow import keras
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Function to build a simple neural network
8
+ def build_model():
9
+ model = keras.Sequential([
10
+ keras.layers.Dense(32, activation='relu', input_shape=(X_train.shape[1],)),
11
+ keras.layers.Dense(16, activation='relu'),
12
+ keras.layers.Dense(1, activation='sigmoid')
13
+ ])
14
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
15
+ return model
16
+
17
+ # Load a sample dataset
18
+ @st.cache
19
+ def load_data():
20
+ (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
21
+ X_train = X_train.reshape(-1, 28 * 28).astype("float32") / 255
22
+ X_test = X_test.reshape(-1, 28 * 28).astype("float32") / 255
23
+ y_train = (y_train == 1).astype("float32") # Binary classification (1 vs non-1)
24
+ y_test = (y_test == 1).astype("float32")
25
+ return X_train, y_train, X_test, y_test
26
+
27
+ # Streamlit UI
28
+ st.title('TensorFlow Playground with Streamlit')
29
+ st.write("This is a simple neural network app built with TensorFlow and Streamlit.")
30
+
31
+ # Data loading
32
+ X_train, y_train, X_test, y_test = load_data()
33
+
34
+ # User input to modify the network
35
+ hidden_layers = st.slider('Number of hidden layers:', 1, 5, 2)
36
+ neurons_per_layer = st.slider('Number of neurons per layer:', 8, 128, 32)
37
+
38
+ # Build and compile the model
39
+ model = keras.Sequential()
40
+ model.add(keras.layers.Dense(neurons_per_layer, activation='relu', input_shape=(X_train.shape[1],)))
41
+ for _ in range(hidden_layers - 1):
42
+ model.add(keras.layers.Dense(neurons_per_layer, activation='relu'))
43
+ model.add(keras.layers.Dense(1, activation='sigmoid'))
44
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
45
+
46
+ # Training
47
+ st.write('Training model...')
48
+ history = model.fit(X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test))
49
+
50
+ # Model performance
51
+ st.write("Training and validation accuracy:")
52
+ fig, ax = plt.subplots()
53
+ ax.plot(history.history['accuracy'], label='accuracy')
54
+ ax.plot(history.history['val_accuracy'], label = 'val_accuracy')
55
+ ax.set_xlabel('Epoch')
56
+ ax.set_ylabel('Accuracy')
57
+ ax.legend(loc='lower right')
58
+ st.pyplot(fig)
59
+
60
+ # Show final accuracy
61
+ final_accuracy = history.history['accuracy'][-1]
62
+ st.write(f"Final Training Accuracy: {final_accuracy:.2f}")