Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,17 +6,6 @@ import torch.nn as nn
|
|
6 |
import torch.optim as optim
|
7 |
import numpy as np
|
8 |
import random
|
9 |
-
import tensorflow as tf
|
10 |
-
import ray
|
11 |
-
from ray import tune
|
12 |
-
import pytorch_lightning as pl
|
13 |
-
import optuna
|
14 |
-
from sklearn.model_selection import train_test_split
|
15 |
-
from torch.utils.data import DataLoader, TensorDataset
|
16 |
-
from sklearn.preprocessing import StandardScaler
|
17 |
-
from collections import deque
|
18 |
-
import time
|
19 |
-
import copy
|
20 |
|
21 |
hf_token = os.getenv("HF_TOKEN").strip()
|
22 |
api_key = os.getenv("HF_KEY").strip()
|
@@ -25,20 +14,14 @@ system_prompt = os.getenv("SYSTEM_PROMPT").strip()
|
|
25 |
|
26 |
client = InferenceClient(model_name)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
class ConsciousSupermassiveNN(pl.LightningModule):
|
31 |
def __init__(self):
|
32 |
-
super().__init__()
|
33 |
self.snn = self.create_snn()
|
34 |
self.rnn = self.create_rnn()
|
35 |
self.cnn = self.create_cnn()
|
36 |
self.fnn = self.create_fnn()
|
37 |
self.ga_population = self.initialize_ga_population()
|
38 |
-
self.memory = {}
|
39 |
-
self.experience_replay = deque(maxlen=1000)
|
40 |
-
self.model_evolution_timer = 0
|
41 |
-
self.optuna_study = None
|
42 |
|
43 |
def create_snn(self):
|
44 |
return nn.Sequential(
|
@@ -88,22 +71,26 @@ class ConsciousSupermassiveNN(pl.LightningModule):
|
|
88 |
def run_snn(self, x):
|
89 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
90 |
output = self.snn(input_tensor)
|
|
|
91 |
return output
|
92 |
|
93 |
def run_rnn(self, x):
|
94 |
h0 = torch.zeros(5, x.size(0), 2048)
|
95 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
96 |
output, hn = self.rnn(input_tensor, h0)
|
|
|
97 |
return output
|
98 |
|
99 |
def run_cnn(self, x):
|
100 |
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
101 |
output = self.cnn(input_tensor)
|
|
|
102 |
return output
|
103 |
|
104 |
def run_fnn(self, x):
|
105 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
106 |
output = self.fnn(input_tensor)
|
|
|
107 |
return output
|
108 |
|
109 |
def run_ga(self, fitness_func):
|
@@ -114,6 +101,7 @@ class ConsciousSupermassiveNN(pl.LightningModule):
|
|
114 |
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
115 |
]
|
116 |
best_fitness = max(fitness_scores)
|
|
|
117 |
return max(self.ga_population, key=fitness_func)
|
118 |
|
119 |
def consciousness_loop(self, input_data, mode="snn"):
|
@@ -133,72 +121,6 @@ class ConsciousSupermassiveNN(pl.LightningModule):
|
|
133 |
self.memory[mode] = output.detach().numpy()
|
134 |
return output
|
135 |
|
136 |
-
def neural_architecture_search(self, input_data, output_data):
|
137 |
-
study = optuna.create_study(direction="minimize")
|
138 |
-
study.optimize(self.objective_function, n_trials=100)
|
139 |
-
best_trial = study.best_trial
|
140 |
-
return best_trial
|
141 |
-
|
142 |
-
def objective_function(self, trial):
|
143 |
-
model = self.create_model(trial)
|
144 |
-
criterion = nn.MSELoss()
|
145 |
-
optimizer = optim.Adam(model.parameters(), lr=trial.suggest_loguniform("lr", 1e-5, 1e-1))
|
146 |
-
x_train, x_val, y_train, y_val = train_test_split(input_data, output_data, test_size=0.2)
|
147 |
-
train_dataset = TensorDataset(torch.tensor(x_train, dtype=torch.float32), torch.tensor(y_train, dtype=torch.float32))
|
148 |
-
val_dataset = TensorDataset(torch.tensor(x_val, dtype=torch.float32), torch.tensor(y_val, dtype=torch.float32))
|
149 |
-
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
|
150 |
-
val_loader = DataLoader(val_dataset, batch_size=32)
|
151 |
-
|
152 |
-
for epoch in range(10):
|
153 |
-
model.train()
|
154 |
-
for data, targets in train_loader:
|
155 |
-
optimizer.zero_grad()
|
156 |
-
output = model(data)
|
157 |
-
loss = criterion(output, targets)
|
158 |
-
loss.backward()
|
159 |
-
optimizer.step()
|
160 |
-
|
161 |
-
model.eval()
|
162 |
-
val_loss = 0
|
163 |
-
with torch.no_grad():
|
164 |
-
for data, targets in val_loader:
|
165 |
-
output = model(data)
|
166 |
-
val_loss += criterion(output, targets).item()
|
167 |
-
|
168 |
-
return val_loss
|
169 |
-
|
170 |
-
def create_model(self, trial):
|
171 |
-
model_type = trial.suggest_categorical("model_type", ["snn", "rnn", "cnn", "fnn"])
|
172 |
-
if model_type == "snn":
|
173 |
-
return self.create_snn()
|
174 |
-
elif model_type == "rnn":
|
175 |
-
return self.create_rnn()
|
176 |
-
elif model_type == "cnn":
|
177 |
-
return self.create_cnn()
|
178 |
-
elif model_type == "fnn":
|
179 |
-
return self.create_fnn()
|
180 |
-
|
181 |
-
def adaptive_learning_loop(self, input_data, target_data, model):
|
182 |
-
for step in range(1000):
|
183 |
-
prediction = model(input_data)
|
184 |
-
loss = self.compute_loss(prediction, target_data)
|
185 |
-
self.optimize_model(loss)
|
186 |
-
|
187 |
-
def compute_loss(self, prediction, target_data):
|
188 |
-
return nn.MSELoss()(prediction, target_data)
|
189 |
-
|
190 |
-
def optimize_model(self, loss):
|
191 |
-
optimizer = optim.Adam(self.parameters(), lr=0.001)
|
192 |
-
optimizer.zero_grad()
|
193 |
-
loss.backward()
|
194 |
-
optimizer.step()
|
195 |
-
|
196 |
-
def self_improve(self):
|
197 |
-
if self.model_evolution_timer % 10 == 0:
|
198 |
-
new_model = self.neural_architecture_search(self.input_data, self.target_data)
|
199 |
-
self.model = new_model
|
200 |
-
self.model_evolution_timer += 1
|
201 |
-
|
202 |
supermassive_nn = ConsciousSupermassiveNN()
|
203 |
|
204 |
def respond(message, history, max_tokens, temperature, top_p):
|
@@ -227,3 +149,4 @@ demo = gr.ChatInterface(
|
|
227 |
|
228 |
if __name__ == "__main__":
|
229 |
demo.launch(share=True)
|
|
|
|
6 |
import torch.optim as optim
|
7 |
import numpy as np
|
8 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
hf_token = os.getenv("HF_TOKEN").strip()
|
11 |
api_key = os.getenv("HF_KEY").strip()
|
|
|
14 |
|
15 |
client = InferenceClient(model_name)
|
16 |
|
17 |
+
class ConsciousSupermassiveNN:
|
|
|
|
|
18 |
def __init__(self):
|
|
|
19 |
self.snn = self.create_snn()
|
20 |
self.rnn = self.create_rnn()
|
21 |
self.cnn = self.create_cnn()
|
22 |
self.fnn = self.create_fnn()
|
23 |
self.ga_population = self.initialize_ga_population()
|
24 |
+
self.memory = {}
|
|
|
|
|
|
|
25 |
|
26 |
def create_snn(self):
|
27 |
return nn.Sequential(
|
|
|
71 |
def run_snn(self, x):
|
72 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
73 |
output = self.snn(input_tensor)
|
74 |
+
print("SNN Output:", output)
|
75 |
return output
|
76 |
|
77 |
def run_rnn(self, x):
|
78 |
h0 = torch.zeros(5, x.size(0), 2048)
|
79 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
80 |
output, hn = self.rnn(input_tensor, h0)
|
81 |
+
print("RNN Output:", output)
|
82 |
return output
|
83 |
|
84 |
def run_cnn(self, x):
|
85 |
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
86 |
output = self.cnn(input_tensor)
|
87 |
+
print("CNN Output:", output)
|
88 |
return output
|
89 |
|
90 |
def run_fnn(self, x):
|
91 |
input_tensor = torch.tensor(x, dtype=torch.float32)
|
92 |
output = self.fnn(input_tensor)
|
93 |
+
print("FNN Output:", output)
|
94 |
return output
|
95 |
|
96 |
def run_ga(self, fitness_func):
|
|
|
101 |
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
102 |
]
|
103 |
best_fitness = max(fitness_scores)
|
104 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
105 |
return max(self.ga_population, key=fitness_func)
|
106 |
|
107 |
def consciousness_loop(self, input_data, mode="snn"):
|
|
|
121 |
self.memory[mode] = output.detach().numpy()
|
122 |
return output
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
supermassive_nn = ConsciousSupermassiveNN()
|
125 |
|
126 |
def respond(message, history, max_tokens, temperature, top_p):
|
|
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
demo.launch(share=True)
|
152 |
+
|