HemanM commited on
Commit
b63d039
·
verified ·
1 Parent(s): 303646a

Update evo_transformer.py

Browse files
Files changed (1) hide show
  1. evo_transformer.py +35 -8
evo_transformer.py CHANGED
@@ -1,9 +1,8 @@
1
- # evo_transformer.py
2
  import random
 
3
 
4
  class EvoTransformer:
5
  def __init__(self, config=None):
6
- # Initialize with default or passed config
7
  self.config = config or {
8
  "layers": 4,
9
  "attention_heads": 4,
@@ -16,7 +15,7 @@ class EvoTransformer:
16
  def mutate(self):
17
  new_config = self.config.copy()
18
  trait = random.choice(list(new_config.keys()))
19
-
20
  if trait == "layers":
21
  new_config[trait] = max(1, new_config[trait] + random.choice([-1, 1]))
22
  elif trait == "attention_heads":
@@ -31,7 +30,7 @@ class EvoTransformer:
31
  self.config = new_config
32
  self.history.append(new_config.copy())
33
 
34
- def evolve(self, generations=3):
35
  for _ in range(generations):
36
  self.mutate()
37
 
@@ -39,10 +38,38 @@ class EvoTransformer:
39
  return self.history
40
 
41
  def evaluate(self):
42
- # Simulate an accuracy score for demo purposes
43
  score = round(random.uniform(0.85, 0.95), 4)
44
- return {"accuracy": score, "params": self.estimate_params()}
 
 
 
45
 
46
  def estimate_params(self):
47
- # Simulated parameter count based on config
48
- return 10 + self.config["layers"] * self.config["ffn_dim"] * 0.001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import random
2
+ import matplotlib.pyplot as plt
3
 
4
  class EvoTransformer:
5
  def __init__(self, config=None):
 
6
  self.config = config or {
7
  "layers": 4,
8
  "attention_heads": 4,
 
15
  def mutate(self):
16
  new_config = self.config.copy()
17
  trait = random.choice(list(new_config.keys()))
18
+
19
  if trait == "layers":
20
  new_config[trait] = max(1, new_config[trait] + random.choice([-1, 1]))
21
  elif trait == "attention_heads":
 
30
  self.config = new_config
31
  self.history.append(new_config.copy())
32
 
33
+ def evolve(self, generations=5):
34
  for _ in range(generations):
35
  self.mutate()
36
 
 
38
  return self.history
39
 
40
  def evaluate(self):
41
+ # Simulated accuracy for demo
42
  score = round(random.uniform(0.85, 0.95), 4)
43
+ return {
44
+ "accuracy": score,
45
+ "params": self.estimate_params()
46
+ }
47
 
48
  def estimate_params(self):
49
+ return round(10 + self.config["layers"] * self.config["ffn_dim"] * 0.001, 2)
50
+
51
+ def plot_evolution(self):
52
+ layers = [cfg["layers"] for cfg in self.history]
53
+ heads = [cfg["attention_heads"] for cfg in self.history]
54
+ ffn_dims = [cfg["ffn_dim"] for cfg in self.history]
55
+
56
+ plt.figure(figsize=(10, 6))
57
+ plt.plot(layers, label="Layers", marker='o', color='orange')
58
+ plt.plot(heads, label="Attention Heads", marker='s', color='blue')
59
+ plt.plot(ffn_dims, label="FFN Dim", marker='^', color='green')
60
+ plt.xlabel("Generation")
61
+ plt.ylabel("Value")
62
+ plt.title("Evolution of Traits")
63
+ plt.legend()
64
+ plt.grid(True)
65
+ plt.tight_layout()
66
+ plt.show()
67
+
68
+
69
+ # Run test locally
70
+ if __name__ == "__main__":
71
+ evo = EvoTransformer()
72
+ evo.evolve(generations=8)
73
+ evo.plot_evolution()
74
+ print("Final Config:", evo.config)
75
+ print("Evaluation:", evo.evaluate())