The `model_fn` for TPUEstimator.
(features, labels, mode, params)
| 241 | """Returns `model_fn` closure for TPUEstimator.""" |
| 242 | |
| 243 | def model_fn(features, labels, mode, params): # pylint: disable=unused-argument |
| 244 | """The `model_fn` for TPUEstimator.""" |
| 245 | |
| 246 | tf.logging.info("*** Features ***") |
| 247 | for name in sorted(features.keys()): |
| 248 | tf.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) |
| 249 | |
| 250 | input_ids = features["input_ids"] |
| 251 | input_mask = features["input_mask"] |
| 252 | segment_ids = features["segment_ids"] |
| 253 | label_ids = features["label_ids"] |
| 254 | is_real_example = None |
| 255 | if "is_real_example" in features: |
| 256 | is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32) |
| 257 | else: |
| 258 | is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32) |
| 259 | |
| 260 | is_training = (mode == tf_estimator.ModeKeys.TRAIN) |
| 261 | |
| 262 | (total_loss, per_example_loss, logits, |
| 263 | probabilities) = create_model(bert_config, is_training, input_ids, |
| 264 | input_mask, segment_ids, label_ids, |
| 265 | use_one_hot_embeddings) |
| 266 | |
| 267 | tvars = tf.trainable_variables() |
| 268 | initialized_variable_names = {} |
| 269 | scaffold_fn = None |
| 270 | if init_checkpoint: |
| 271 | (assignment_map, initialized_variable_names |
| 272 | ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) |
| 273 | if use_tpu: |
| 274 | |
| 275 | def tpu_scaffold(): |
| 276 | tf.train.init_from_checkpoint(init_checkpoint, assignment_map) |
| 277 | return tf.train.Scaffold() |
| 278 | |
| 279 | scaffold_fn = tpu_scaffold |
| 280 | else: |
| 281 | tf.train.init_from_checkpoint(init_checkpoint, assignment_map) |
| 282 | |
| 283 | tf.logging.info("**** Trainable Variables ****") |
| 284 | for var in tvars: |
| 285 | init_string = "" |
| 286 | if var.name in initialized_variable_names: |
| 287 | init_string = ", *INIT_FROM_CKPT*" |
| 288 | tf.logging.info(" name = %s, shape = %s%s", var.name, var.shape, |
| 289 | init_string) |
| 290 | |
| 291 | output_spec = None |
| 292 | if mode == tf_estimator.ModeKeys.TRAIN: |
| 293 | |
| 294 | train_op = optimization.create_optimizer(total_loss, learning_rate, |
| 295 | num_train_steps, |
| 296 | num_warmup_steps, use_tpu) |
| 297 | |
| 298 | output_spec = contrib_tpu.TPUEstimatorSpec( |
| 299 | mode=mode, |
| 300 | loss=total_loss, |
nothing calls this directly
no test coverage detected
searching dependent graphs…