ncf_keras_main.py 19.4 KB
Newer Older
Frederick Liu's avatar
Frederick Liu committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Shining Sun's avatar
Shining Sun committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Frederick Liu's avatar
Frederick Liu committed
14

Shining Sun's avatar
Shining Sun committed
15
16
17
18
19
20
"""NCF framework to train and evaluate the NeuMF model.

The NeuMF model assembles both MF and MLP models under the NCF framework. Check
`neumf_model.py` for more details about the models.
"""

21
import json
Shining Sun's avatar
Shining Sun committed
22
23
24
import os

# pylint: disable=g-bad-import-order
Hongkun Yu's avatar
Hongkun Yu committed
25

David Chen's avatar
David Chen committed
26
from absl import app
Shining Sun's avatar
Shining Sun committed
27
from absl import flags
28
from absl import logging
Hongkun Yu's avatar
Hongkun Yu committed
29
import tensorflow as tf
Shining Sun's avatar
Shining Sun committed
30
31
# pylint: enable=g-bad-import-order

32
from official.common import distribute_utils
33
from official.recommendation import constants as rconst
34
from official.recommendation import movielens
Shining Sun's avatar
Shining Sun committed
35
from official.recommendation import ncf_common
36
from official.recommendation import ncf_input_pipeline
Shining Sun's avatar
Shining Sun committed
37
from official.recommendation import neumf_model
38
from official.utils.flags import core as flags_core
39
from official.utils.misc import keras_utils
Shining Sun's avatar
Shining Sun committed
40
from official.utils.misc import model_helpers
41

Shining Sun's avatar
Shining Sun committed
42
43
44
FLAGS = flags.FLAGS


45
def metric_fn(logits, dup_mask, match_mlperf):
guptapriya's avatar
guptapriya committed
46
  dup_mask = tf.cast(dup_mask, tf.float32)
47
  logits = tf.slice(logits, [0, 1], [-1, -1])
guptapriya's avatar
guptapriya committed
48
  in_top_k, _, metric_weights, _ = neumf_model.compute_top_k_and_ndcg(
Hongkun Yu's avatar
Hongkun Yu committed
49
      logits, dup_mask, match_mlperf)
guptapriya's avatar
guptapriya committed
50
51
52
53
  metric_weights = tf.cast(metric_weights, tf.float32)
  return in_top_k, metric_weights


54
55
56
class MetricLayer(tf.keras.layers.Layer):
  """Custom layer of metrics for NCF model."""

57
  def __init__(self, match_mlperf):
58
    super(MetricLayer, self).__init__()
59
60
61
62
63
64
65
66
    self.match_mlperf = match_mlperf

  def get_config(self):
    return {"match_mlperf": self.match_mlperf}

  @classmethod
  def from_config(cls, config, custom_objects=None):
    return cls(**config)
guptapriya's avatar
guptapriya committed
67

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
68
  def call(self, inputs, training=False):
69
    logits, dup_mask = inputs
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
70
71
72
73
74

    if training:
      hr_sum = 0.0
      hr_count = 0.0
    else:
75
      metric, metric_weights = metric_fn(logits, dup_mask, self.match_mlperf)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
76
77
78
79
80
      hr_sum = tf.reduce_sum(metric * metric_weights)
      hr_count = tf.reduce_sum(metric_weights)

    self.add_metric(hr_sum, name="hr_sum", aggregation="mean")
    self.add_metric(hr_count, name="hr_count", aggregation="mean")
guptapriya's avatar
guptapriya committed
81
    return logits
82
83


84
85
86
87
class LossLayer(tf.keras.layers.Layer):
  """Pass-through loss layer for NCF model."""

  def __init__(self, loss_normalization_factor):
88
89
    # The loss may overflow in float16, so we use float32 instead.
    super(LossLayer, self).__init__(dtype="float32")
90
91
92
93
    self.loss_normalization_factor = loss_normalization_factor
    self.loss = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=True, reduction="sum")

94
95
96
97
98
99
100
  def get_config(self):
    return {"loss_normalization_factor": self.loss_normalization_factor}

  @classmethod
  def from_config(cls, config, custom_objects=None):
    return cls(**config)

