Spaces:
Sleeping
Sleeping
Update evo_transformer.py
Browse files- 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
|
6 |
-
self.
|
|
|
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.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
self.
|
36 |
-
|
37 |
-
|
38 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|