Spaces:
Sleeping
Sleeping
Update evo_transformer.py
Browse files- evo_transformer.py +55 -24
evo_transformer.py
CHANGED
@@ -1,10 +1,36 @@
|
|
1 |
import random
|
2 |
-
import
|
|
|
|
|
3 |
|
4 |
class EvoTransformer:
|
5 |
def __init__(self):
|
6 |
self.history = []
|
7 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
"layers": 4,
|
9 |
"attention_heads": 4,
|
10 |
"ffn_dim": 1024,
|
@@ -12,26 +38,31 @@ class EvoTransformer:
|
|
12 |
"memory": False
|
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 |
-
|
|
|
|
1 |
import random
|
2 |
+
import json
|
3 |
+
import csv
|
4 |
+
import io
|
5 |
|
6 |
class EvoTransformer:
|
7 |
def __init__(self):
|
8 |
self.history = []
|
9 |
+
self.current = {}
|
10 |
+
|
11 |
+
def reset(self):
|
12 |
+
self.history = []
|
13 |
+
self.current = {}
|
14 |
+
|
15 |
+
def mutate(self, config):
|
16 |
+
config["layers"] = max(1, config["layers"] + random.choice([-1, 1]))
|
17 |
+
config["attention_heads"] = max(1, config["attention_heads"] + random.choice([-1, 1]))
|
18 |
+
config["ffn_dim"] = max(64, config["ffn_dim"] + random.choice([-256, 256]))
|
19 |
+
config["dropout"] = min(0.5, max(0.01, config["dropout"] + random.choice([-0.02, 0.02])))
|
20 |
+
config["memory"] = random.choice([True, False])
|
21 |
+
return config
|
22 |
+
|
23 |
+
def simulate_accuracy(self, config):
|
24 |
+
return round(random.uniform(70, 90), 2)
|
25 |
+
|
26 |
+
def estimate_parameters(self, config):
|
27 |
+
return round(
|
28 |
+
config["layers"] * config["attention_heads"] * config["ffn_dim"] / 1e6,
|
29 |
+
2
|
30 |
+
)
|
31 |
+
|
32 |
+
def evolve(self, generations):
|
33 |
+
base_config = {
|
34 |
"layers": 4,
|
35 |
"attention_heads": 4,
|
36 |
"ffn_dim": 1024,
|
|
|
38 |
"memory": False
|
39 |
}
|
40 |
|
41 |
+
for _ in range(generations):
|
42 |
+
base_config = self.mutate(base_config.copy())
|
43 |
+
acc = self.simulate_accuracy(base_config)
|
44 |
+
params = self.estimate_parameters(base_config)
|
45 |
+
record = {"traits": base_config.copy(), "accuracy": acc, "parameters": params}
|
46 |
+
self.history.append(record)
|
47 |
|
48 |
+
self.current = self.history[-1]
|
49 |
+
|
50 |
+
def get_best_config(self):
|
51 |
+
return self.current
|
52 |
+
|
53 |
+
def export_csv(self):
|
54 |
+
output = io.StringIO()
|
55 |
+
writer = csv.writer(output)
|
56 |
+
writer.writerow(["Generation", "Layers", "Attention Heads", "FFN Dim", "Dropout", "Memory", "Accuracy", "Params"])
|
57 |
+
for i, entry in enumerate(self.history):
|
58 |
+
t = entry["traits"]
|
59 |
+
writer.writerow([
|
60 |
+
i+1,
|
61 |
+
t["layers"], t["attention_heads"], t["ffn_dim"],
|
62 |
+
t["dropout"], t["memory"],
|
63 |
+
entry["accuracy"], entry["parameters"]
|
64 |
+
])
|
65 |
+
return output.getvalue()
|
66 |
+
|
67 |
+
def export_json(self):
|
68 |
+
return json.dumps(self.history, indent=2)
|