-
Notifications
You must be signed in to change notification settings - Fork 13
/
deep.py
75 lines (63 loc) · 2.85 KB
/
deep.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
import shutil
from argparse import ArgumentParser
import tensorflow as tf
from trainers.conf_utils import get_run_config, get_train_spec, get_exporter, get_eval_spec
from trainers.ml_100k import get_feature_columns, get_input_fn, serving_input_fn
def train_and_evaluate(args):
# paths
train_csv = args.train_csv
test_csv = args.test_csv
job_dir = args.job_dir
restore = args.restore
# model
embedding_size = args.embedding_size
hidden_units = args.hidden_units
dropout = args.dropout
# training
batch_size = args.batch_size
train_steps = args.train_steps
# init
tf.logging.set_verbosity(tf.logging.INFO)
if not restore:
shutil.rmtree(job_dir, ignore_errors=True)
# estimator
feature_columns = get_feature_columns(embedding_size=embedding_size)
run_config = get_run_config()
estimator = tf.estimator.DNNClassifier(
hidden_units=hidden_units,
feature_columns=feature_columns["deep"],
model_dir=job_dir,
dropout=dropout,
config=run_config
)
# train spec
train_input_fn = get_input_fn(train_csv, batch_size=batch_size)
train_spec = get_train_spec(train_input_fn, train_steps)
# eval spec
eval_input_fn = get_input_fn(test_csv, tf.estimator.ModeKeys.EVAL, batch_size=batch_size)
exporter = get_exporter(serving_input_fn)
eval_spec = get_eval_spec(eval_input_fn, exporter)
# train and evaluate
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--train-csv", default="data/ml-100k/train.csv",
help="path to the training csv data (default: %(default)s)")
parser.add_argument("--test-csv", default="data/ml-100k/test.csv",
help="path to the test csv data (default: %(default)s)")
parser.add_argument("--job-dir", default="checkpoints/deep",
help="job directory (default: %(default)s)")
parser.add_argument("--restore", action="store_true",
help="whether to restore from job_dir")
parser.add_argument("--embedding-size", type=int, default=4,
help="embedding size (default: %(default)s)")
parser.add_argument("--hidden-units", type=int, nargs='+', default=[16, 16],
help="hidden layer specification (default: %(default)s)")
parser.add_argument("--dropout", type=float, default=0.1,
help="dropout rate (default: %(default)s)")
parser.add_argument("--batch-size", type=int, default=32,
help="batch size (default: %(default)s)")
parser.add_argument("--train-steps", type=int, default=20000,
help="number of training steps (default: %(default)s)")
args = parser.parse_args()
train_and_evaluate(args)