File size: 2,014 Bytes
7970f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()