101
102
103
104
105
106
107
108
109
  def call(self, inputs):
    logits, labels, valid_pt_mask_input = inputs
    loss = self.loss(
        y_true=labels, y_pred=logits, sample_weight=valid_pt_mask_input)
    loss = loss * (1.0 / self.loss_normalization_factor)
    self.add_loss(loss)
    return logits


Shining Sun's avatar
Shining Sun committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class IncrementEpochCallback(tf.keras.callbacks.Callback):
  """A callback to increase the requested epoch for the data producer.

  The reason why we need this is because we can only buffer a limited amount of
  data. So we keep a moving window to represent the buffer. This is to move the
  one of the window's boundaries for each epoch.
  """

  def __init__(self, producer):
    self._producer = producer

  def on_epoch_begin(self, epoch, logs=None):
    self._producer.increment_request_epoch()


125
126
127
128
129
130
131
132
class CustomEarlyStopping(tf.keras.callbacks.Callback):
  """Stop training has reached a desired hit rate."""

  def __init__(self, monitor, desired_value):
    super(CustomEarlyStopping, self).__init__()

    self.monitor = monitor
    self.desired = desired_value
133
    self.stopped_epoch = 0
134
135
136
137
138
139
140
141
142

  def on_epoch_end(self, epoch, logs=None):
    current = self.get_monitor_value(logs)
    if current and current >= self.desired:
      self.stopped_epoch = epoch
      self.model.stop_training = True

  def on_train_end(self, logs=None):
    if self.stopped_epoch > 0:
Haoyu Zhang's avatar
Haoyu Zhang committed
143
      print("Epoch %05d: early stopping" % (self.stopped_epoch + 1))
144
145
146
147
148

  def get_monitor_value(self, logs):
    logs = logs or {}
    monitor_value = logs.get(self.monitor)
    if monitor_value is None:
Hongkun Yu's avatar
Hongkun Yu committed
149
150
151
152
      logging.warning(
          "Early stopping conditioned on metric `%s` "
          "which is not available. Available metrics are: %s", self.monitor,
          ",".join(list(logs.keys())))
153
154
155
    return monitor_value


Shining Sun's avatar
Shining Sun committed
156
157
def _get_keras_model(params):
  """Constructs and returns the model."""
Haoyu Zhang's avatar
Haoyu Zhang committed
158
  batch_size = params["batch_size"]
Shining Sun's avatar
Shining Sun committed
159
160

  user_input = tf.keras.layers.Input(
161
      shape=(1,), name=movielens.USER_COLUMN, dtype=tf.int32)
Shining Sun's avatar
Shining Sun committed
162
163

  item_input = tf.keras.layers.Input(
164
      shape=(1,), name=movielens.ITEM_COLUMN, dtype=tf.int32)
guptapriya's avatar
guptapriya committed
165

166
  valid_pt_mask_input = tf.keras.layers.Input(
167
      shape=(1,), name=rconst.VALID_POINT_MASK, dtype=tf.bool)
168
169

  dup_mask_input = tf.keras.layers.Input(
170
      shape=(1,), name=rconst.DUPLICATE_MASK, dtype=tf.int32)
171
172

  label_input = tf.keras.layers.Input(
173
      shape=(1,), name=rconst.TRAIN_LABEL_KEY, dtype=tf.bool)
Shining Sun's avatar
Shining Sun committed
174

175
  base_model = neumf_model.construct_model(user_input, item_input, params)
Shining Sun's avatar
Shining Sun committed
176

177
  logits = base_model.output
178

Hongkun Yu's avatar
Hongkun Yu committed
179
  zeros = tf.keras.layers.Lambda(lambda x: x * 0)(logits)
Shining Sun's avatar
Shining Sun committed
180

Hongkun Yu's avatar
Hongkun Yu committed
181
  softmax_logits = tf.keras.layers.concatenate([zeros, logits], axis=-1)
Shining Sun's avatar
Shining Sun committed
182

183
184
  # Custom training loop calculates loss and metric as a part of
  # training/evaluation step function.
185
  if not params["keras_use_ctl"]:
Chen Chen's avatar
Chen Chen committed
186
187
    softmax_logits = MetricLayer(
        params["match_mlperf"])([softmax_logits, dup_mask_input])
