Builds the `tf.estimator.EstimatorSpec` to train/eval with.
(features, labels, mode, params)
| 432 | # Need a named parameter `param` since this will be called |
| 433 | # with named arguments, so pylint: disable=unused-argument |
| 434 | def model_function(features, labels, mode, params): |
| 435 | """Builds the `tf.estimator.EstimatorSpec` to train/eval with.""" |
| 436 | is_train = mode == tf_estimator.ModeKeys.TRAIN |
| 437 | logits = predict(is_train, embeddings, features["premise"], |
| 438 | features["hypothesis"]) |
| 439 | |
| 440 | loss = tf.nn.sparse_softmax_cross_entropy_with_logits( |
| 441 | labels=tf.to_int32(labels), logits=logits) |
| 442 | loss = tf.reduce_mean(loss) |
| 443 | if mode == tf_estimator.ModeKeys.TRAIN: |
| 444 | train_op = get_train_op(loss) |
| 445 | else: |
| 446 | # Don't build the train_op unnecessarily, since the ADAM variables can |
| 447 | # cause problems with loading checkpoints on CPUs. |
| 448 | train_op = None |
| 449 | metrics = dict( |
| 450 | accuracy=tf.metrics.accuracy( |
| 451 | tf.argmax(logits, 1, output_type=tf.int32), tf.to_int32(labels))) |
| 452 | |
| 453 | checkpoint_file = FLAGS.checkpoint_file |
| 454 | if checkpoint_file is None: |
| 455 | scaffold = None |
| 456 | else: |
| 457 | saver = tf.train.Saver(tf.trainable_variables()) |
| 458 | |
| 459 | def _init_fn(_, sess): |
| 460 | saver.restore(sess, checkpoint_file) |
| 461 | |
| 462 | scaffold = tf.train.Scaffold(init_fn=_init_fn) |
| 463 | |
| 464 | return tf_estimator.EstimatorSpec( |
| 465 | mode=mode, |
| 466 | scaffold=scaffold, |
| 467 | loss=loss, |
| 468 | predictions=None, |
| 469 | train_op=train_op, |
| 470 | eval_metric_ops=metrics) |
| 471 | |
| 472 | def compare_fn(best_eval_result, current_eval_result): |
| 473 | return best_eval_result["accuracy"] < current_eval_result["accuracy"] |
nothing calls this directly
no test coverage detected
searching dependent graphs…