-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
109 lines (88 loc) · 4.12 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
import argparse
import logging
from logging import getLogger
from recbole.utils import init_logger, init_seed, set_color
from recbole_gnn.config import Config
from recbole_gnn.utils import create_dataset, data_preparation, get_model, get_trainer
from trainer import MyTrainer
def run_single_model(args):
# configurations initialization
config = Config(
model=args.model,
dataset=args.dataset,
config_file_list=args.config_file_list
)
try:
assert config["enable_sparse"] in [True, False, None]
except AssertionError:
raise ValueError("Your config `enable_sparse` must be `True` or `False` or `None`")
init_seed(config['seed'], config['reproducibility'])
# logger initialization
init_logger(config)
logger = getLogger()
logger.info(config)
# dataset filtering
dataset = create_dataset(config)
logger.info(dataset)
# dataset splitting
train_data, valid_data, test_data = data_preparation(config, dataset)
# model loading and initialization
model = get_model(config['model'])(config, train_data.dataset).to(config['device'])
logger.info(model)
# trainer loading and initialization
if config['model'].lower() == "ncl":
trainer = get_trainer(config['MODEL_TYPE'], config['model'])(config, model)
else:
trainer = MyTrainer(config, model)
# model training
best_valid_score, best_valid_result = trainer.fit(
train_data, valid_data, saved=True, show_progress=config['show_progress']
)
# model evaluation
test_result = trainer.evaluate(test_data, load_best_model=True, show_progress=config['show_progress'])
logger.info(set_color('best valid ', 'yellow') + f': {best_valid_result}')
logger.info(set_color('test result', 'yellow') + f': {test_result}')
def objective_function(config_dict=None, config_file_list=None, saved=True):
r""" The default objective_function used in HyperTuning
Args:
config_dict (dict, optional): Parameters dictionary used to modify experiment parameters. Defaults to ``None``.
config_file_list (list, optional): Config files used to modify experiment parameters. Defaults to ``None``.
saved (bool, optional): Whether to save the model. Defaults to ``True``.
"""
config = Config(config_dict=config_dict, config_file_list=config_file_list)
try:
assert config["enable_sparse"] in [True, False, None]
except AssertionError:
raise ValueError("config `enable_sparse` must be `True` or `False` or `None`")
init_seed(config['seed'], config['reproducibility'])
logging.basicConfig(level=logging.ERROR)
dataset = create_dataset(config)
train_data, valid_data, test_data = data_preparation(config, dataset)
init_seed(config['seed'], config['reproducibility'])
model = get_model(config['model'])(config, train_data.dataset).to(config['device'])
trainer = MyTrainer(config, model)
best_valid_score, best_valid_result = trainer.fit(train_data, valid_data, verbose=False, saved=saved)
test_result = trainer.evaluate(test_data, load_best_model=saved)
return {
'model': config['model'],
'best_valid_score': best_valid_score,
'valid_score_bigger': config['valid_metric_bigger'],
'best_valid_result': best_valid_result,
'test_result': test_result
}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='bpr', help='name of models')
parser.add_argument('--dataset', type=str, default='ml-100k',
help='The datasets can be: amazon-kindle-store, yelp, amazon-books, QB-video.')
parser.add_argument('--config_files', type=str, default='', help='External config file name.')
args, _ = parser.parse_known_args()
# Config files
args.config_file_list = [
'properties/overall.yaml',
]
if args.config_files != '':
args.config_file_list.extend(args.config_files.split(","))
if args.dataset in ['yelp', 'amazon-books', 'amazon-kindle-store', 'QB-video']:
args.config_file_list.append(f'properties/{args.dataset}.yaml')
run_single_model(args)