File size: 2,240 Bytes
9dce9f3
304a057
 
 
9dce9f3
 
d6397a3
 
304a057
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dce9f3
 
 
 
d6397a3
9dce9f3
a41d201
304a057
 
 
 
 
 
d6397a3
304a057
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import random
import json
import csv
import io

class EvoTransformer:
    def __init__(self):
        self.history = []
        self.current = {}

    def reset(self):
        self.history = []
        self.current = {}

    def mutate(self, config):
        config["layers"] = max(1, config["layers"] + random.choice([-1, 1]))
        config["attention_heads"] = max(1, config["attention_heads"] + random.choice([-1, 1]))
        config["ffn_dim"] = max(64, config["ffn_dim"] + random.choice([-256, 256]))
        config["dropout"] = min(0.5, max(0.01, config["dropout"] + random.choice([-0.02, 0.02])))
        config["memory"] = random.choice([True, False])
        return config

    def simulate_accuracy(self, config):
        return round(random.uniform(70, 90), 2)

    def estimate_parameters(self, config):
        return round(
            config["layers"] * config["attention_heads"] * config["ffn_dim"] / 1e6,
            2
        )

    def evolve(self, generations):
        base_config = {
            "layers": 4,
            "attention_heads": 4,
            "ffn_dim": 1024,
            "dropout": 0.1,
            "memory": False
        }

        for _ in range(generations):
            base_config = self.mutate(base_config.copy())
            acc = self.simulate_accuracy(base_config)
            params = self.estimate_parameters(base_config)
            record = {"traits": base_config.copy(), "accuracy": acc, "parameters": params}
            self.history.append(record)

        self.current = self.history[-1]

    def get_best_config(self):
        return self.current

    def export_csv(self):
        output = io.StringIO()
        writer = csv.writer(output)
        writer.writerow(["Generation", "Layers", "Attention Heads", "FFN Dim", "Dropout", "Memory", "Accuracy", "Params"])
        for i, entry in enumerate(self.history):
            t = entry["traits"]
            writer.writerow([
                i+1,
                t["layers"], t["attention_heads"], t["ffn_dim"],
                t["dropout"], t["memory"],
                entry["accuracy"], entry["parameters"]
            ])
        return output.getvalue()

    def export_json(self):
        return json.dumps(self.history, indent=2)