(is_training, input_ids, input_mask, segment_ids, labels,
                 num_labels, bert_hub_module_handle)
Creates a classification model.
Creates a classification model.
Creates a classification model .
def create_model(is_training, input_ids, input_mask, segment_ids, labels,
                 num_labels, bert_hub_module_handle):
  """Creates a classification model."""
  tags = set()
  if is_training:
    tags.add("train")
  bert_module = hub.Module(bert_hub_module_handle, tags=tags, trainable=True)
  bert_inputs = dict(
      input_ids=input_ids,
      input_mask=input_mask,
      segment_ids=segment_ids)
  bert_outputs = bert_module(
      inputs=bert_inputs,
      signature="tokens",
      as_dict=True)

  # In the demo, we are doing a simple classification task on the entire
  # segment.
  #
  # If you want to use the token-level output, use
  # bert_outputs["sequence_output"] instead.
  output_layer = bert_outputs["pooled_output"]

  hidden_size = output_layer.shape[-1].value

  output_weights = tf.get_variable(
      "output_weights", [num_labels, hidden_size],
      initializer=tf.truncated_normal_initializer(stddev=0.02))

  output_bias = tf.get_variable(
      "output_bias", [num_labels], initializer=tf.zeros_initializer())

  with tf.variable_scope("loss"):
    if is_training:
      # I.e., 0.1 dropout
      output_layer = tf.nn.dropout(output_layer, keep_prob=0.9)

    logits = tf.matmul(output_layer, output_weights, transpose_b=True)
    logits = tf.nn.bias_add(logits, output_bias)
    probabilities = tf.nn.softmax(logits, axis=-1)
    log_probs = tf.nn.log_softmax(logits, axis=-1)

    one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)

    per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
    loss = tf.reduce_mean(per_example_loss)

    return (loss, per_example_loss, logits, probabilities)
def create_model ( is_training , input_ids , input_mask , segment_ids , labels , num_labels , bert_hub_module_handle ) : tags = set ( ) if is_training : tags . add ( "train" ) bert_module = hub . Module ( bert_hub_module_handle , tags = tags , trainable = True ) bert_inputs = dict ( input_ids = input_ids , input_mask = input_mask , segment_ids = segment_ids ) bert_outputs = bert_module ( inputs = bert_inputs , signature = "tokens" , as_dict = True ) # In the demo, we are doing a simple classification task on the entire # segment. # # If you want to use the token-level output, use # bert_outputs["sequence_output"] instead. output_layer = bert_outputs [ "pooled_output" ] hidden_size = output_layer . shape [ - 1 ] . value output_weights = tf . get_variable ( "output_weights" , [ num_labels , hidden_size ] , initializer = tf . truncated_normal_initializer ( stddev = 0.02 ) ) output_bias = tf . get_variable ( "output_bias" , [ num_labels ] , initializer = tf . zeros_initializer ( ) ) with tf . variable_scope ( "loss" ) : if is_training : # I.e., 0.1 dropout output_layer = tf . nn . dropout ( output_layer , keep_prob = 0.9 ) logits = tf . matmul ( output_layer , output_weights , transpose_b = True ) logits = tf . nn . bias_add ( logits , output_bias ) probabilities = tf . nn . softmax ( logits , axis = - 1 ) log_probs = tf . nn . log_softmax ( logits , axis = - 1 ) one_hot_labels = tf . one_hot ( labels , depth = num_labels , dtype = tf . float32 ) per_example_loss = - tf . reduce_sum ( one_hot_labels * log_probs , axis = - 1 ) loss = tf . reduce_mean ( per_example_loss ) return ( loss , per_example_loss , logits , probabilities )