-
Notifications
You must be signed in to change notification settings - Fork 1
/
resume_run_recbole.py
177 lines (143 loc) · 7.35 KB
/
resume_run_recbole.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# @Time : 2020/10/6
# @Author : Shanlei Mu
# @Email : slmu@ruc.edu.cn
"""
recbole.quick_start
########################
"""
import logging
from logging import getLogger
import torch
import pickle
import sys
import os
import argparse
import glob
from recbole.config import Config
from recbole.data import create_dataset, data_preparation, save_split_dataloaders, load_split_dataloaders
from recbole.utils import init_logger, get_model, get_trainer, init_seed, logger, set_color
def get_latest_model_file(model, dataset):
r"""
A function to get the latest model file.
"""
saved_dir = f'saved/saved_{dataset}/'
pattern = saved_dir + f'{model}-*.pth'
files = glob.glob(pattern)
if files:
latest_file = max(files, key=os.path.getctime)
return latest_file
else:
raise FileNotFoundError(f"No saved model file found for {model} and {dataset}")
def run_recbole(model=None, dataset=None, config_file_list=None, config_dict=None, saved=True, model_file=None):
r""" A fast running api, which includes the complete process of
training and testing a model on a specified dataset
Args:
model (str, optional): Model name. Defaults to ``None``.
dataset (str, optional): Dataset name. Defaults to ``None``.
config_file_list (list, optional): Config files used to modify experiment parameters. Defaults to ``None``.
config_dict (dict, optional): Parameters dictionary used to modify experiment parameters. Defaults to ``None``.
saved (bool, optional): Whether to save the model. Defaults to ``True``.
"""
# configurations initialization
# model_file = f'saved/saved_{model}/{dataset}_{model}.pth'
config, model, dataset, train_data, valid_data, test_data = load_data_and_model(
model=model,
dataset=dataset,
model_file=model_file,
dataset_file=None,
dataloader_file=None,
config_file_list=config_file_list,
config_dict=config_dict
)
# config = Config(model='UGF_MF', dataset='yahoo', config_file_list=['UGF_MF.yaml'])
# train_data, valid_data, test_data = data_preparation(config, dataset)
init_seed(config['seed'], config['reproducibility'])
# logger initialization
init_logger(config)
logger = getLogger()
logger.info(config)
logger.info(dataset)
logger.info(model)
# trainer loading and initialization
trainer = get_trainer(config['MODEL_TYPE'], config['model'])(config, model)
trainer.resume_checkpoint(resume_file=model_file)
# trainer._save_sst_embed(train_data)
# model training
# best_valid_score, best_valid_result = trainer.fit(
# train_data, valid_data, saved=saved, show_progress=config['show_progress']
# )
# When calculate ItemCoverage metrics, we need to run this code for set item_nums in eval_collector.
trainer.eval_collector.data_collect(train_data)
# model evaluation
test_result = trainer.evaluate(test_data, load_best_model=saved, model_file=model_file,show_progress=config['show_progress'])
valid_result = trainer.evaluate(valid_data, load_best_model=saved, model_file=model_file, show_progress=config['show_progress'])
logger.info(set_color('test result', 'yellow') + f': {test_result}')
# best_valid_result = trainer.evaluate(test_data, load_best_model=saved, model_file=model_file, show_progress=config['show_progress'])
logger.info(set_color('valid result', 'yellow') + f': {valid_result}')
# logger.info(set_color('best valid ', 'yellow') + f': {best_valid_result}')
return valid_result[config['valid_metric'].lower()]
# return best_valid_result[config['valid_metric'].lower()]
# return {
# 'best_valid_score': best_valid_score,
# 'valid_score_bigger': config['valid_metric_bigger'],
# 'best_valid_result': best_valid_result,
# 'test_result': test_result
# }
def load_data_and_model(model, dataset, model_file, dataset_file=None, dataloader_file=None, config_file_list=None, config_dict=None):
r"""Load filtered dataset, split dataloaders and saved model.
Args:
model_file (str): The path of saved model file.
dataset_file (str, optional): The path of filtered dataset. Defaults to ``None``.
dataloader_file (str, optional): The path of split dataloaders. Defaults to ``None``.
Note:
The :attr:`dataset` will be loaded or created according to the following strategy:
If :attr:`dataset_file` is not ``None``, the :attr:`dataset` will be loaded from :attr:`dataset_file`.
If :attr:`dataset_file` is ``None`` and :attr:`dataloader_file` is ``None``,
the :attr:`dataset` will be created according to :attr:`config`.
If :attr:`dataset_file` is ``None`` and :attr:`dataloader_file` is not ``None``,
the :attr:`dataset` will neither be loaded or created.
The :attr:`dataloader` will be loaded or created according to the following strategy:
If :attr:`dataloader_file` is not ``None``, the :attr:`dataloader` will be loaded from :attr:`dataloader_file`.
If :attr:`dataloader_file` is ``None``, the :attr:`dataloader` will be created according to :attr:`config`.
Returns:
tuple:
- config (Config): An instance object of Config, which record parameter information in :attr:`model_file`.
- model (AbstractRecommender): The model load from :attr:`model_file`.
- dataset (Dataset): The filtered dataset.
- train_data (AbstractDataLoader): The dataloader for training.
- valid_data (AbstractDataLoader): The dataloader for validation.
- test_data (AbstractDataLoader): The dataloader for testing.
"""
checkpoint = torch.load(model_file)
if config_file_list is not None:
config = Config(model=model, dataset=dataset, config_file_list=config_file_list, config_dict=config_dict)
else:
config = checkpoint['config']
init_logger(config)
dataset = None
if dataset_file:
with open(dataset_file, 'rb') as f:
dataset = pickle.load(f)
if dataloader_file:
train_data, valid_data, test_data = load_split_dataloaders(config)
else:
if dataset is None:
dataset = create_dataset(config)
train_data, valid_data, test_data = data_preparation(config, dataset)
model = get_model(config['model'])(config, train_data.dataset).to(config['device'])
model.load_state_dict(checkpoint['state_dict'])
model.load_other_parameter(checkpoint.get('other_parameter'))
return config, model, dataset, train_data, valid_data, test_data
if __name__ == '__main__':
os.chdir(sys.path[0])
parser = argparse.ArgumentParser()
#parser.add_argument('--model', '-m', type=str, default='PFCN_PMF', help='name of models')
parser.add_argument('--model', '-m', type=str, default='ItemKNN', help='name of models')
parser.add_argument('--dataset', '-d', type=str, default='ml-1M', help='name of datasets')
parser.add_argument('--config_files', type=str, default='test.yaml', help='config files')
args, _ = parser.parse_known_args()
config_file_list = args.config_files.strip().split(' ') if args.config_files else None
model_file = get_latest_model_file(args.model, args.dataset)
results = run_recbole(model=args.model, dataset=args.dataset, config_file_list=config_file_list, model_file=model_file)
print('---'*10)
#print(results)