188
189
190
191
    # TODO(b/134744680): Use model.add_loss() instead once the API is well
    # supported.
    softmax_logits = LossLayer(batch_size)(
        [softmax_logits, label_input, valid_pt_mask_input])
192

Shining Sun's avatar
Shining Sun committed
193
  keras_model = tf.keras.Model(
guptapriya's avatar
guptapriya committed
194
195
196
197
198
      inputs={
          movielens.USER_COLUMN: user_input,
          movielens.ITEM_COLUMN: item_input,
          rconst.VALID_POINT_MASK: valid_pt_mask_input,
          rconst.DUPLICATE_MASK: dup_mask_input,
Hongkun Yu's avatar
Hongkun Yu committed
199
200
          rconst.TRAIN_LABEL_KEY: label_input
      },
Shining Sun's avatar
Shining Sun committed
201
202
203
204
205
206
207
      outputs=softmax_logits)

  keras_model.summary()
  return keras_model


def run_ncf(_):
208
209
  """Run NCF training and eval with Keras."""

210
211
  keras_utils.set_session_config(enable_xla=FLAGS.enable_xla)

guptapriya's avatar
guptapriya committed
212
213
214
  if FLAGS.seed is not None:
    print("Setting tf seed")
    tf.random.set_seed(FLAGS.seed)
215

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
216
  model_helpers.apply_clean(FLAGS)
Shining Sun's avatar
Shining Sun committed
217

218
  if FLAGS.dtype == "fp16" and FLAGS.fp16_implementation == "keras":
Reed Wanderman-Milne's avatar
Reed Wanderman-Milne committed
219
    tf.keras.mixed_precision.set_global_policy("mixed_float16")
220

221
  strategy = distribute_utils.get_distribution_strategy(
222
      distribution_strategy=FLAGS.distribution_strategy,
223
224
      num_gpus=FLAGS.num_gpus,
      tpu_address=FLAGS.tpu)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
225
226

  params = ncf_common.parse_flags(FLAGS)
227
  params["distribute_strategy"] = strategy
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
228
  params["use_tpu"] = (FLAGS.distribution_strategy == "tpu")
229

230
231
232
  if params["use_tpu"] and not params["keras_use_ctl"]:
    logging.error("Custom training loop must be used when using TPUStrategy.")
    return
233

234
  batch_size = params["batch_size"]
235
236
237
238
239
240
241
242
  time_callback = keras_utils.TimeHistory(batch_size, FLAGS.log_steps)
  callbacks = [time_callback]

  producer, input_meta_data = None, None
  generate_input_online = params["train_dataset_path"] is None

  if generate_input_online:
    # Start data producing thread.
243
    num_users, num_items, _, _, producer = ncf_common.get_inputs(params)
244
245
246
247
248
    producer.start()
    per_epoch_callback = IncrementEpochCallback(producer)
    callbacks.append(per_epoch_callback)
  else:
    assert params["eval_dataset_path"] and params["input_meta_data_path"]
249
    with tf.io.gfile.GFile(params["input_meta_data_path"], "rb") as reader:
250
251
252
      input_meta_data = json.loads(reader.read().decode("utf-8"))
      num_users = input_meta_data["num_users"]
      num_items = input_meta_data["num_items"]
Shining Sun's avatar
Shining Sun committed
253
254

  params["num_users"], params["num_items"] = num_users, num_items
255
256
257

  if FLAGS.early_stopping:
    early_stopping_callback = CustomEarlyStopping(
guptapriya's avatar
guptapriya committed
258
        "val_HR_METRIC", desired_value=FLAGS.hr_threshold)
259
    callbacks.append(early_stopping_callback)
260

261
262
263
  (train_input_dataset, eval_input_dataset, num_train_steps,
   num_eval_steps) = ncf_input_pipeline.create_ncf_input_data(
       params, producer, input_meta_data, strategy)
264
265
  steps_per_epoch = None if generate_input_online else num_train_steps

266
  with distribute_utils.get_strategy_scope(strategy):
