TuringsSolutions's picture
Create app.py
7970f31 verified
raw
history blame
2.01 kB
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
# Include your SNN code here, for brevity I'll only show the Gradio interface part.
def run_snn(X, epochs, batch_size, l2_lambda, patience):
# Your SNN initialization and training code here
snn = SwarmNeuralNetwork(layer_sizes=[1, 32, 16, 8, 1],
fractal_methods=[sierpinski_fractal, mandelbrot_fractal, julia_fractal, julia_fractal])
snn.train(X, y, epochs=epochs, batch_size=batch_size, l2_lambda=l2_lambda, patience=patience)
y_pred = snn.forward(X, training=False)
fractal_outputs = snn.apply_fractals(X)
return y_pred, fractal_outputs
def plot_results(X, y, y_pred, fractal_outputs):
fig, axs = plt.subplots(2, 2, figsize=(15, 10))
axs[0, 0].plot(X, y, label='True')
axs[0, 0].plot(X, y_pred, label='Predicted')
axs[0, 0].legend()
axs[0, 0].set_title('True vs Predicted')
x, y = fractal_outputs[0]
axs[0, 1].plot(x, y)
axs[0, 1].set_title('Sierpinski Fractal Output')
axs[1, 0].plot(X, fractal_outputs[1])
axs[1, 0].set_title('Mandelbrot Fractal Output')
axs[1, 1].plot(X, fractal_outputs[2])
axs[1, 1].set_title('Julia Fractal Output')
return fig
def main_interface(epochs, batch_size, l2_lambda, patience):
X = np.linspace(0, 10, 1000).reshape(-1, 1)
y = np.sin(X).reshape(-1, 1)
X = (X - X.min()) / (X.max() - X.min())
y = (y - y.min()) / (y.max() - y.min())
y_pred, fractal_outputs = run_snn(X, epochs, batch_size, l2_lambda, patience)
fig = plot_results(X, y, y_pred, fractal_outputs)
return fig
gr.Interface(
fn=main_interface,
inputs=[
gr.inputs.Slider(1, 10000, default=5000, label="Epochs"),
gr.inputs.Slider(1, 100, default=32, label="Batch Size"),
gr.inputs.Slider(0.0001, 0.1, default=0.00001, label="L2 Lambda"),
gr.inputs.Slider(1, 1000, default=50, label="Patience")
],
outputs=gr.outputs.Plot()
).launch()