HemanM commited on
Commit
304a057
·
verified ·
1 Parent(s): a38eb6f

Update evo_transformer.py

Browse files
Files changed (1) hide show
  1. evo_transformer.py +55 -24
evo_transformer.py CHANGED
@@ -1,10 +1,36 @@
1
  import random
2
- import copy
 
 
3
 
4
  class EvoTransformer:
5
  def __init__(self):
6
  self.history = []
7
- self.base_config = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  "layers": 4,
9
  "attention_heads": 4,
10
  "ffn_dim": 1024,
@@ -12,26 +38,31 @@ class EvoTransformer:
12
  "memory": False
13
  }
14
 
15
- def reset(self):
16
- self.history = []
 
 
 
 
17
 
18
- def mutate(self, config):
19
- new_config = copy.deepcopy(config)
20
- if random.random() < 0.5:
21
- new_config["layers"] = min(12, max(1, new_config["layers"] + random.choice([-1, 1])))
22
- if random.random() < 0.5:
23
- new_config["attention_heads"] = min(12, max(1, new_config["attention_heads"] + random.choice([-1, 1])))
24
- if random.random() < 0.5:
25
- new_config["ffn_dim"] = min(4096, max(128, new_config["ffn_dim"] + random.choice([-512, 512])))
26
- if random.random() < 0.5:
27
- new_config["dropout"] = round(min(0.5, max(0.0, new_config["dropout"] + random.choice([-0.02, 0.02]))), 2)
28
- if random.random() < 0.3:
29
- new_config["memory"] = not new_config["memory"]
30
- return new_config
31
-
32
- def run_evolution(self, generations=5):
33
- current = self.base_config
34
- self.history.append(current)
35
- for _ in range(generations - 1):
36
- current = self.mutate(current)
37
- self.history.append(current)
 
 
1
  import random
2
+ import json
3
+ import csv
4
+ import io
5
 
6
  class EvoTransformer:
7
  def __init__(self):
8
  self.history = []
9
+ self.current = {}
10
+
11
+ def reset(self):
12
+ self.history = []
13
+ self.current = {}
14
+
15
+ def mutate(self, config):
16
+ config["layers"] = max(1, config["layers"] + random.choice([-1, 1]))
17
+ config["attention_heads"] = max(1, config["attention_heads"] + random.choice([-1, 1]))
18
+ config["ffn_dim"] = max(64, config["ffn_dim"] + random.choice([-256, 256]))
19
+ config["dropout"] = min(0.5, max(0.01, config["dropout"] + random.choice([-0.02, 0.02])))
20
+ config["memory"] = random.choice([True, False])
21
+ return config
22
+
23
+ def simulate_accuracy(self, config):
24
+ return round(random.uniform(70, 90), 2)
25
+
26
+ def estimate_parameters(self, config):
27
+ return round(
28
+ config["layers"] * config["attention_heads"] * config["ffn_dim"] / 1e6,
29
+ 2
30
+ )
31
+
32
+ def evolve(self, generations):
33
+ base_config = {
34
  "layers": 4,
35
  "attention_heads": 4,
36
  "ffn_dim": 1024,
 
38
  "memory": False
39
  }
40
 
41
+ for _ in range(generations):
42
+ base_config = self.mutate(base_config.copy())
43
+ acc = self.simulate_accuracy(base_config)
44
+ params = self.estimate_parameters(base_config)
45
+ record = {"traits": base_config.copy(), "accuracy": acc, "parameters": params}
46
+ self.history.append(record)
47
 
48
+ self.current = self.history[-1]
49
+
50
+ def get_best_config(self):
51
+ return self.current
52
+
53
+ def export_csv(self):
54
+ output = io.StringIO()
55
+ writer = csv.writer(output)
56
+ writer.writerow(["Generation", "Layers", "Attention Heads", "FFN Dim", "Dropout", "Memory", "Accuracy", "Params"])
57
+ for i, entry in enumerate(self.history):
58
+ t = entry["traits"]
59
+ writer.writerow([
60
+ i+1,
61
+ t["layers"], t["attention_heads"], t["ffn_dim"],
62
+ t["dropout"], t["memory"],
63
+ entry["accuracy"], entry["parameters"]
64
+ ])
65
+ return output.getvalue()
66
+
67
+ def export_json(self):
68
+ return json.dumps(self.history, indent=2)