forked from reczoo/FuxiCTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample3_DeepFM_with_npz_input.py
71 lines (62 loc) · 3.01 KB
/
example3_DeepFM_with_npz_input.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
# =========================================================================
# Copyright (C) 2024. The FuxiCTR Library. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========================================================================
import sys
sys.path.append('../')
import os
import logging
from fuxictr import datasets
from datetime import datetime
from fuxictr.utils import load_config, set_logger, print_to_json
from fuxictr.features import FeatureMap
from fuxictr.pytorch.torch_utils import seed_everything
from fuxictr.pytorch.dataloaders import RankDataLoader
from model_zoo import DeepFM
if __name__ == '__main__':
# Load params from config files
config_dir = './config/example3_config'
experiment_id = 'DeepFM_test_npz' # corresponds to input `data/tiny_npz`
params = load_config(config_dir, experiment_id)
# set up logger and random seed
set_logger(params)
logging.info("Params: " + print_to_json(params))
seed_everything(seed=params['seed'])
# Load feature_map from json
data_dir = os.path.join(params['data_root'], params['dataset_id'])
feature_map_json = os.path.join(data_dir, "feature_map.json")
feature_map = FeatureMap(params['dataset_id'], data_dir)
feature_map.load(feature_map_json, params)
logging.info("Feature specs: " + print_to_json(feature_map.features))
# Get train and validation data generators
train_gen, valid_gen = RankDataLoader(feature_map,
stage='train',
train_data=params['train_data'],
valid_data=params['valid_data'],
batch_size=params['batch_size'],
data_format=params["data_format"],
shuffle=params['shuffle']).make_iterator()
# Model initialization and fitting
model = DeepFM(feature_map, **params)
model.fit(train_gen, validation_data=valid_gen, epochs=params['epochs'])
logging.info('***** Validation evaluation *****')
model.evaluate(valid_gen)
logging.info('***** Test evaluation *****')
test_gen = RankDataLoader(feature_map,
stage='test',
test_data=params['test_data'],
batch_size=params['batch_size'],
data_format=params["data_format"],
shuffle=False).make_iterator()
model.evaluate(test_gen)