The BERT Paper


Section 4.2 covers the SQuAD training.

So from my understanding, there are two extra parameters trained, they are two vectors with the same dimension as the hidden size, so the same dimensions as the contextualized embeddings in BERT. They are S (for start) and E (for End).

For each, a softmax is taken with S and each of the final contextualized embeddings to get a score for the correct Start position. And the same thing is done for E and the correct end position.

I get up to this part. But I am having trouble figuring out how the did the labeling and final loss calculations, which is described in this paragraph

"and the maximum scoring span is used as the prediction. The training objective is the loglikelihood of the correct start and end positions."

What do they mean by "maximum scoring span is used as the prediction"?

Furthermore, how does that play into "The training objective is the loglikelihood of the correct start and end positions"?

From this Source:


It says the log-likelihood is only applied to the correct classes. So the we are only calculating the softmax for the correct positions only, Not any of the in correct positions.

If this interpretation is correct, then the loss will be

Loss = -Log( Softmax(S*T(predictedStart) / Sum(S*Ti) ) -Log( Softmax(E*T(predictedEnd) / Sum(S*Ti) )

These answers are based on my personal understanding of Bert from both the paper and official_implementation, hope it will help:

What do they mean by "maximum scoring span is used as the prediction"?

As you know in SQuAD the input sequence is divided to 2 parts: Question and Document (from which we extract the answer if possible).

Sometimes the input length exceeds the max_seq_length parameter, in this case the document is truncated to as many parts as needed and we end up having more than one input for the same question/document. Mention that the question is replicated in all the resulting inputs (see line_350 for details).

So in such cases, in order to determine the predicted span among all the generated-inputs we use the maximum scoring i.e (max_start + max_end) / 2 = ( max(Softmax(S*Ti)) + max(Softmax(E*Ti)) ) / 2 of all the inputs related to the same question.

"The training objective is the loglikelihood of the correct start and end positions"

The loss is the average of the start_position loss start_loss and the end_postition loss end_loss. Each loss is computed in the same way: after applying the Softmax to the final output (i.e S*Ti or E*Ti) we use the real start/end postitions to compute the loss (see code below).

From run_squad.py in Bert_repo:

 def compute_loss(logits, positions):
        one_hot_positions = tf.one_hot(positions, depth=seq_length, dtype=tf.float32)
        log_probs = tf.nn.log_softmax(logits, axis=-1)
        loss = -tf.reduce_mean(tf.reduce_sum(one_hot_positions * log_probs, axis=-1))
        return loss

 start_positions = features["start_positions"]
 end_positions = features["end_positions"]

 start_loss = compute_loss(start_logits, start_positions)
 end_loss = compute_loss(end_logits, end_positions)

 total_loss = (start_loss + end_loss) / 2.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.