Spaces:
Sleeping
Sleeping
| # 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 | |
| } | |