-
Notifications
You must be signed in to change notification settings - Fork 0
/
soft_ensemble_test.py
49 lines (39 loc) · 1.21 KB
/
soft_ensemble_test.py
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
import os
import yaml
import pandas as pd
with open("./config.yaml") as file:
config = yaml.safe_load(file)
model_dir = os.path.join(
config["model"]["model_loc"],
config["dataset"]["file_name"],
)
_mode = config["ensemble"]["mode"]
notoff_probs = {}
for model_name in config["ensemble"]["models"]:
csv_file = os.path.join(model_dir, model_name, f"_{_mode}.csv")
df = pd.read_csv(csv_file)
for _, row in df.iterrows():
id = row["id"]
notoff_prob = (
row["probability"] if row["label"] == 0 else 1 - row["probability"]
)
if id in notoff_probs:
notoff_probs[id]["prob"] += notoff_prob
else:
notoff_probs[id] = {"prob": notoff_prob, "text": row["text"]}
_ids, _probs = [], []
_labels, _text = [], []
for id, stats in notoff_probs.items():
prob = stats["prob"] / 3
text = stats["text"]
label = 0 if prob > 0.5 else 1
_ids.append(id)
_text.append(text)
_labels.append(label)
_probs.append(prob if label == 0 else 1 - prob)
df = pd.DataFrame({"id": _ids, "text": _text, "pred": _labels})
df.to_csv(
f"{model_dir}/{config['dataset']['file_name']}_{_mode}_preds.tsv",
sep="\t",
index=False,
)