Spaces:
Sleeping
Sleeping
Update evo_transformer.py
Browse files- evo_transformer.py +48 -0
evo_transformer.py
CHANGED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# evo_transformer.py
|
2 |
+
import random
|
3 |
+
|
4 |
+
class EvoTransformer:
|
5 |
+
def __init__(self, config=None):
|
6 |
+
# Initialize with default or passed config
|
7 |
+
self.config = config or {
|
8 |
+
"layers": 4,
|
9 |
+
"attention_heads": 4,
|
10 |
+
"ffn_dim": 1024,
|
11 |
+
"dropout": 0.1,
|
12 |
+
"memory": False,
|
13 |
+
}
|
14 |
+
self.history = [self.config.copy()]
|
15 |
+
|
16 |
+
def mutate(self):
|
17 |
+
new_config = self.config.copy()
|
18 |
+
trait = random.choice(list(new_config.keys()))
|
19 |
+
|
20 |
+
if trait == "layers":
|
21 |
+
new_config[trait] = max(1, new_config[trait] + random.choice([-1, 1]))
|
22 |
+
elif trait == "attention_heads":
|
23 |
+
new_config[trait] = random.choice([2, 4, 6, 8])
|
24 |
+
elif trait == "ffn_dim":
|
25 |
+
new_config[trait] = random.choice([512, 1024, 2048])
|
26 |
+
elif trait == "dropout":
|
27 |
+
new_config[trait] = round(min(max(0.0, new_config[trait] + random.uniform(-0.05, 0.05)), 0.5), 2)
|
28 |
+
elif trait == "memory":
|
29 |
+
new_config[trait] = not new_config[trait]
|
30 |
+
|
31 |
+
self.config = new_config
|
32 |
+
self.history.append(new_config.copy())
|
33 |
+
|
34 |
+
def evolve(self, generations=3):
|
35 |
+
for _ in range(generations):
|
36 |
+
self.mutate()
|
37 |
+
|
38 |
+
def get_history(self):
|
39 |
+
return self.history
|
40 |
+
|
41 |
+
def evaluate(self):
|
42 |
+
# Simulate an accuracy score for demo purposes
|
43 |
+
score = round(random.uniform(0.85, 0.95), 4)
|
44 |
+
return {"accuracy": score, "params": self.estimate_params()}
|
45 |
+
|
46 |
+
def estimate_params(self):
|
47 |
+
# Simulated parameter count based on config
|
48 |
+
return 10 + self.config["layers"] * self.config["ffn_dim"] * 0.001
|