Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# Include your SNN code here, for brevity I'll only show the Gradio interface part.
|
| 6 |
+
|
| 7 |
+
def run_snn(X, epochs, batch_size, l2_lambda, patience):
|
| 8 |
+
# Your SNN initialization and training code here
|
| 9 |
+
snn = SwarmNeuralNetwork(layer_sizes=[1, 32, 16, 8, 1],
|
| 10 |
+
fractal_methods=[sierpinski_fractal, mandelbrot_fractal, julia_fractal, julia_fractal])
|
| 11 |
+
snn.train(X, y, epochs=epochs, batch_size=batch_size, l2_lambda=l2_lambda, patience=patience)
|
| 12 |
+
y_pred = snn.forward(X, training=False)
|
| 13 |
+
fractal_outputs = snn.apply_fractals(X)
|
| 14 |
+
return y_pred, fractal_outputs
|
| 15 |
+
|
| 16 |
+
def plot_results(X, y, y_pred, fractal_outputs):
|
| 17 |
+
fig, axs = plt.subplots(2, 2, figsize=(15, 10))
|
| 18 |
+
|
| 19 |
+
axs[0, 0].plot(X, y, label='True')
|
| 20 |
+
axs[0, 0].plot(X, y_pred, label='Predicted')
|
| 21 |
+
axs[0, 0].legend()
|
| 22 |
+
axs[0, 0].set_title('True vs Predicted')
|
| 23 |
+
|
| 24 |
+
x, y = fractal_outputs[0]
|
| 25 |
+
axs[0, 1].plot(x, y)
|
| 26 |
+
axs[0, 1].set_title('Sierpinski Fractal Output')
|
| 27 |
+
|
| 28 |
+
axs[1, 0].plot(X, fractal_outputs[1])
|
| 29 |
+
axs[1, 0].set_title('Mandelbrot Fractal Output')
|
| 30 |
+
|
| 31 |
+
axs[1, 1].plot(X, fractal_outputs[2])
|
| 32 |
+
axs[1, 1].set_title('Julia Fractal Output')
|
| 33 |
+
|
| 34 |
+
return fig
|
| 35 |
+
|
| 36 |
+
def main_interface(epochs, batch_size, l2_lambda, patience):
|
| 37 |
+
X = np.linspace(0, 10, 1000).reshape(-1, 1)
|
| 38 |
+
y = np.sin(X).reshape(-1, 1)
|
| 39 |
+
X = (X - X.min()) / (X.max() - X.min())
|
| 40 |
+
y = (y - y.min()) / (y.max() - y.min())
|
| 41 |
+
|
| 42 |
+
y_pred, fractal_outputs = run_snn(X, epochs, batch_size, l2_lambda, patience)
|
| 43 |
+
fig = plot_results(X, y, y_pred, fractal_outputs)
|
| 44 |
+
|
| 45 |
+
return fig
|
| 46 |
+
|
| 47 |
+
gr.Interface(
|
| 48 |
+
fn=main_interface,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.inputs.Slider(1, 10000, default=5000, label="Epochs"),
|
| 51 |
+
gr.inputs.Slider(1, 100, default=32, label="Batch Size"),
|
| 52 |
+
gr.inputs.Slider(0.0001, 0.1, default=0.00001, label="L2 Lambda"),
|
| 53 |
+
gr.inputs.Slider(1, 1000, default=50, label="Patience")
|
| 54 |
+
],
|
| 55 |
+
outputs=gr.outputs.Plot()
|
| 56 |
+
).launch()
|