Feature extraction for sequential labelling
Hi, I have a question in terms of using BERT for sequential labeling task. Please correct me if I'm wrong. My understanding is:
- Use BertModel loaded with pretrained weights instead of MaskedBertModel.
- In such case, take a sequence of tokens as input, BertModel would output a list of hidden states, I only use the top layer hidden states as the embedding for that sequence.
- Then to fine tune the model, add a linear fully connected layer and softmax to make final decision.
Is this entire process correct? I followed this procedure but could not have any results.
Thank you!
Well that seems like a good approach. Maybe you can find some inspiration in the code of the BertForQuestionAnswering model? It is not exactly what you are doing but maybe it can help.
Thanks. It worked. However, a interesting issue about BERT is that it's highly sensitive to learning rate, which makes it very difficult to combine with other models
@zhaoxy92 what sequence labeling task are you doing? I've got CoNLL'03 NER running with the bert-base-cased model, and also found the same sensitivity to hyper-parameters.
The best dev F1 score i've gotten after ~~half a day~~ a day of trying some parameters is ~~92.4~~ 94.6, which is a bit lower than the 96.4 dev score for BERT_base reported in the paper. I guess more tuning will increase the score some more.
The best configuration for me so far is:
- Batch size: 160 (on four P40 GPUs with 24GB RAM each). Smaller batch sizes that fit on one or two GPUs give bad results.
- Optimizer: Adam with learning rate 1e-4. Tried BertAdam with learning rate 1e-5, but it didn't seem to converge.
- fp16/fp32: Only fp32 works. Tried fp16 (half precision) to allow larger batch sizes, but this gave really low scores, with and without loss scaling.
Also, properly averaging the loss is important: Not just loss /= batch_size. You need to take into account padding and word pieces without predictions (https://github.com/google-research/bert/issues/33#issuecomment-436726952). If you have a mask tensor that indicates which bert inputs correspond to tagged tokens, then the proper averaging is loss /= mask.float().sum
Another tip, truncating the input (https://github.com/huggingface/pytorch-pretrained-BERT/pull/66) enables much larger batch sizes. Without it the largest possible batch size was 56, but with truncating 160 is possible.
I am also working on CoNLL03. Similar results as you got.
@bheinzerling with the risk of going off topic here, would you mind sharing your code? I'd love to read and adapt it for a similar sequential classification task.
I have some code for preparing batches here:
https://github.com/bheinzerling/dougu/blob/2f54b14d588f17d77b7a8bca9f4e5eb38d6a2805/dougu/bert.py#L98
The important methods are subword_tokenize_to_ids and subword_tokenize, you can probably ignore the other stuff.
With this, feature extraction for each sentence, i.e. a list of tokens, is simply:
bert = dougu.bert.Bert.Model("bert-base-cased")
featurized_sentences = []
for tokens in sentences:
features = {}
features["bert_ids"], features["bert_mask"], features["bert_token_starts"] = bert.subword_tokenize_to_ids(tokens)
featurized_sentences.append(features)
Then I use a custom collate function for a DataLoader that turns featurized_sentences into batches:
def collate_fn(featurized_sentences_batch):
bert_batch = [torch.cat(features[key] for features in featurized_sentences], dim=0) for key in ("bert_ids", "bert_mask", "bert_token_starts")]
return bert_batch
A simple sequence tagger module would look something like this:
class SequenceTagger(torch.nn.Module):
def __init__(self, data_parallel=True):
bert = BertModel.from_pretrained("bert-base-cased").to(device=torch.device("cuda"))
if data_parallel:
self.bert = torch.nn.DataParallel(bert)
else:
self.bert = bert
bert_dim = 786 # (or get the dim from BertEmbeddings)
n_labels = 5 # need to set this for your task
self.out = torch.nn.Linear(bert_dim, n_labels)
... # droput, log_softmax...
def forward(self, bert_batch, true_labels):
bert_ids, bert_mask, bert_token_starts = bert_batch
# truncate to longest sequence length in batch (usually much smaller than 512) to save GPU RAM
max_length = (bert_mask != 0).max(0)[0].nonzero()[-1].item()
if max_length < bert_ids.shape[1]:
bert_ids = bert_ids[:, :max_length]
bert_mask = bert_mask[:, :max_length]
segment_ids = torch.zeros_like(bert_mask) # dummy segment IDs, since we only have one sentence
bert_last_layer = self.bert(bert_ids, segment_ids)[0][-1]
# select the states representing each token start, for each instance in the batch
bert_token_reprs = [
layer[starts.nonzero().squeeze(1)]
for layer, starts in zip(bert_last_layer, bert_token_starts)]
# need to pad because sentence length varies
padded_bert_token_reprs = pad_sequence(
bert_token_reprs, batch_first=True, padding_value=-1)
# output/classification layer: input bert states and get log probabilities for cross entropy loss
pred_logits = self.log_softmax(self.out(self.dropout(padded_bert_token_reprs)))
mask = true_labels != -1 # I did set label = -1 for all padding tokens somewhere else
loss = cross_entropy(pred_logits, true_labels)
# average/reduce the loss according to the actual number of of predictions (i.e. one prediction per token).
loss /= mask.float().sum()
return loss
Wrote this without checking if it runs (my actual code is tied into some other things so I cannot just copy&paste it), but it should help you get started.
@bheinzerling Thanks a lot for the starter, got awesome results!
Thanks for sharing these tips here! It helps a lot.
I tried to finetune BERT on multiple imbalanced datasets and found the result quite unstable... For an imbalanced dataset, I mean there are much more O labels than the others under the {B,I,O} tagging scheme. Tried weighted cross-entropy loss but the performance is still not as expected. Has anyone met the same issue?
Thanks!
Hi~@bheinzerling I uesd batch size=16, and lr=2e-5, get the dev F1=0.951 and test F1=0.914 which lower than ELMO. What about your result now?
@kugwzk I didn't do any more CoNLL'03 runs since the numbers reported in the BERT paper were apparently achieved by using document context, which is different from the standard sentence-based evaluation. You can find more details here: https://github.com/allenai/allennlp/pull/2067#issuecomment-443961816
Hmmm...I think they should tell that in the paper...And do you know where to find that they used document context?
That's what the folks over at allennlp said. I don't know where they got this information, maybe personal communication with one of the BERT authors?
Anyway, thank you very much for tell me that.
https://github.com/kamalkraj/BERT-NER Replicated results from BERT paper
https://github.com/JianLiu91/bert_ner gives a solution that is very easy to understand. However, I still wonder whether is the best practice.
Hi all,
I am trying to train the BERT model on some data that I have. However, I am having trouble understanding how to adjust the labels following tokenization. I am trying to perform word level classification (similar to NER)
If I have the following tokenized sentence and its' labels:
original_tokens = ['The', <start>', 'eng-30-01258617-a', '<end>', 'frailty']
original_labels = [0, 2, 3, 4, 1]
Then after using the BERT tokenizer I get the following:
bert_tokens = ['[CLS]', 'the', '<start>', 'eng-30-01258617-a', '<end>', 'frail', '##ty', '[SEP]']
Also, I adjust my label array as follows:
bert_labels = [0, 2, 3, 4, 1, 1]
N.B. Tokens such as eng-30-01258617-a are not tokenized further as I included an ignore list which contains words and tokens that I do not want tokenized and I swapped them with the [unusedXXX] tokens found in the vocab.txt file.
Notice how the last word 'frailty' is transformed into ['frail', '##ty'] and the label '1' which was used for the whole word is now placed under each word piece. Is this the correct way of doing it? If you would like a more in-depth explanation of what I am trying to achieve you can read the following: https://stackoverflow.com/questions/56129165/how-to-handle-labels-when-using-the-berts-wordpiece-tokenizer
Any help would be greatly appreciated! Thanks in advance
@dangal95, adjusting the original labels is probably not the best way. A simpler method that works well is described in this issue, here https://github.com/huggingface/pytorch-pretrained-BERT/issues/64#issuecomment-443703063
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
@nijianmo Hi, I am recently considering using weighted loss in NER task. I wonder if you have tried weighted crf or weighted softmax in pytorch implementation. If so, did you get a good performance ? Thanks in advance.
Many thanks to @bheinzerling! For those who may concern , I've implemented a NER model based on pytorch-transformers and @bheinzerling's idea, which might help you get a quick start on it. Welcome to check this out.