Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ matplotlib.use('Agg')
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from torchvision import datasets, transforms
|
| 9 |
import gradio as gr
|
| 10 |
-
import json
|
| 11 |
-
import os
|
| 12 |
import io
|
| 13 |
from datetime import datetime
|
| 14 |
from PIL import Image
|
|
@@ -17,7 +15,6 @@ from PIL import Image
|
|
| 17 |
transform = transforms.Compose([transforms.ToTensor()])
|
| 18 |
mnist_dataset = datasets.MNIST(root='./data', train=True, download=False, transform=transform)
|
| 19 |
|
| 20 |
-
HISTORY_FILE = "history.json"
|
| 21 |
|
| 22 |
|
| 23 |
class SKAModel(nn.Module):
|
|
@@ -128,20 +125,7 @@ def get_mnist_subset(samples_per_class, data_seed=0):
|
|
| 128 |
return torch.stack(images_list)
|
| 129 |
|
| 130 |
|
| 131 |
-
def
|
| 132 |
-
if os.path.exists(HISTORY_FILE):
|
| 133 |
-
with open(HISTORY_FILE, "r") as f:
|
| 134 |
-
return json.load(f)
|
| 135 |
-
return []
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
def save_history(history):
|
| 139 |
-
with open(HISTORY_FILE, "w") as f:
|
| 140 |
-
json.dump(history, f)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
def plot_convergence_comparison():
|
| 144 |
-
history = load_history()
|
| 145 |
if not history:
|
| 146 |
fig, ax = plt.subplots()
|
| 147 |
ax.text(0.5, 0.5, "No history yet — run at least one architecture.", ha='center', va='center')
|
|
@@ -215,7 +199,7 @@ def plot_convergence_comparison():
|
|
| 215 |
return Image.open(buf)
|
| 216 |
|
| 217 |
|
| 218 |
-
def run_ska(n1, n2, n3, n4, K, tau, samples_per_class, data_seed):
|
| 219 |
layer_sizes = [int(n1), int(n2), int(n3), int(n4)]
|
| 220 |
neurons_str = ", ".join(str(n) for n in layer_sizes)
|
| 221 |
|
|
@@ -256,8 +240,6 @@ def run_ska(n1, n2, n3, n4, K, tau, samples_per_class, data_seed):
|
|
| 256 |
for l in range(num_layers)
|
| 257 |
]
|
| 258 |
|
| 259 |
-
# Save to history
|
| 260 |
-
history = load_history()
|
| 261 |
run = {
|
| 262 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 263 |
"architecture": neurons_str,
|
|
@@ -268,8 +250,7 @@ def run_ska(n1, n2, n3, n4, K, tau, samples_per_class, data_seed):
|
|
| 268 |
"convergence_state": convergence_state,
|
| 269 |
"entropy_history_norm": entropy_history_norm,
|
| 270 |
}
|
| 271 |
-
history
|
| 272 |
-
save_history(history)
|
| 273 |
|
| 274 |
# Plot 1: normalized entropy trajectory (current run)
|
| 275 |
fig1, axes1 = plt.subplots(num_layers, 1, figsize=(10, 3 * num_layers), sharex=True)
|
|
@@ -284,15 +265,13 @@ def run_ska(n1, n2, n3, n4, K, tau, samples_per_class, data_seed):
|
|
| 284 |
fig1.suptitle(f"Architecture: [{neurons_str}] | K={K} | τ={tau:.2f}", fontsize=12)
|
| 285 |
fig1.tight_layout()
|
| 286 |
|
| 287 |
-
|
| 288 |
-
fig2 = plot_convergence_comparison()
|
| 289 |
|
| 290 |
-
return fig1, fig2
|
| 291 |
|
| 292 |
|
| 293 |
def clear_history():
|
| 294 |
-
|
| 295 |
-
return plot_convergence_comparison()
|
| 296 |
|
| 297 |
|
| 298 |
with gr.Blocks(title="SKA Entropy State Explorer") as demo:
|
|
@@ -336,10 +315,12 @@ with gr.Blocks(title="SKA Entropy State Explorer") as demo:
|
|
| 336 |
plot_current = gr.Plot(label="Current Run: Normalized Entropy Trajectory")
|
| 337 |
plot_comparison = gr.Image(label="4D Entropy State Trajectory")
|
| 338 |
|
|
|
|
|
|
|
| 339 |
run_btn.click(
|
| 340 |
fn=run_ska,
|
| 341 |
-
inputs=[n1_input, n2_input, n3_input, n4_input, k_slider, tau_slider, samples_slider, seed_slider],
|
| 342 |
-
outputs=[plot_current, plot_comparison],
|
| 343 |
)
|
| 344 |
clear_btn.click(
|
| 345 |
fn=clear_history,
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from torchvision import datasets, transforms
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
| 10 |
import io
|
| 11 |
from datetime import datetime
|
| 12 |
from PIL import Image
|
|
|
|
| 15 |
transform = transforms.Compose([transforms.ToTensor()])
|
| 16 |
mnist_dataset = datasets.MNIST(root='./data', train=True, download=False, transform=transform)
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
class SKAModel(nn.Module):
|
|
|
|
| 125 |
return torch.stack(images_list)
|
| 126 |
|
| 127 |
|
| 128 |
+
def plot_convergence_comparison(history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if not history:
|
| 130 |
fig, ax = plt.subplots()
|
| 131 |
ax.text(0.5, 0.5, "No history yet — run at least one architecture.", ha='center', va='center')
|
|
|
|
| 199 |
return Image.open(buf)
|
| 200 |
|
| 201 |
|
| 202 |
+
def run_ska(n1, n2, n3, n4, K, tau, samples_per_class, data_seed, history):
|
| 203 |
layer_sizes = [int(n1), int(n2), int(n3), int(n4)]
|
| 204 |
neurons_str = ", ".join(str(n) for n in layer_sizes)
|
| 205 |
|
|
|
|
| 240 |
for l in range(num_layers)
|
| 241 |
]
|
| 242 |
|
|
|
|
|
|
|
| 243 |
run = {
|
| 244 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 245 |
"architecture": neurons_str,
|
|
|
|
| 250 |
"convergence_state": convergence_state,
|
| 251 |
"entropy_history_norm": entropy_history_norm,
|
| 252 |
}
|
| 253 |
+
history = history + [run]
|
|
|
|
| 254 |
|
| 255 |
# Plot 1: normalized entropy trajectory (current run)
|
| 256 |
fig1, axes1 = plt.subplots(num_layers, 1, figsize=(10, 3 * num_layers), sharex=True)
|
|
|
|
| 265 |
fig1.suptitle(f"Architecture: [{neurons_str}] | K={K} | τ={tau:.2f}", fontsize=12)
|
| 266 |
fig1.tight_layout()
|
| 267 |
|
| 268 |
+
fig2 = plot_convergence_comparison(history)
|
|
|
|
| 269 |
|
| 270 |
+
return fig1, fig2, history
|
| 271 |
|
| 272 |
|
| 273 |
def clear_history():
|
| 274 |
+
return plot_convergence_comparison([])
|
|
|
|
| 275 |
|
| 276 |
|
| 277 |
with gr.Blocks(title="SKA Entropy State Explorer") as demo:
|
|
|
|
| 315 |
plot_current = gr.Plot(label="Current Run: Normalized Entropy Trajectory")
|
| 316 |
plot_comparison = gr.Image(label="4D Entropy State Trajectory")
|
| 317 |
|
| 318 |
+
history_state = gr.State([])
|
| 319 |
+
|
| 320 |
run_btn.click(
|
| 321 |
fn=run_ska,
|
| 322 |
+
inputs=[n1_input, n2_input, n3_input, n4_input, k_slider, tau_slider, samples_slider, seed_slider, history_state],
|
| 323 |
+
outputs=[plot_current, plot_comparison, history_state],
|
| 324 |
)
|
| 325 |
clear_btn.click(
|
| 326 |
fn=clear_history,
|