267
268
269
270
271
272
    keras_model = _get_keras_model(params)
    optimizer = tf.keras.optimizers.Adam(
        learning_rate=params["learning_rate"],
        beta_1=params["beta1"],
        beta_2=params["beta2"],
        epsilon=params["epsilon"])
273
    if FLAGS.fp16_implementation == "graph_rewrite":
274
275
      optimizer = \
        tf.compat.v1.train.experimental.enable_mixed_precision_graph_rewrite(
276
            optimizer,
277
278
            loss_scale=flags_core.get_loss_scale(FLAGS,
                                                 default_for_fp16="dynamic"))
Reed Wanderman-Milne's avatar
Reed Wanderman-Milne committed
279
280
281
282
283
284
285
286
287
288
289
    elif FLAGS.dtype == "fp16":
      loss_scale = flags_core.get_loss_scale(FLAGS, default_for_fp16="dynamic")
      # Note Model.compile automatically wraps the optimizer with a
      # LossScaleOptimizer using dynamic loss scaling. We explicitly wrap it
      # here for the case where a custom training loop or fixed loss scale is
      # used.
      if loss_scale == "dynamic":
        optimizer = tf.keras.mixed_precision.LossScaleOptimizer(optimizer)
      else:
        optimizer = tf.keras.mixed_precision.LossScaleOptimizer(
            optimizer, dynamic=False, initial_scale=loss_scale)
290
291
292
293
294
295
296
297
298
299
300
301
302
303

    if params["keras_use_ctl"]:
      train_loss, eval_results = run_ncf_custom_training(
          params,
          strategy,
          keras_model,
          optimizer,
          callbacks,
          train_input_dataset,
          eval_input_dataset,
          num_train_steps,
          num_eval_steps,
          generate_input_online=generate_input_online)
    else:
304
      keras_model.compile(optimizer=optimizer, run_eagerly=FLAGS.run_eagerly)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
305
306
307
308

      if not FLAGS.ml_perf:
        # Create Tensorboard summary and checkpoint callbacks.
        summary_dir = os.path.join(FLAGS.model_dir, "summaries")
309
310
        summary_callback = tf.keras.callbacks.TensorBoard(
            summary_dir, profile_batch=0)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
311
312
313
314
315
        checkpoint_path = os.path.join(FLAGS.model_dir, "checkpoint")
        checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
            checkpoint_path, save_weights_only=True)

        callbacks += [summary_callback, checkpoint_callback]
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

      history = keras_model.fit(
          train_input_dataset,
          epochs=FLAGS.train_epochs,
          steps_per_epoch=steps_per_epoch,
          callbacks=callbacks,
          validation_data=eval_input_dataset,
          validation_steps=num_eval_steps,
          verbose=2)

      logging.info("Training done. Start evaluating")

      eval_loss_and_metrics = keras_model.evaluate(
          eval_input_dataset, steps=num_eval_steps, verbose=2)

      logging.info("Keras evaluation is done.")

      # Keras evaluate() API returns scalar loss and metric values from
      # evaluation as a list. Here, the returned list would contain
      # [evaluation loss, hr sum, hr count].
      eval_hit_rate = eval_loss_and_metrics[1] / eval_loss_and_metrics[2]

      # Format evaluation result into [eval loss, eval hit accuracy].
      eval_results = [eval_loss_and_metrics[0], eval_hit_rate]

      if history and history.history:
        train_history = history.history
        train_loss = train_history["loss"][-1]

  stats = build_stats(train_loss, eval_results, time_callback)
  return stats
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381


def run_ncf_custom_training(params,
                            strategy,
                            keras_model,
                            optimizer,
                            callbacks,
                            train_input_dataset,
                            eval_input_dataset,
                            num_train_steps,
                            num_eval_steps,
                            generate_input_online=True):
  """Runs custom training loop.

  Args:
    params: Dictionary containing training parameters.
    strategy: Distribution strategy to be used for distributed training.
    keras_model: Model used for training.
    optimizer: Optimizer used for training.
    callbacks: Callbacks to be invoked between batches/epochs.
    train_input_dataset: tf.data.Dataset used for training.
    eval_input_dataset: tf.data.Dataset used for evaluation.
    num_train_steps: Total number of steps to run for training.
    num_eval_steps: Total number of steps to run for evaluation.
    generate_input_online: Whether input data was generated by data producer.
      When data is generated by data producer, then train dataset must be
      re-initialized after every epoch.

  Returns:
    A tuple of train loss and a list of training and evaluation results.
  """
  loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
      reduction="sum", from_logits=True)
  train_input_iterator = iter(
      strategy.experimental_distribute_dataset(train_input_dataset))
