Spaces:
Sleeping
Sleeping
Update evo_architecture.py
Browse files- evo_architecture.py +50 -28
evo_architecture.py
CHANGED
@@ -3,49 +3,71 @@
|
|
3 |
import random
|
4 |
import json
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
GENOME_LOG = "genome_log.csv"
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
if mutation_type == "
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
new_config["memory_enabled"] = not new_config["memory_enabled"]
|
23 |
|
24 |
-
return
|
25 |
|
26 |
-
def log_genome(config,
|
27 |
-
"""Logs
|
28 |
-
header = ["num_layers", "ffn_dim", "num_heads", "memory_enabled", "score"]
|
29 |
row = [
|
|
|
30 |
config["num_layers"],
|
31 |
config["ffn_dim"],
|
32 |
config["num_heads"],
|
33 |
config["memory_enabled"],
|
34 |
-
|
35 |
]
|
36 |
-
|
37 |
file_exists = os.path.exists(GENOME_LOG)
|
38 |
-
with open(GENOME_LOG, "a", newline=
|
39 |
-
import csv
|
40 |
writer = csv.writer(f)
|
41 |
if not file_exists:
|
42 |
-
writer.writerow(
|
43 |
writer.writerow(row)
|
44 |
|
45 |
-
def
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
3 |
import random
|
4 |
import json
|
5 |
import os
|
6 |
+
import uuid
|
7 |
+
import csv
|
8 |
|
9 |
GENOME_LOG = "genome_log.csv"
|
10 |
+
BEST_GENOME_FILE = "best_genome.json"
|
11 |
|
12 |
+
# Mutation bounds
|
13 |
+
MUTATION_LIMITS = {
|
14 |
+
"num_layers": (2, 12),
|
15 |
+
"ffn_dim": (256, 4096),
|
16 |
+
"num_heads": (2, 16),
|
17 |
+
}
|
18 |
|
19 |
+
def default_config():
|
20 |
+
return {
|
21 |
+
"genome_id": str(uuid.uuid4()),
|
22 |
+
"num_layers": 6,
|
23 |
+
"ffn_dim": 1024,
|
24 |
+
"num_heads": 8,
|
25 |
+
"memory_enabled": True
|
26 |
+
}
|
27 |
+
|
28 |
+
def mutate_genome(base_config, exploration_rate=0.5):
|
29 |
+
"""Mutates the genome. Exploration rate controls how wild the mutation is (0 = minor, 1 = wild)."""
|
30 |
+
config = base_config.copy()
|
31 |
+
config["genome_id"] = str(uuid.uuid4())
|
32 |
+
|
33 |
+
# Select mutation type
|
34 |
+
mutation_type = random.choice(["num_layers", "ffn_dim", "num_heads", "memory_enabled"])
|
35 |
|
36 |
+
if mutation_type == "memory_enabled":
|
37 |
+
config["memory_enabled"] = not config["memory_enabled"]
|
38 |
+
else:
|
39 |
+
min_val, max_val = MUTATION_LIMITS[mutation_type]
|
40 |
+
change = int((max_val - min_val) * exploration_rate)
|
41 |
+
delta = random.randint(-change, change)
|
42 |
+
config[mutation_type] = max(min_val, min(max_val, config[mutation_type] + delta))
|
|
|
43 |
|
44 |
+
return config
|
45 |
|
46 |
+
def log_genome(config, score=None):
|
47 |
+
"""Logs genome config + performance to CSV."""
|
|
|
48 |
row = [
|
49 |
+
config.get("genome_id", ""),
|
50 |
config["num_layers"],
|
51 |
config["ffn_dim"],
|
52 |
config["num_heads"],
|
53 |
config["memory_enabled"],
|
54 |
+
score if score is not None else ""
|
55 |
]
|
|
|
56 |
file_exists = os.path.exists(GENOME_LOG)
|
57 |
+
with open(GENOME_LOG, "a", newline="", encoding="utf-8") as f:
|
|
|
58 |
writer = csv.writer(f)
|
59 |
if not file_exists:
|
60 |
+
writer.writerow(["genome_id", "num_layers", "ffn_dim", "num_heads", "memory_enabled", "score"])
|
61 |
writer.writerow(row)
|
62 |
|
63 |
+
def save_best_genome(config):
|
64 |
+
"""Saves the best-performing genome to file."""
|
65 |
+
with open(BEST_GENOME_FILE, "w", encoding="utf-8") as f:
|
66 |
+
json.dump(config, f)
|
67 |
+
|
68 |
+
def load_best_genome():
|
69 |
+
"""Loads best-performing genome (if exists), else default."""
|
70 |
+
if os.path.exists(BEST_GENOME_FILE):
|
71 |
+
with open(BEST_GENOME_FILE, "r", encoding="utf-8") as f:
|
72 |
+
return json.load(f)
|
73 |
+
return default_config()
|