HemanM commited on
Commit
2f93104
·
verified ·
1 Parent(s): 1db4c59

Create evo_architecture.py

Browse files
Files changed (1) hide show
  1. evo_architecture.py +51 -0
evo_architecture.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # evo_architecture.py
2
+
3
+ import random
4
+ import json
5
+ import os
6
+
7
+ GENOME_LOG = "genome_log.csv"
8
+
9
+ def mutate_genome(base_config):
10
+ """Randomly mutates one architectural parameter from the base config."""
11
+ new_config = base_config.copy()
12
+
13
+ mutation_type = random.choice(["layers", "ffn_dim", "num_heads", "memory_enabled"])
14
+
15
+ if mutation_type == "layers":
16
+ new_config["num_layers"] = max(2, min(8, new_config["num_layers"] + random.choice([-1, 1])))
17
+ elif mutation_type == "ffn_dim":
18
+ new_config["ffn_dim"] = max(256, min(2048, new_config["ffn_dim"] + random.choice([-256, 256])))
19
+ elif mutation_type == "num_heads":
20
+ new_config["num_heads"] = max(2, min(12, new_config["num_heads"] + random.choice([-2, 2])))
21
+ elif mutation_type == "memory_enabled":
22
+ new_config["memory_enabled"] = not new_config["memory_enabled"]
23
+
24
+ return new_config
25
+
26
+ def log_genome(config, performance=None):
27
+ """Logs the evolved genome and its score."""
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
+ performance if performance is not None else ""
35
+ ]
36
+
37
+ file_exists = os.path.exists(GENOME_LOG)
38
+ with open(GENOME_LOG, "a", newline='', encoding='utf-8') as f:
39
+ import csv
40
+ writer = csv.writer(f)
41
+ if not file_exists:
42
+ writer.writerow(header)
43
+ writer.writerow(row)
44
+
45
+ def default_config():
46
+ return {
47
+ "num_layers": 6,
48
+ "ffn_dim": 1024,
49
+ "num_heads": 8,
50
+ "memory_enabled": True
51
+ }