HemanM commited on
Commit
1e1224e
·
verified ·
1 Parent(s): 819cc50

Update evo_architecture.py

Browse files
Files changed (1) hide show
  1. 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
- 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
- }
 
 
 
 
 
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()