Update app.py
Browse filesLanguage change to English
app.py
CHANGED
|
@@ -3,87 +3,100 @@ import matplotlib.pyplot as plt
|
|
| 3 |
import networkx as nx
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
input_size = 3
|
| 8 |
hidden_size = 4
|
| 9 |
output_size = 2
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
G = nx.DiGraph()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
def update_graph(input_size, hidden_size, output_size):
|
| 16 |
-
#
|
| 17 |
input_size = int(input_size)
|
| 18 |
hidden_size = int(hidden_size)
|
| 19 |
output_size = int(output_size)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
G.clear()
|
| 23 |
|
| 24 |
-
#
|
| 25 |
for i in range(input_size):
|
| 26 |
G.add_node(f'I{i}', layer='input')
|
| 27 |
|
| 28 |
-
#
|
| 29 |
for i in range(hidden_size):
|
| 30 |
G.add_node(f'H{i}', layer='hidden')
|
| 31 |
|
| 32 |
-
#
|
| 33 |
for i in range(output_size):
|
| 34 |
G.add_node(f'O{i}', layer='output')
|
| 35 |
|
| 36 |
-
#
|
| 37 |
for i in range(input_size):
|
| 38 |
for j in range(hidden_size):
|
| 39 |
G.add_edge(f'I{i}', f'H{j}', weight=np.random.rand())
|
| 40 |
|
| 41 |
-
#
|
| 42 |
for j in range(hidden_size):
|
| 43 |
for k in range(output_size):
|
| 44 |
G.add_edge(f'H{j}', f'O{k}', weight=np.random.rand())
|
| 45 |
|
| 46 |
-
#
|
| 47 |
pos = {}
|
| 48 |
|
| 49 |
-
#
|
| 50 |
for i in range(input_size):
|
| 51 |
-
pos[f'I{i}'] = (0, 1 - (i / (input_size - 1))) #
|
| 52 |
|
| 53 |
-
#
|
| 54 |
for i in range(hidden_size):
|
| 55 |
-
pos[f'H{i}'] = (1, 1 - (i / (hidden_size - 1))) #
|
| 56 |
|
| 57 |
-
#
|
| 58 |
for i in range(output_size):
|
| 59 |
-
pos[f'O{i}'] = (2, 1 - (i / (output_size - 1))) #
|
| 60 |
|
| 61 |
-
#
|
| 62 |
edges = G.edges(data=True)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
plt.figure(figsize=(10, 6))
|
| 66 |
-
nx.draw(G, pos, with_labels=True, node_size=2000, node_color=
|
| 67 |
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): f'{d["weight"]:.2f}' for u, v, d in edges})
|
| 68 |
plt.title("Visual MLP", fontsize=16)
|
| 69 |
-
plt.axis('off') #
|
| 70 |
-
plt.tight_layout() #
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
-
gr.Markdown("### MLP
|
| 77 |
-
|
| 78 |
-
input_slider = gr.Slider(minimum=1, maximum=10, value=input_size, label="Input Layer")
|
| 79 |
-
hidden_slider = gr.Slider(minimum=1, maximum=10, value=hidden_size, label="Hidden Layer")
|
| 80 |
-
output_slider = gr.Slider(minimum=1, maximum=10, value=output_size, label="Output Layer")
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
|
|
|
| 84 |
update_button = gr.Button("Update")
|
| 85 |
|
| 86 |
-
update_button.click(fn=update_graph,
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
demo.launch()
|
|
|
|
| 3 |
import networkx as nx
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Initial neuron numbers and colors
|
| 7 |
input_size = 3
|
| 8 |
hidden_size = 4
|
| 9 |
output_size = 2
|
| 10 |
+
input_color = "skyblue"
|
| 11 |
+
hidden_color = "lightgreen"
|
| 12 |
+
output_color = "salmon"
|
| 13 |
|
| 14 |
+
# Create an empty directed graph for the visualization
|
| 15 |
G = nx.DiGraph()
|
| 16 |
|
| 17 |
+
# Update neurons and create the graph
|
| 18 |
+
def update_graph(input_size, hidden_size, output_size, input_color, hidden_color, output_color):
|
| 19 |
+
# Convert to integer type
|
| 20 |
input_size = int(input_size)
|
| 21 |
hidden_size = int(hidden_size)
|
| 22 |
output_size = int(output_size)
|
| 23 |
|
| 24 |
+
# Clear the graph
|
| 25 |
G.clear()
|
| 26 |
|
| 27 |
+
# Input layer neurons
|
| 28 |
for i in range(input_size):
|
| 29 |
G.add_node(f'I{i}', layer='input')
|
| 30 |
|
| 31 |
+
# Hidden layer neurons
|
| 32 |
for i in range(hidden_size):
|
| 33 |
G.add_node(f'H{i}', layer='hidden')
|
| 34 |
|
| 35 |
+
# Output layer neurons
|
| 36 |
for i in range(output_size):
|
| 37 |
G.add_node(f'O{i}', layer='output')
|
| 38 |
|
| 39 |
+
# Connections from input layer to hidden layer
|
| 40 |
for i in range(input_size):
|
| 41 |
for j in range(hidden_size):
|
| 42 |
G.add_edge(f'I{i}', f'H{j}', weight=np.random.rand())
|
| 43 |
|
| 44 |
+
# Connections from hidden layer to output layer
|
| 45 |
for j in range(hidden_size):
|
| 46 |
for k in range(output_size):
|
| 47 |
G.add_edge(f'H{j}', f'O{k}', weight=np.random.rand())
|
| 48 |
|
| 49 |
+
# Calculate neuron positions
|
| 50 |
pos = {}
|
| 51 |
|
| 52 |
+
# Input layer positions
|
| 53 |
for i in range(input_size):
|
| 54 |
+
pos[f'I{i}'] = (0, 1 - (i / (input_size - 1))) # Vertically aligned
|
| 55 |
|
| 56 |
+
# Hidden layer positions
|
| 57 |
for i in range(hidden_size):
|
| 58 |
+
pos[f'H{i}'] = (1, 1 - (i / (hidden_size - 1))) # Vertically aligned
|
| 59 |
|
| 60 |
+
# Output layer positions
|
| 61 |
for i in range(output_size):
|
| 62 |
+
pos[f'O{i}'] = (2, 1 - (i / (output_size - 1))) # Vertically aligned
|
| 63 |
|
| 64 |
+
# Visualize edge weights
|
| 65 |
edges = G.edges(data=True)
|
| 66 |
|
| 67 |
+
# Visualize the graph
|
| 68 |
plt.figure(figsize=(10, 6))
|
| 69 |
+
nx.draw(G, pos, with_labels=True, node_size=2000, node_color=[input_color] * input_size + [hidden_color] * hidden_size + [output_color] * output_size, font_size=12, font_weight='bold', arrows=True)
|
| 70 |
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): f'{d["weight"]:.2f}' for u, v, d in edges})
|
| 71 |
plt.title("Visual MLP", fontsize=16)
|
| 72 |
+
plt.axis('off') # Turn off axes
|
| 73 |
+
plt.tight_layout() # Adjust layout
|
| 74 |
|
| 75 |
+
# Display the graph
|
| 76 |
+
buf = plt.gcf() # Get the current figure
|
| 77 |
+
plt.close() # Close the plot
|
| 78 |
+
|
| 79 |
+
return buf # Return the graph
|
| 80 |
|
| 81 |
+
# Define the Gradio interface
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("### MLP Model Visualizer")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
input_slider = gr.Slider(minimum=2, maximum=10, value=input_size, label="Input Layer")
|
| 86 |
+
hidden_slider = gr.Slider(minimum=2, maximum=10, value=hidden_size, label="Hidden Layer")
|
| 87 |
+
output_slider = gr.Slider(minimum=2, maximum=10, value=output_size, label="Output Layer")
|
| 88 |
+
|
| 89 |
+
input_color_picker = gr.ColorPicker(value=input_color, label="Input Layer Colour")
|
| 90 |
+
hidden_color_picker = gr.ColorPicker(value=hidden_color, label="Hidden Layer Colour")
|
| 91 |
+
output_color_picker = gr.ColorPicker(value=output_color, label="Output Colour")
|
| 92 |
|
| 93 |
+
output_plot = gr.Plot(label="MLP Model Graph")
|
| 94 |
+
|
| 95 |
update_button = gr.Button("Update")
|
| 96 |
|
| 97 |
+
update_button.click(fn=update_graph,
|
| 98 |
+
inputs=[input_slider, hidden_slider, output_slider, input_color_picker, hidden_color_picker, output_color_picker],
|
| 99 |
+
outputs=output_plot)
|
| 100 |
|
| 101 |
+
# Run the application
|
| 102 |
demo.launch()
|