-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
115 lines (96 loc) · 3.42 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
import logging
import sys
import abc
import keras
from keras import backend as K
import numpy as np
import tensorflow as tf
from sklearn.metrics import roc_auc_score
import document
import models
import settings
import utils
from lz import UserModeling
def train(config):
# "1. log config"
logging.basicConfig(
format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO,
handlers=[logging.FileHandler(config.log_output),
logging.StreamHandler()]
)
# "2. training loop: data, model and process"
UM = UserModeling(config)
training_data = UM.train
for i in range(config.rounds):
logging.info("launching the {} round of {}".format(i, m))
for epoch in range(config.epochs):
logging.info('[+] start epoch {}'.format(epoch))
model = UM.build_model(epoch)
history = model.fit_generator(
training_data,
config.training_step,
epochs=epoch + 1,
initial_epoch=epoch,
callbacks=None,
verbose=1 if config.debug and not config.background else 2)
utils.logging_history(history)
try:
evaluations = model.evaluate_generator(UM.valid,
config.validation_step,
verbose=1 if config.debug and not config.background else 2)
assert len(evaluations) == len(model.metrics_names)
utils.logging_evaluation(dict(zip(model.metrics_names, evaluations)))
except:
pass
if hasattr(UM, 'callback_valid'):
UM.callback_valid(epoch)
logging.info('[-] finish epoch {}'.format(epoch))
if hasattr(UM, 'callback'):
UM.callback(epoch)
# "3. save model to .json and .pkl"
print("saving model to file ...")
UM.save_model()
# K.clear_session()
return 0
def test(config):
logging.basicConfig(
format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO,
handlers=[logging.FileHandler(config.log_output),
logging.StreamHandler()]
)
print('start testing')
model = utils.load_model(config.model_output)
UM = UserModeling(config)
UM.model = model
UM.callback(1)
def users(config: settings.Config):
logging.basicConfig(
format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO,
handlers=[logging.FileHandler(config.log_output),
logging.StreamHandler()]
)
UM = RunUserModel(config)
UM.save_result()
return 0
def args_parser(args):
args = dict(a.split("=") for a in args)
assert "models" in args.keys()
args["models"] = args["models"].split(",")
if "rounds" in args.keys():
args["rounds"] = int(args["rounds"])
if "epochs" in args.keys():
args["epochs"] = int(args["epochs"])
return args
if __name__ == "__main__":
import os
if not os.path.exists('./log'):
os.mkdir('./log')
if not os.path.exists('./models'):
os.mkdir('./models')
args = args_parser(sys.argv[1:])
for m in args["models"]:
config = settings.Config(rounds=args["rounds"], epochs=args["epochs"], arch=m, name=m)
train(config=config)