HemanM commited on
Commit
5490a10
·
verified ·
1 Parent(s): 76d9193

Update evo_transformer.py

Browse files
Files changed (1) hide show
  1. evo_transformer.py +14 -32
evo_transformer.py CHANGED
@@ -1,5 +1,6 @@
 
1
  import random
2
- import matplotlib.pyplot as plt
3
 
4
  class EvoTransformer:
5
  def __init__(self, config=None):
@@ -15,7 +16,7 @@ class EvoTransformer:
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,7 +31,7 @@ class EvoTransformer:
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,38 +39,19 @@ class EvoTransformer:
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())
 
1
+ # evo_transformer.py
2
  import random
3
+ import json
4
 
5
  class EvoTransformer:
6
  def __init__(self, config=None):
 
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
  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
  return self.history
40
 
41
  def evaluate(self):
 
42
  score = round(random.uniform(0.85, 0.95), 4)
43
+ return {"accuracy": score, "params": self.estimate_params()}
 
 
 
44
 
45
  def estimate_params(self):
46
  return round(10 + self.config["layers"] * self.config["ffn_dim"] * 0.001, 2)
47
 
48
+ def export_csv(self):
49
+ headers = list(self.history[0].keys())
50
+ lines = [",".join(headers)]
51
+ for config in self.history:
52
+ line = ",".join([str(config[h]) for h in headers])
53
+ lines.append(line)
54
+ return "\n".join(lines)
 
 
 
 
 
 
 
 
 
 
55
 
56
+ def export_json(self):
57
+ return json.dumps(self.history, indent=2)