382

383
384
  def train_step(train_iterator):
    """Called once per step to train the model."""
385

386
387
388
389
    def step_fn(features):
      """Computes loss and applied gradient per replica."""
      with tf.GradientTape() as tape:
        softmax_logits = keras_model(features)
390
391
        # The loss can overflow in float16, so we cast to float32.
        softmax_logits = tf.cast(softmax_logits, "float32")
392
393
394
395
396
397
        labels = features[rconst.TRAIN_LABEL_KEY]
        loss = loss_object(
            labels,
            softmax_logits,
            sample_weight=features[rconst.VALID_POINT_MASK])
        loss *= (1.0 / params["batch_size"])
Nimit Nigania's avatar
Nimit Nigania committed
398
399
        if FLAGS.dtype == "fp16":
          loss = optimizer.get_scaled_loss(loss)
400
401

      grads = tape.gradient(loss, keras_model.trainable_variables)
Nimit Nigania's avatar
Nimit Nigania committed
402
403
      if FLAGS.dtype == "fp16":
        grads = optimizer.get_unscaled_gradients(grads)
404
405
406
407
408
409
      # Converting gradients to dense form helps in perf on GPU for NCF
      grads = neumf_model.sparse_to_dense_grads(
          list(zip(grads, keras_model.trainable_variables)))
      optimizer.apply_gradients(grads)
      return loss

Hongkun Yu's avatar
Hongkun Yu committed
410
    per_replica_losses = strategy.run(step_fn, args=(next(train_iterator),))
411
412
413
414
415
416
417
418
419
420
421
422
    mean_loss = strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)
    return mean_loss

  def eval_step(eval_iterator):
    """Called once per eval step to compute eval metrics."""

    def step_fn(features):
      """Computes eval metrics per replica."""
      softmax_logits = keras_model(features)
      in_top_k, metric_weights = metric_fn(softmax_logits,
                                           features[rconst.DUPLICATE_MASK],
423
                                           params["match_mlperf"])
424
425
426
      hr_sum = tf.reduce_sum(in_top_k * metric_weights)
      hr_count = tf.reduce_sum(metric_weights)
      return hr_sum, hr_count
427

428
    per_replica_hr_sum, per_replica_hr_count = (
Hongkun Yu's avatar
Hongkun Yu committed
429
        strategy.run(step_fn, args=(next(eval_iterator),)))
430
431
432
433
434
    hr_sum = strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_hr_sum, axis=None)
    hr_count = strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_hr_count, axis=None)
    return hr_sum, hr_count
435

436
437
438
  if not FLAGS.run_eagerly:
    train_step = tf.function(train_step)
    eval_step = tf.function(eval_step)
439

440
441
  for callback in callbacks:
    callback.on_train_begin()
442

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
443
444
445
446
447
448
449
450
451
452
  # Not writing tensorboard summaries if running in MLPerf.
  if FLAGS.ml_perf:
    eval_summary_writer, train_summary_writer = None, None
  else:
    summary_dir = os.path.join(FLAGS.model_dir, "summaries")
    eval_summary_writer = tf.summary.create_file_writer(
        os.path.join(summary_dir, "eval"))
    train_summary_writer = tf.summary.create_file_writer(
        os.path.join(summary_dir, "train"))

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  train_loss = 0
  for epoch in range(FLAGS.train_epochs):
    for cb in callbacks:
      cb.on_epoch_begin(epoch)

    # As NCF dataset is sampled with randomness, not repeating
    # data elements in each epoch has significant impact on
    # convergence. As so, offline-generated TF record files
    # contains all epoch worth of data. Thus we do not need
    # to initialize dataset when reading from tf record files.
    if generate_input_online:
      train_input_iterator = iter(
          strategy.experimental_distribute_dataset(train_input_dataset))

    train_loss = 0
    for step in range(num_train_steps):
      current_step = step + epoch * num_train_steps
      for c in callbacks:
        c.on_batch_begin(current_step)

      train_loss += train_step(train_input_iterator)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
