Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import logging
|
4 |
+
from tqdm import tqdm
|
5 |
+
|
6 |
+
class SwarmAgent:
|
7 |
+
def __init__(self, position, velocity):
|
8 |
+
self.position = position
|
9 |
+
self.velocity = velocity
|
10 |
+
self.m = np.zeros_like(position)
|
11 |
+
self.v = np.zeros_like(position)
|
12 |
+
|
13 |
+
class SwarmNeuralNetwork:
|
14 |
+
def __init__(self, num_agents, param_shape, target_response):
|
15 |
+
self.param_shape = param_shape
|
16 |
+
self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
|
17 |
+
self.target_response = target_response
|
18 |
+
self.current_epoch = 0
|
19 |
+
self.noise_schedule = np.linspace(0.1, 0.002, 1000) # Noise schedule
|
20 |
+
|
21 |
+
def random_position(self):
|
22 |
+
return np.random.randn(*self.param_shape) # Use Gaussian noise
|
23 |
+
|
24 |
+
def random_velocity(self):
|
25 |
+
return np.random.randn(*self.param_shape) * 0.01
|
26 |
+
|
27 |
+
def call_api(self, params):
|
28 |
+
# Placeholder for API call logic
|
29 |
+
# Simulate API call response based on parameters
|
30 |
+
# In real scenario, this would involve an actual API call
|
31 |
+
response = np.sum(params) # Simulate response as sum of parameters
|
32 |
+
return response
|
33 |
+
|
34 |
+
def update_agents(self, timestep):
|
35 |
+
noise_level = self.noise_schedule[min(timestep, len(self.noise_schedule) - 1)]
|
36 |
+
|
37 |
+
for agent in self.agents:
|
38 |
+
# Predict noise
|
39 |
+
predicted_noise = agent.position - self.target_response
|
40 |
+
|
41 |
+
# Denoise
|
42 |
+
denoised = (agent.position - noise_level * predicted_noise) / (1 - noise_level)
|
43 |
+
|
44 |
+
# Add scaled noise for next step
|
45 |
+
agent.position = denoised + np.random.randn(*self.param_shape) * np.sqrt(noise_level)
|
46 |
+
|
47 |
+
# Clip values
|
48 |
+
agent.position = np.clip(agent.position, -1, 1)
|
49 |
+
|
50 |
+
def train(self, epochs):
|
51 |
+
logging.basicConfig(filename='training.log', level=logging.INFO)
|
52 |
+
|
53 |
+
for epoch in tqdm(range(epochs), desc="Training Epochs"):
|
54 |
+
self.update_agents(epoch)
|
55 |
+
|
56 |
+
# Evaluate performance
|
57 |
+
responses = [self.call_api(agent.position) for agent in self.agents]
|
58 |
+
mse = np.mean((np.array(responses) - self.target_response)**2)
|
59 |
+
logging.info(f"Epoch {epoch}, MSE: {mse}")
|
60 |
+
|
61 |
+
if epoch % 10 == 0:
|
62 |
+
print(f"Epoch {epoch}, MSE: {mse}")
|
63 |
+
self.current_epoch += 1
|
64 |
+
|
65 |
+
def save_model(self, filename):
|
66 |
+
model_state = {
|
67 |
+
'agents': self.agents,
|
68 |
+
'current_epoch': self.current_epoch
|
69 |
+
}
|
70 |
+
np.save(filename, model_state)
|
71 |
+
|
72 |
+
def load_model(self, filename):
|
73 |
+
model_state = np.load(filename, allow_pickle=True).item()
|
74 |
+
self.agents = model_state['agents']
|
75 |
+
self.current_epoch = model_state['current_epoch']
|
76 |
+
|
77 |
+
def generate_new_parameters(self, num_steps=1000):
|
78 |
+
for agent in self.agents:
|
79 |
+
agent.position = np.random.randn(*self.param_shape)
|
80 |
+
|
81 |
+
for step in tqdm(range(num_steps), desc="Generating Parameters"):
|
82 |
+
self.update_agents(num_steps - step - 1) # Reverse order
|
83 |
+
|
84 |
+
best_agent = min(self.agents, key=lambda agent: np.mean((self.call_api(agent.position) - self.target_response)**2))
|
85 |
+
return best_agent.position
|
86 |
+
|
87 |
+
# Gradio Interface
|
88 |
+
def train_snn(target_response, num_agents, epochs):
|
89 |
+
param_shape = (10,) # Example parameter shape
|
90 |
+
snn = SwarmNeuralNetwork(num_agents=num_agents, param_shape=param_shape, target_response=target_response)
|
91 |
+
snn.train(epochs=epochs)
|
92 |
+
snn.save_model('snn_model.npy')
|
93 |
+
return snn.generate_new_parameters()
|
94 |
+
|
95 |
+
def generate_new_parameters():
|
96 |
+
param_shape = (10,) # Example parameter shape
|
97 |
+
snn = SwarmNeuralNetwork(num_agents=2000, param_shape=param_shape, target_response=None)
|
98 |
+
snn.load_model('snn_model.npy')
|
99 |
+
new_params = snn.generate_new_parameters()
|
100 |
+
return new_params
|
101 |
+
|
102 |
+
interface = gr.Interface(
|
103 |
+
fn=train_snn,
|
104 |
+
inputs=[
|
105 |
+
gr.Number(label="Target Response"),
|
106 |
+
gr.Slider(minimum=500, maximum=3000, value=2000, label="Number of Agents"),
|
107 |
+
gr.Slider(minimum=10, maximum=200, value=100, label="Number of Epochs")
|
108 |
+
],
|
109 |
+
outputs=gr.Textbox(label="Generated Parameters"),
|
110 |
+
title="Swarm Neural Network API Parameter Optimization",
|
111 |
+
description="Set the target response and the number of agents and epochs to train the Swarm Neural Network to generate optimized API parameters."
|
112 |
+
)
|
113 |
+
|
114 |
+
interface.launch()
|