Spaces:
Sleeping
Sleeping
Update retrain_from_feedback.py
Browse files- retrain_from_feedback.py +21 -16
retrain_from_feedback.py
CHANGED
|
@@ -15,39 +15,42 @@ CSV_PATH = "feedback_log.csv"
|
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 43 |
total_loss = 0.0
|
| 44 |
-
for _, row in
|
| 45 |
question = row["question"]
|
| 46 |
opt1 = row["option1"]
|
| 47 |
opt2 = row["option2"]
|
| 48 |
-
|
| 49 |
|
| 50 |
-
label = torch.tensor([1.0 if
|
| 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)
|
|
@@ -59,12 +62,14 @@ def train_evo():
|
|
| 59 |
optimizer.zero_grad()
|
| 60 |
total_loss += loss.item()
|
| 61 |
|
| 62 |
-
# Step 4: Save
|
| 63 |
torch.save(model.state_dict(), MODEL_PATH)
|
| 64 |
print("✅ Evo model retrained and saved.")
|
| 65 |
|
| 66 |
-
# Step 5: Log genome
|
| 67 |
-
avg_loss = total_loss / len(
|
| 68 |
-
|
| 69 |
-
log_genome(evolved_config,
|
| 70 |
-
print("🧬 Genome logged with score:",
|
|
|
|
|
|
|
|
|
| 15 |
def train_evo():
|
| 16 |
if not os.path.exists(CSV_PATH):
|
| 17 |
print("⚠️ No feedback_log.csv file found.")
|
| 18 |
+
return "⚠️ No feedback data file found."
|
| 19 |
|
| 20 |
df = pd.read_csv(CSV_PATH)
|
| 21 |
+
|
| 22 |
+
# ✅ Only use rows where vote is Evo or GPT
|
| 23 |
+
usable_df = df[df["vote"].isin(["Evo", "GPT"])].copy()
|
| 24 |
+
|
| 25 |
+
if usable_df.empty:
|
| 26 |
+
print("⚠️ No usable feedback data. Please vote on Evo or GPT.")
|
| 27 |
+
return "⚠️ No usable feedback data. Please vote on Evo or GPT."
|
| 28 |
|
| 29 |
# Step 1: Evolve new architecture
|
| 30 |
base_config = default_config()
|
| 31 |
evolved_config = mutate_genome(base_config)
|
| 32 |
print("🧬 New mutated config:", evolved_config)
|
| 33 |
|
| 34 |
+
# Step 2: Initialize model
|
| 35 |
model = EvoTransformerV22(
|
| 36 |
num_layers=evolved_config["num_layers"],
|
| 37 |
num_heads=evolved_config["num_heads"],
|
| 38 |
ffn_dim=evolved_config["ffn_dim"],
|
| 39 |
memory_enabled=evolved_config["memory_enabled"]
|
| 40 |
)
|
|
|
|
| 41 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 42 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 43 |
model.train()
|
| 44 |
|
| 45 |
+
# Step 3: Train using feedback
|
| 46 |
total_loss = 0.0
|
| 47 |
+
for _, row in usable_df.iterrows():
|
| 48 |
question = row["question"]
|
| 49 |
opt1 = row["option1"]
|
| 50 |
opt2 = row["option2"]
|
| 51 |
+
evo_answer = row["evo_answer"]
|
| 52 |
|
| 53 |
+
label = torch.tensor([1.0 if evo_answer.strip() == opt2.strip() else 0.0])
|
| 54 |
|
| 55 |
input_text = f"{question} [SEP] {opt2 if label.item() == 1 else opt1}"
|
| 56 |
encoded = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
|
|
|
| 62 |
optimizer.zero_grad()
|
| 63 |
total_loss += loss.item()
|
| 64 |
|
| 65 |
+
# Step 4: Save the retrained model
|
| 66 |
torch.save(model.state_dict(), MODEL_PATH)
|
| 67 |
print("✅ Evo model retrained and saved.")
|
| 68 |
|
| 69 |
+
# Step 5: Log genome with fitness score (1 - avg_loss)
|
| 70 |
+
avg_loss = total_loss / len(usable_df)
|
| 71 |
+
fitness = round(1.0 - avg_loss, 4)
|
| 72 |
+
log_genome(evolved_config, score=fitness)
|
| 73 |
+
print("🧬 Genome logged with score:", fitness)
|
| 74 |
+
|
| 75 |
+
return f"✅ Evo retrained. Loss: {avg_loss:.4f}, Fitness: {fitness}"
|