Fine Tune Bert Ner using TFBertForTokenClassification.from_pretrained
See original GitHub issueHey,
I am new to the transformers Bert Training world and am trying to fine tune Bert model for NER on a coNll like dataset. But its not training the model and giving the below error
ValueError: No gradients provided for any variable: [‘tf_bert_for_token_classification_8/classifier/kernel:0’, ‘tf_bert_for_token_classification_8/classifier/bias:0’].
Below is my code
tr_inputs = tf.convert_to_tensor(tr_inputs)
val_inputs = tf.convert_to_tensor(val_inputs)
tr_tags = tf.convert_to_tensor(tr_tags)
val_tags = tf.convert_to_tensor(val_tags)
tr_masks = tf.convert_to_tensor(tr_masks)
val_masks = tf.convert_to_tensor(val_masks)
tr_segs = tf.convert_to_tensor(tr_segs)
val_segs = tf.convert_to_tensor(val_segs)
input_features_dict = {"input_ids":tr_inputs, "attention_mask":tr_masks, "token_type_ids":tr_segs, 'labels':tr_tags}
val_features_dict = {"input_ids":val_inputs, "attention_mask":val_masks, "token_type_ids":val_segs, 'labels':tr_tags}
train_data = tf.data.Dataset.from_tensor_slices(input_features_dict)
batch_train_data = train_data.batch(batch_num)
valid_data = tf.data.Dataset.from_tensor_slices(val_features_dict)
batch_valid_data = valid_data.batch(batch_num)
modell = TFBertForTokenClassification.from_pretrained('bert-base-uncased',num_labels=len(tag2idx))
modell.layers[2].activation = tf.keras.activations.softmax
modell.layers[0].trainable = False
modell.compile(optimizer=optimizer, loss=loss, metrics=[metrics])
modell.fit(batch_train_data, epochs=epochs, validation_data=batch_val_data)
Not sure what needs to be done. Any advice/pointers on this would be highly helpful for me.
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (7 by maintainers)
Top Results From Across the Web
How to Fine-Tune BERT for NER Using HuggingFace
It uses a large text corpus to learn how best to represent tokens and perform downstream-tasks like text classification, token classification, ...
Read more >How to fine tune bert on entity recognition? - Beginners
I have a paragraph for example below Either party may terminate this Agreement by written notice at any time if the other party...
Read more >Tutorial: How to Fine-tune BERT for NER - Skim AI
Introduction. This article is on how to fine-tune BERT for Named Entity Recognition (NER). Specifically, how to train a BERT variation, SpanBERTa, for...
Read more >Named-Entity Recognition on HuggingFace – Weights & Biases
After preprocessing , we will now finetune pretrained bert-base-cased model to train NER model for token classification. For finetuning , we will use...
Read more >Fine-Tuned Named Entity Recognition with Hugging Face BERT
First we need to retrieve a dataset that is set up with text and it's associated entity labels. Because we want to fine-tune...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello @aks2193!
Sorry for this, but for now you cannot use
.compile()
+.fit()
to train a Token Classification model. To make it short, this is because a layer is not used and then the gradients will be None, something that.fit()
cannot handle.If you want to train a NER I suggest you to use the example.
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.