Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from tensorflow.keras.utils import plot_model
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
-
# Main app title
|
| 10 |
st.title("TensorFlow Neural Network Playground")
|
| 11 |
|
| 12 |
# Sidebar for network configuration
|
|
@@ -29,17 +28,15 @@ def create_model(input_dim, hidden_dim, output_dim, lr):
|
|
| 29 |
|
| 30 |
# Visualize the model architecture using Keras plot_model
|
| 31 |
def plot_network(model):
|
| 32 |
-
# Generate the plot in memory
|
| 33 |
img_data = BytesIO()
|
| 34 |
plot_model(model,
|
| 35 |
to_file=img_data,
|
| 36 |
show_shapes=True,
|
| 37 |
show_layer_names=True,
|
| 38 |
-
rankdir='TB',
|
| 39 |
expand_nested=True,
|
| 40 |
dpi=96)
|
| 41 |
-
|
| 42 |
-
# Display the image in Streamlit
|
| 43 |
st.image(img_data.getvalue(), caption="Neural Network Architecture")
|
| 44 |
|
| 45 |
# Create and display the model structure
|
|
@@ -54,54 +51,36 @@ def generate_sample_data(samples=100):
|
|
| 54 |
y = tf.keras.utils.to_categorical(y, output_nodes)
|
| 55 |
return X, y
|
| 56 |
|
| 57 |
-
# Train model button
|
| 58 |
if st.button("Train Model"):
|
| 59 |
X, y = generate_sample_data()
|
| 60 |
-
|
| 61 |
-
# Training
|
| 62 |
history = model.fit(X, y, epochs=10, verbose=0)
|
| 63 |
|
| 64 |
-
# Display results
|
| 65 |
st.write("Training Complete!")
|
| 66 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
|
| 67 |
-
|
| 68 |
-
# Plot accuracy
|
| 69 |
ax1.plot(history.history['accuracy'])
|
| 70 |
ax1.set_title('Model Accuracy')
|
| 71 |
ax1.set_ylabel('Accuracy')
|
| 72 |
ax1.set_xlabel('Epoch')
|
| 73 |
-
|
| 74 |
-
# Plot loss
|
| 75 |
ax2.plot(history.history['loss'])
|
| 76 |
ax2.set_title('Model Loss')
|
| 77 |
ax2.set_ylabel('Loss')
|
| 78 |
ax2.set_xlabel('Epoch')
|
| 79 |
-
|
| 80 |
st.pyplot(fig)
|
| 81 |
|
| 82 |
-
# Enhanced model summary with styling
|
| 83 |
if st.checkbox("Show Model Summary"):
|
| 84 |
st.subheader("Model Summary")
|
| 85 |
summary_str = []
|
| 86 |
model.summary(print_fn=lambda x: summary_str.append(x))
|
| 87 |
-
|
| 88 |
-
# Format the summary in a more visual way
|
| 89 |
st.markdown("### Model: sequential")
|
| 90 |
st.markdown("""
|
| 91 |
| Layer (type) | Output Shape | Param # |
|
| 92 |
|----------------------|----------------------|---------|""")
|
| 93 |
-
|
| 94 |
-
# Parse and display layers
|
| 95 |
-
for line in summary_str[1:-2]: # Skip header and total params
|
| 96 |
if 'dense' in line.lower():
|
| 97 |
parts = line.split()
|
| 98 |
layer_name = parts[0] + " (Dense)"
|
| 99 |
output_shape = parts[1]
|
| 100 |
param_count = parts[2]
|
| 101 |
st.markdown(f"| {layer_name:<20} | {output_shape:<20} | {param_count:>7} |")
|
| 102 |
-
|
| 103 |
-
# Display total params
|
| 104 |
total_params = summary_str[-1].split()[-1]
|
| 105 |
-
st.markdown(f"**Total params:** {total_params}")
|
| 106 |
-
|
| 107 |
-
# Requirements.txt remains mostly the same, just ensure tensorflow is included
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from io import BytesIO
|
| 8 |
|
|
|
|
| 9 |
st.title("TensorFlow Neural Network Playground")
|
| 10 |
|
| 11 |
# Sidebar for network configuration
|
|
|
|
| 28 |
|
| 29 |
# Visualize the model architecture using Keras plot_model
|
| 30 |
def plot_network(model):
|
|
|
|
| 31 |
img_data = BytesIO()
|
| 32 |
plot_model(model,
|
| 33 |
to_file=img_data,
|
| 34 |
show_shapes=True,
|
| 35 |
show_layer_names=True,
|
| 36 |
+
rankdir='TB',
|
| 37 |
expand_nested=True,
|
| 38 |
dpi=96)
|
| 39 |
+
img_data.seek(0) # Reset buffer position to start
|
|
|
|
| 40 |
st.image(img_data.getvalue(), caption="Neural Network Architecture")
|
| 41 |
|
| 42 |
# Create and display the model structure
|
|
|
|
| 51 |
y = tf.keras.utils.to_categorical(y, output_nodes)
|
| 52 |
return X, y
|
| 53 |
|
|
|
|
| 54 |
if st.button("Train Model"):
|
| 55 |
X, y = generate_sample_data()
|
|
|
|
|
|
|
| 56 |
history = model.fit(X, y, epochs=10, verbose=0)
|
| 57 |
|
|
|
|
| 58 |
st.write("Training Complete!")
|
| 59 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
|
|
|
|
|
|
|
| 60 |
ax1.plot(history.history['accuracy'])
|
| 61 |
ax1.set_title('Model Accuracy')
|
| 62 |
ax1.set_ylabel('Accuracy')
|
| 63 |
ax1.set_xlabel('Epoch')
|
|
|
|
|
|
|
| 64 |
ax2.plot(history.history['loss'])
|
| 65 |
ax2.set_title('Model Loss')
|
| 66 |
ax2.set_ylabel('Loss')
|
| 67 |
ax2.set_xlabel('Epoch')
|
|
|
|
| 68 |
st.pyplot(fig)
|
| 69 |
|
|
|
|
| 70 |
if st.checkbox("Show Model Summary"):
|
| 71 |
st.subheader("Model Summary")
|
| 72 |
summary_str = []
|
| 73 |
model.summary(print_fn=lambda x: summary_str.append(x))
|
|
|
|
|
|
|
| 74 |
st.markdown("### Model: sequential")
|
| 75 |
st.markdown("""
|
| 76 |
| Layer (type) | Output Shape | Param # |
|
| 77 |
|----------------------|----------------------|---------|""")
|
| 78 |
+
for line in summary_str[1:-2]:
|
|
|
|
|
|
|
| 79 |
if 'dense' in line.lower():
|
| 80 |
parts = line.split()
|
| 81 |
layer_name = parts[0] + " (Dense)"
|
| 82 |
output_shape = parts[1]
|
| 83 |
param_count = parts[2]
|
| 84 |
st.markdown(f"| {layer_name:<20} | {output_shape:<20} | {param_count:>7} |")
|
|
|
|
|
|
|
| 85 |
total_params = summary_str[-1].split()[-1]
|
| 86 |
+
st.markdown(f"**Total params:** {total_params}")
|
|
|
|
|
|