HemanM commited on
Commit
d6397a3
·
verified ·
1 Parent(s): 947ad46

Update evo_transformer.py

Browse files
Files changed (1) hide show
  1. evo_transformer.py +27 -40
evo_transformer.py CHANGED
@@ -1,50 +1,37 @@
1
- # evo_transformer.py
2
  import random
 
3
 
4
  class EvoTransformer:
5
- def __init__(self, config=None):
6
- self.default_config = {
 
7
  "layers": 4,
8
  "attention_heads": 4,
9
  "ffn_dim": 1024,
10
  "dropout": 0.1,
11
- "memory": False,
12
  }
13
- self.config = config or self.default_config.copy()
14
- self.history = [self.config.copy()]
15
 
16
  def reset(self):
17
- self.config = self.default_config.copy()
18
- self.history = [self.config.copy()]
19
-
20
- def mutate(self):
21
- new_config = self.config.copy()
22
- trait = random.choice(list(new_config.keys()))
23
-
24
- if trait == "layers":
25
- new_config[trait] = max(1, new_config[trait] + random.choice([-1, 1]))
26
- elif trait == "attention_heads":
27
- new_config[trait] = random.choice([2, 4, 6, 8])
28
- elif trait == "ffn_dim":
29
- new_config[trait] = random.choice([512, 1024, 2048])
30
- elif trait == "dropout":
31
- new_config[trait] = round(min(max(0.0, new_config[trait] + random.uniform(-0.05, 0.05)), 0.5), 2)
32
- elif trait == "memory":
33
- new_config[trait] = not new_config[trait]
34
-
35
- self.config = new_config
36
- self.history.append(new_config.copy())
37
-
38
- def evolve(self, generations=3):
39
- for _ in range(generations):
40
- self.mutate()
41
-
42
- def get_history(self):
43
- return self.history
44
-
45
- def evaluate(self):
46
- score = round(random.uniform(0.85, 0.95), 4)
47
- return {"accuracy": score, "params": self.estimate_params()}
48
-
49
- def estimate_params(self):
50
- return round(10 + self.config["layers"] * self.config["ffn_dim"] * 1.0, 2)
 
 
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,
11
  "dropout": 0.1,
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)