-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
161 lines (140 loc) · 4.56 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Entrypoint of this assignment."""
import os
import sys
import logging
from typing import List
from constants import (
TASK_A_DATA_ABS_PATH,
TASK_B_DATA_ABS_PATH,
TASK_A_CONFIG_PATH,
TASK_B_CONFIG_PATH,
)
from utils.logger import logger, set_log_level
from A.solution import SolutionA
from B.solution import SolutionB
CWD = os.getcwd()
def setup_parse():
import argparse
description = "AMLS Final Assignment"
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-d",
"--debug",
help="Print lots of debugging statements",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
"-v",
"--verbose",
help="Be verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
)
parser.set_defaults(whether_output=True)
subparsers = parser.add_subparsers(dest="action", help="actions provided")
subparsers.required = True
solve_subparser = subparsers.add_parser("solve")
subparsers.add_parser("info")
solve_subparser.add_argument(
"--task",
action="store",
default="all",
help="task to solve: A, B, or all; default: all",
)
solve_subparser.add_argument(
"--stages",
action="store",
default="train,test",
help="task stages: val, train, test, or all; default: train,test",
)
solve_subparser.add_argument(
"--device",
action="store",
default="cuda",
help="Device ordinal for xgboost, available options: cpu, cuda, and gpu; default: cuda",
)
solve_subparser.add_argument(
"--save",
action="store",
default="True",
help="Save results or not, available options: True or False; default: True",
)
solve_subparser.add_argument(
"--early_stopping_rounds",
action="store",
default="3",
help="Used to prevent overfitting; require >= 0, default: 3",
)
args, _ = parser.parse_known_args()
return args
def print_info():
print("-------------------------------------")
print("| AMLS Assignment 23-24 |")
print("| Name: Zhaoyan Lu |")
print("| Student No: 23049710 |")
print("-------------------------------------")
def solve(
task: str,
stages: List[str],
device: str,
early_stopping_rounds: int,
save: bool = True,
):
logger.info("-----------[Tasks running]-----------")
if task in ["A", "all"]:
if os.path.exists(TASK_A_DATA_ABS_PATH):
solution_A = SolutionA(
dataset_path=TASK_A_DATA_ABS_PATH,
device=device,
config_path=TASK_A_CONFIG_PATH,
save_result=save,
early_stopping_rounds=early_stopping_rounds,
)
solution_A.solve(stages)
else:
raise Exception(
f"No dataset for task A: {TASK_A_DATA_ABS_PATH}! Please run `make download`."
)
if task in ["B", "all"]:
if os.path.exists(TASK_B_DATA_ABS_PATH):
solution_B = SolutionB(
dataset_path=TASK_B_DATA_ABS_PATH,
device=device,
config_path=TASK_B_CONFIG_PATH,
save_result=save,
early_stopping_rounds=early_stopping_rounds,
)
solution_B.solve(stages)
else:
raise Exception(
f"No dataset for task B: {TASK_B_DATA_ABS_PATH}! Please run `make download`."
)
logger.info("---------All tasks finished!---------")
def main():
try:
args = setup_parse()
set_log_level(args.loglevel)
if args.action == "info":
print_info()
elif args.action == "solve":
stages = args.stages.split(",")
save_result = args.save.lower() == "true"
assert (
args.early_stopping_rounds.isdigit()
and int(args.early_stopping_rounds) >= 0
), f"ERROR: early_stopping_rounds: '{args.early_stopping_rounds}' needs to be greater than or equal to 0."
early_stopping_rounds = int(args.early_stopping_rounds)
solve(args.task, stages, args.device, early_stopping_rounds, save_result)
else:
raise Exception(f"Unsupported action: {args.action}")
except Exception as e:
print(e)
sys.exit(1)
if __name__ == "__main__":
main()