-
Notifications
You must be signed in to change notification settings - Fork 0
/
soft_voting.py
63 lines (51 loc) · 1.48 KB
/
soft_voting.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import dotenv
dotenv.load_dotenv(
override=True,
)
import os
import warnings
os.environ["HYDRA_FULL_ERROR"] = "1"
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
import hydra
from omegaconf import DictConfig
@hydra.main(
config_path="configs/",
config_name="voting.yaml",
)
def softly_vote_logits(
config: DictConfig,
) -> None:
connected_dir = config.connected_dir
voted_logit = config.voted_logit
submission_file = config.submission_file
target_column_name = config.target_column_name
voted_file = config.voted_file
votings = config.votings
weights = list(votings.values())
if not np.isclose(sum(weights), 1):
raise ValueError(f"summation of weights({sum(weights)}) is not equal to 1")
weighted_logits = None
for logit_file, weight in votings.items():
try:
logit = np.load(f"{connected_dir}/logits/{logit_file}.npy")
except:
raise FileNotFoundError(f"logit file {logit_file} does not exist")
if weighted_logits is None:
weighted_logits = logit * weight
else:
weighted_logits += logit * weight
ensemble_predictions = weighted_logits
submission_df = pd.read_csv(submission_file)
np.save(
voted_logit,
weighted_logits,
)
submission_df[target_column_name] = ensemble_predictions
submission_df.to_csv(
voted_file,
index=False,
)
if __name__ == "__main__":
softly_vote_logits()