475
476
      # Write train loss once in every 1000 steps.
      if train_summary_writer and step % 1000 == 0:
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
477
        with train_summary_writer.as_default():
Hongkun Yu's avatar
Hongkun Yu committed
478
479
          tf.summary.scalar(
              "training_loss", train_loss / (step + 1), step=current_step)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
480

481
482
483
484
      for c in callbacks:
        c.on_batch_end(current_step)

    train_loss /= num_train_steps
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
485
    logging.info("Done training epoch %s, epoch loss=%.3f", epoch + 1,
486
487
                 train_loss)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
488
489
    eval_input_iterator = iter(
        strategy.experimental_distribute_dataset(eval_input_dataset))
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
490
491
492

    hr_sum = 0.0
    hr_count = 0.0
493
494
495
496
497
    for _ in range(num_eval_steps):
      step_hr_sum, step_hr_count = eval_step(eval_input_iterator)
      hr_sum += step_hr_sum
      hr_count += step_hr_count

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
498
    logging.info("Done eval epoch %s, hit_rate=%.3f", epoch + 1,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
499
500
501
502
                 hr_sum / hr_count)
    if eval_summary_writer:
      with eval_summary_writer.as_default():
        tf.summary.scalar("hit_rate", hr_sum / hr_count, step=current_step)
503
504
505
506
507
508
509
510

    if (FLAGS.early_stopping and
        float(hr_sum / hr_count) > params["hr_threshold"]):
      break

  for c in callbacks:
    c.on_train_end()

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
511
512
513
514
515
516
517
  # Saving the model at the end of training.
  if not FLAGS.ml_perf:
    checkpoint = tf.train.Checkpoint(model=keras_model, optimizer=optimizer)
    checkpoint_path = os.path.join(FLAGS.model_dir, "ctl_checkpoint")
    checkpoint.save(checkpoint_path)
    logging.info("Saving model as TF checkpoint: %s", checkpoint_path)

518
  return train_loss, [None, hr_sum / hr_count]
519
520


521
def build_stats(loss, eval_result, time_callback):
522
523
  """Normalizes and returns dictionary of stats.

Haoyu Zhang's avatar
Haoyu Zhang committed
524
525
526
527
528
529
530
531
  Args:
    loss: The final loss at training time.
    eval_result: Output of the eval step. Assumes first value is eval_loss and
      second value is accuracy_top_1.
    time_callback: Time tracking callback likely used during keras.fit.

  Returns:
    Dictionary of normalized results.
532
533
  """
  stats = {}
534
  if loss:
Haoyu Zhang's avatar
Haoyu Zhang committed
535
    stats["loss"] = loss
536
537

  if eval_result:
Haoyu Zhang's avatar
Haoyu Zhang committed
538
539
    stats["eval_loss"] = eval_result[0]
    stats["eval_hit_rate"] = eval_result[1]
540
541
542

  if time_callback:
    timestamp_log = time_callback.timestamp_log
Haoyu Zhang's avatar
Haoyu Zhang committed
543
544
    stats["step_timestamp_log"] = timestamp_log
    stats["train_finish_time"] = time_callback.train_finish_time
545
    if len(timestamp_log) > 1:
Haoyu Zhang's avatar
Haoyu Zhang committed
546
      stats["avg_exp_per_second"] = (
547
          time_callback.batch_size * time_callback.log_steps *
Hongkun Yu's avatar
Hongkun Yu committed
548
          (len(time_callback.timestamp_log) - 1) /
549
550
551
          (timestamp_log[-1].timestamp - timestamp_log[0].timestamp))

  return stats
Shining Sun's avatar
Shining Sun committed
552
553
554


def main(_):
555
  logging.info("Result is %s", run_ncf(FLAGS))
Shining Sun's avatar
Shining Sun committed
556
557
558
559


if __name__ == "__main__":
  ncf_common.define_ncf_flags()
David Chen's avatar
David Chen committed
560
  app.run(main)