File size: 1,647 Bytes
2f93104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# evo_architecture.py

import random
import json
import os

GENOME_LOG = "genome_log.csv"

def mutate_genome(base_config):
    """Randomly mutates one architectural parameter from the base config."""
    new_config = base_config.copy()

    mutation_type = random.choice(["layers", "ffn_dim", "num_heads", "memory_enabled"])

    if mutation_type == "layers":
        new_config["num_layers"] = max(2, min(8, new_config["num_layers"] + random.choice([-1, 1])))
    elif mutation_type == "ffn_dim":
        new_config["ffn_dim"] = max(256, min(2048, new_config["ffn_dim"] + random.choice([-256, 256])))
    elif mutation_type == "num_heads":
        new_config["num_heads"] = max(2, min(12, new_config["num_heads"] + random.choice([-2, 2])))
    elif mutation_type == "memory_enabled":
        new_config["memory_enabled"] = not new_config["memory_enabled"]

    return new_config

def log_genome(config, performance=None):
    """Logs the evolved genome and its score."""
    header = ["num_layers", "ffn_dim", "num_heads", "memory_enabled", "score"]
    row = [
        config["num_layers"],
        config["ffn_dim"],
        config["num_heads"],
        config["memory_enabled"],
        performance if performance is not None else ""
    ]

    file_exists = os.path.exists(GENOME_LOG)
    with open(GENOME_LOG, "a", newline='', encoding='utf-8') as f:
        import csv
        writer = csv.writer(f)
        if not file_exists:
            writer.writerow(header)
        writer.writerow(row)

def default_config():
    return {
        "num_layers": 6,
        "ffn_dim": 1024,
        "num_heads": 8,
        "memory_enabled": True
    }