-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupervised.py
More file actions
191 lines (158 loc) · 6.48 KB
/
supervised.py
File metadata and controls
191 lines (158 loc) · 6.48 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import warnings
warnings.filterwarnings("ignore")
import hydra
from omegaconf import OmegaConf
from sklearn.model_selection import RandomizedSearchCV
from data.dataset import *
from utils.utils import *
from sklearn.metrics import accuracy_score, balanced_accuracy_score, f1_score, confusion_matrix
import json
import pickle
@hydra.main(version_base=None, config_path="conf", config_name="config.yaml")
def main_sup_baseline(args):
global logger
if hasattr(args, "seed"):
set_seed(args.seed)
print(f"set seed as {args.seed}")
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
logger = get_logger(args)
logger.info(OmegaConf.to_yaml(args))
if not os.path.exists(args.out_dir):
os.makedirs(args.out_dir)
dataset = Dataset(args, logger)
param_grid = get_param_grid(args)
iteration_num = 5
if args.model == "lr":
from sklearn.linear_model import LogisticRegression
source_model = LogisticRegression()
source_model = source_model.fit(dataset.train_x, dataset.train_y.argmax(1))
elif args.model == "knn":
from sklearn.neighbors import KNeighborsClassifier
source_model = KNeighborsClassifier()
rs = RandomizedSearchCV(
estimator=source_model,
param_distributions=param_grid,
n_iter=iteration_num,
cv=2,
verbose=1,
n_jobs=1,
)
rs.fit(dataset.train_x, dataset.train_y.argmax(1))
best_params = rs.best_params_
print(f"best params are: {rs.best_params_}")
source_model = KNeighborsClassifier(**best_params)
source_model.fit(dataset.train_x, dataset.train_y.argmax(1))
elif args.model == "rf":
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
source_model = RandomForestClassifier(random_state=args.seed)
rs = RandomizedSearchCV(
estimator=source_model,
param_distributions=param_grid,
n_iter=iteration_num,
cv=2,
verbose=1,
n_jobs=-1,
)
rs.fit(dataset.train_x, dataset.train_y.argmax(1))
best_params = rs.best_params_
print(f"best params are: {rs.best_params_}")
source_model = RandomForestClassifier(**best_params, random_state=args.seed)
source_model.fit(dataset.train_x, dataset.train_y.argmax(1))
elif args.model == "xgboost":
if dataset.train_y.argmax(1).max() == 1:
objective = "binary:logistic"
else:
objective = "multi:softprob"
from xgboost import XGBRegressor, XGBClassifier
source_model = XGBClassifier(tree_method='hist', device=f"{args.device}", random_state=args.seed)
rs = RandomizedSearchCV(
estimator=source_model,
param_distributions=param_grid,
n_iter=iteration_num,
cv=2,
verbose=1,
n_jobs=-1,
)
rs.fit(dataset.train_x, dataset.train_y.argmax(1))
best_params = rs.best_params_
print(f"best params are: {rs.best_params_}")
source_model = XGBClassifier(**best_params, random_state=args.seed)
source_model.fit(dataset.train_x, dataset.train_y.argmax(1))
elif args.model == "catboost":
from catboost import CatBoostClassifier, CatBoostRegressor
train_x = dataset.train_x
catboost_model = CatBoostClassifier(task_type='GPU', devices='0:1:2:3')
train_y = dataset.train_y.argmax(1)
rs = RandomizedSearchCV(
estimator=catboost_model,
param_distributions=param_grid,
n_iter=iteration_num,
cv=2,
verbose=1,
n_jobs=1,
)
rs.fit(train_x, train_y)
print(f"best params are: {rs.best_params_}")
source_model = CatBoostClassifier(**rs.best_params_, random_state=args.seed)
source_model.fit(train_x, train_y)
else:
raise NotImplementedError
test_acc, test_len = 0, 0
ESTIMATED_BEFORE_LABEL_LIST = []
GROUND_TRUTH_LABEL_LIST = []
for test_x, test_mask_x, test_y in dataset.test_loader:
estimated_y = source_model.predict(test_x)
test_acc += (estimated_y == np.argmax(np.array(test_y), axis=-1)).sum()
test_len += test_x.shape[0]
ESTIMATED_BEFORE_LABEL_LIST.extend(list(np.argmax(np.array(test_y), axis=-1)))
GROUND_TRUTH_LABEL_LIST.extend(list(estimated_y))
logger.info(
f"before adaptation | acc {accuracy_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST):.4f}, bacc {balanced_accuracy_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST):.4f}, macro f1-score {f1_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST, average='macro'):.4f}"
)
print(f"before adaptation | acc {accuracy_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST):.4f}, bacc {balanced_accuracy_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST):.4f}, macro f1-score {f1_score(GROUND_TRUTH_LABEL_LIST, ESTIMATED_BEFORE_LABEL_LIST, average='macro'):.4f}")
logger.info(f"using {args.model} | test acc {test_acc / test_len:.4f}")
dict_for_saving = {
'test_acc': float(test_acc / test_len)
}
out_path = os.path.join(args.out_dir, 'supervised_results.json')
os.makedirs(args.out_dir, exist_ok=True)
with open(out_path, 'w') as f:
json.dump(dict_for_saving, f)
return test_acc
def get_param_grid(args):
param_grid = {}
if args.model == "xgboost":
param_grid = {
"n_estimators": np.arange(50, 200, 5),
"learning_rate": np.linspace(0.01, 1, 20),
"max_depth": np.arange(2, 12, 1),
"gamma": np.linspace(0, 0.5, 11),
}
elif args.model == "rf":
param_grid = {
"n_estimators": np.arange(50, 200, 5),
"max_depth": np.arange(2, 12, 1),
}
elif args.model == "knn":
param_grid = {
"n_neighbors": np.arange(2, 12, 1),
}
elif args.model == "lr":
pass
elif args.model == "catboost":
param_grid = {
"iterations": np.arange(50, 2000, 50),
"learning_rate": np.linspace(0.01, 1, 20),
"depth": np.arange(5, 40, 5),
}
else:
raise NotImplementedError
return param_grid
if __name__ == "__main__":
import time
start_time = time.time()
acc = main_sup_baseline()
end_time = time.time()
print(f"Time Consumption: {end_time - start_time}")