Search code examples
pythontensorflowkerasclassificationbert-language-model

BERT get sentence level embedding after fine tuning


I came across this page

1) I would like to get sentence level embedding (embedding given by [CLS] token) after the fine tuning is done. How could I do it?

2) I also noticed that the code on that page takes a lot of time to return results on the test data. Why is that? When i trained the model it took less time as compared to when i tried to get test predictions. From the code on that page, I didnt use below blocks of the code

test_InputExamples = test.apply(lambda x: bert.run_classifier.InputExample(guid=None, 
                                                                       text_a = x[DATA_COLUMN], 
                                                                       text_b = None, 
                                                                       label = x[LABEL_COLUMN]), axis = 1

test_features = bert.run_classifier.convert_examples_to_features(test_InputExamples, label_list, MAX_SEQ_LENGTH, tokenizer)

test_input_fn = run_classifier.input_fn_builder(
        features=test_features,
        seq_length=MAX_SEQ_LENGTH,
        is_training=False,
        drop_remainder=False)

estimator.evaluate(input_fn=test_input_fn, steps=None)

Rather I just used below function on my entire test data

def getPrediction(in_sentences):
  labels = ["Negative", "Positive"]
  input_examples = [run_classifier.InputExample(guid="", text_a = x, text_b = None, label = 0) for x in in_sentences] # here, "" is just a dummy label
  input_features = run_classifier.convert_examples_to_features(input_examples, label_list, MAX_SEQ_LENGTH, tokenizer)
  predict_input_fn = run_classifier.input_fn_builder(features=input_features, seq_length=MAX_SEQ_LENGTH, is_training=False, drop_remainder=False)
  predictions = estimator.predict(predict_input_fn)
  return [(sentence, prediction['probabilities'], labels[prediction['labels']]) for sentence, prediction in zip(in_sentences, predictions)]

3) how could i get probability of prediction. is there a way to use keras predict method?

update1

question 2 update - could you test on 20000 training examples using getPrediction function?....it takes much longer time for me..even more than the time took to train model on 20000 examples.


Solution

  • 1) From BERT documentation

    The output dictionary contains:

    pooled_output: pooled output of the entire sequence with shape [batch_size, hidden_size]. sequence_output: representations of every token in the input sequence with shape [batch_size, max_sequence_length, hidden_size].

    I've added pooled_output vector which corresponds to the CLS vector.

    3) You receive log probabilities. Just apply softmax to get normal probabilities.

    Now all it is left to do is for model to report it. I have left the log probs, but they are not necessary anymore.

    See the code changes:

    def create_model(is_predicting, input_ids, input_mask, segment_ids, labels,
                     num_labels):
      """Creates a classification model."""
    
      bert_module = hub.Module(
          BERT_MODEL_HUB,
          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)
    
      # Use "pooled_output" for classification tasks on an entire sentence.
      # Use "sequence_outputs" for token-level output.
      output_layer = bert_outputs["pooled_output"]
    
      pooled_output = output_layer
    
      hidden_size = output_layer.shape[-1].value
    
      # Create our own layer to tune for politeness data.
      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"):
    
        # Dropout helps prevent overfitting
        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)
        log_probs = tf.nn.log_softmax(logits, axis=-1)
        probs = tf.nn.softmax(logits, axis=-1)
    
        # Convert labels into one-hot encoding
        one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)
    
        predicted_labels = tf.squeeze(tf.argmax(log_probs, axis=-1, output_type=tf.int32))
        # If we're predicting, we want predicted labels and the probabiltiies.
        if is_predicting:
          return (predicted_labels, log_probs, probs, pooled_output)
    
        # If we're train/eval, compute loss between predicted and actual label
        per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
        loss = tf.reduce_mean(per_example_loss)
        return (loss, predicted_labels, log_probs, probs, pooled_output)
    

    Now in the model_fn_builder() add support for those values:

      # this should be changed in both places
      (predicted_labels, log_probs, probs, pooled_output) = create_model(
        is_predicting, input_ids, input_mask, segment_ids, label_ids, num_labels)
    
      # return dictionary of all the values you wanted
      predictions = {
          'log_probabilities': log_probs,
          'probabilities': probs,
          'labels': predicted_labels,
          'pooled_output': pooled_output
      }
    

    Adjust getPrediction() accordingly and in the end your predictions will look like this:

    ('That movie was absolutely awful',
      array([0.99599314, 0.00400678], dtype=float32),  <= Probability
      array([-4.0148855e-03, -5.5197663e+00], dtype=float32), <= Log probability, same as previously
      'Negative', <= Label
      array([ 0.9181199 ,  0.7763732 ,  0.9999883 , -0.93533266, -0.9841384 ,
              0.78126144, -0.9918988 , -0.18764131,  0.9981035 ,  0.99999994,
              0.900716  , -0.99926263, -0.5078789 , -0.99417543, -0.07695035,
              0.9501321 ,  0.75836045,  0.49151263, -0.7886792 ,  0.97505844,
             -0.8931161 , -1.        ,  0.9318583 , -0.60531116, -0.8644371 ,
            ...
            and this is 768-d [CLS] vector (sentence embedding).    
    

    Regarding 2): At my end training took about 5 minutes and test about 40 seconds. Very reasonable.

    UPDATE

    For 20k samples it took 12:48 to train and 2:07 minutes to test.

    For 10k samples timings are 8:40 and 1:07 respectively.