Creates a classification model.
(bert_config, is_training, input_ids, input_mask, segment_ids,
labels, num_labels, use_one_hot_embeddings)
| 378 | |
| 379 | |
| 380 | def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, |
| 381 | labels, num_labels, use_one_hot_embeddings): |
| 382 | """Creates a classification model.""" |
| 383 | model = modeling.BertModel( |
| 384 | config=bert_config, |
| 385 | is_training=is_training, |
| 386 | input_ids=input_ids, |
| 387 | input_mask=input_mask, |
| 388 | token_type_ids=segment_ids, |
| 389 | use_one_hot_embeddings=use_one_hot_embeddings) |
| 390 | |
| 391 | # In the demo, we are doing a simple classification task on the entire |
| 392 | # segment. |
| 393 | # |
| 394 | # If you want to use the token-level output, use model.get_sequence_output() |
| 395 | # instead. |
| 396 | output_layer = model.get_pooled_output() |
| 397 | |
| 398 | hidden_size = output_layer.shape[-1].value |
| 399 | |
| 400 | output_weights = tf.get_variable( |
| 401 | "output_weights", [num_labels, hidden_size], |
| 402 | initializer=tf.truncated_normal_initializer(stddev=0.02)) |
| 403 | |
| 404 | output_bias = tf.get_variable( |
| 405 | "output_bias", [num_labels], initializer=tf.zeros_initializer()) |
| 406 | |
| 407 | with tf.variable_scope("loss"): |
| 408 | if is_training: |
| 409 | # I.e., 0.1 dropout |
| 410 | output_layer = tf.nn.dropout(output_layer, keep_prob=0.9) |
| 411 | |
| 412 | logits = tf.matmul(output_layer, output_weights, transpose_b=True) |
| 413 | logits = tf.nn.bias_add(logits, output_bias) |
| 414 | probabilities = tf.nn.softmax(logits, axis=-1) |
| 415 | log_probs = tf.nn.log_softmax(logits, axis=-1) |
| 416 | |
| 417 | one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32) |
| 418 | |
| 419 | per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) |
| 420 | loss = tf.reduce_mean(per_example_loss) |
| 421 | |
| 422 | return (loss, per_example_loss, logits, probabilities) |
| 423 | |
| 424 | |
| 425 | def model_fn_builder(bert_config, num_labels, init_checkpoint, learning_rate, |
no test coverage detected
searching dependent graphs…