Spaces:
Sleeping
Sleeping
Update retrain_from_feedback.py
Browse files- retrain_from_feedback.py +69 -40
retrain_from_feedback.py
CHANGED
@@ -1,41 +1,70 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
return
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# retrain_from_feedback.py
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import pandas as pd
|
7 |
+
from transformers import AutoTokenizer
|
8 |
+
from evo_architecture import mutate_genome, log_genome, default_config
|
9 |
+
from evo_model import EvoTransformerV22
|
10 |
import os
|
11 |
+
|
12 |
+
MODEL_PATH = "evo_hellaswag.pt"
|
13 |
+
CSV_PATH = "feedback_log.csv"
|
14 |
+
|
15 |
+
def train_evo():
|
16 |
+
if not os.path.exists(CSV_PATH):
|
17 |
+
print("⚠️ No feedback_log.csv file found.")
|
18 |
+
return
|
19 |
+
|
20 |
+
df = pd.read_csv(CSV_PATH)
|
21 |
+
if df.empty:
|
22 |
+
print("⚠️ feedback_log.csv is empty.")
|
23 |
+
return
|
24 |
+
|
25 |
+
# Step 1: Evolve new architecture
|
26 |
+
base_config = default_config()
|
27 |
+
evolved_config = mutate_genome(base_config)
|
28 |
+
print("🧬 New mutated config:", evolved_config)
|
29 |
+
|
30 |
+
# Step 2: Initialize model with evolved config
|
31 |
+
model = EvoTransformerV22(
|
32 |
+
num_layers=evolved_config["num_layers"],
|
33 |
+
num_heads=evolved_config["num_heads"],
|
34 |
+
ffn_dim=evolved_config["ffn_dim"],
|
35 |
+
memory_enabled=evolved_config["memory_enabled"]
|
36 |
+
)
|
37 |
+
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
39 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
40 |
+
model.train()
|
41 |
+
|
42 |
+
# Step 3: Train on feedback
|
43 |
+
total_loss = 0.0
|
44 |
+
for _, row in df.iterrows():
|
45 |
+
question = row["question"]
|
46 |
+
opt1 = row["option1"]
|
47 |
+
opt2 = row["option2"]
|
48 |
+
answer = row["answer"]
|
49 |
+
|
50 |
+
label = torch.tensor([1.0 if answer.strip() == opt2.strip() else 0.0])
|
51 |
+
|
52 |
+
input_text = f"{question} [SEP] {opt2 if label.item() == 1 else opt1}"
|
53 |
+
encoded = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
54 |
+
|
55 |
+
logits = model(encoded["input_ids"])
|
56 |
+
loss = F.binary_cross_entropy_with_logits(logits.squeeze(), label)
|
57 |
+
loss.backward()
|
58 |
+
optimizer.step()
|
59 |
+
optimizer.zero_grad()
|
60 |
+
total_loss += loss.item()
|
61 |
+
|
62 |
+
# Step 4: Save new model weights
|
63 |
+
torch.save(model.state_dict(), MODEL_PATH)
|
64 |
+
print("✅ Evo model retrained and saved.")
|
65 |
+
|
66 |
+
# Step 5: Log genome and score (loss as proxy)
|
67 |
+
avg_loss = total_loss / len(df)
|
68 |
+
score = 1.0 - avg_loss # Use (1 - loss) as crude fitness
|
69 |
+
log_genome(evolved_config, performance=round(score, 4))
|
70 |
+
print("🧬 Genome logged with score:", round(score, 4))
|