How to add RNN layer on top of Huggingface BERT model

I am working on a binary classification task and would like to try adding RNN layer on top of the last hidden layer of huggingface BERT PyTorch model. How can I extract the layer-1 and contact it with LSTM layer?

tokenizer = BertTokenizer.from_pretrained(model_path)
# Load BertForSequenceClassification, the pretrained BERT model with a single linear classification layer on top.
model = BertForSequenceClassification.from_pretrained(model_path, num_labels=len(lab2ind))

We can use BertModel instead of BertForSequenceClassification

And feeds hidden states output to LSTM

1 Like

Thank you for your reply. I am trying this now. But does add LSTM on top of Bert needs to train BERT from scratch?

You could use BertModel.from_pretrained and then LSTM :wink:

1 Like