# BERT Next Sentence Prediction: How to do predictions?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%3E%5C
**Category:** Beginners\
**Created:** [June 17, 2021, 1:58pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927 "2021-06-17T13:58:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msamogh](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/msamogh/32/967_2.png) [@msamogh](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmsamogh)%5C
**Post date:** [June 17, 2021, 1:58pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F1 "2021-06-17T13:58:01Z")

</div>

Let’s say I have a pretrained BERT model (pretrained using NSP and MLM tasks as usual) on a large custom dataset.

The way I understand NSP to work is you take the embedding corresponding to the [CLS] token from the final layer and pass it onto a Linear layer that reduces it to 2 dimensions. Then, you apply a softmax on top of it to get predictions on whether the pair of sentences are consecutive or not.

Now, the published pretrained model I have does not include this “NSP head”, so I have to train one myself. How do I do this? Since the only parameters I presume I’ll need to tweak are the ones of the linear layer, will a small dataset be enough for this?

Why is the convention to throw away this NSP head? Isn’t it a useful thing to publish for others to use?

---

<div class="post-metadata">

**Author:** ![nicir](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicir/32/5702_2.png) [@nicir](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicir)%5C
**Post date:** [September 26, 2022, 3:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F2 "2022-09-26T15:16:59Z")

</div>

Have you found a solution for this?

---

<div class="post-metadata">

**Author:** ![cog](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@cog](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fcog)%5C
**Post date:** [September 27, 2022, 1:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F3 "2022-09-27T01:48:46Z")

</div>

hi @nicir .

Next Sentence Prediction (NSP) just train each sentence’s relation.

What is your train task?  
Using pre-trained model to fine-tuning at some next sentence prediction task? or model doesn’t trained NSP loss, only trained with MLM loss?  
Either both way, this class with hf document will be helpful to you : [BertForNectSentencePrediction](https://huggingface.co/docs/transformers/v4.22.1/en/model_doc/bert#transformers.BertForNextSentencePrediction)

in that code, there are BertOnlyNSP in class.

Hope this helps.

regards.

---

<div class="post-metadata">

**Author:** ![nicir](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicir/32/5702_2.png) [@nicir](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicir)%5C
**Post date:** [September 27, 2022, 8:50am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F5 "2022-09-27T08:50:04Z")

</div>

Hi @cog,

I’ve got a custom dataset of sentences and their following sentences with labels of 0 and 1, looking like this:

Dataset({  
features: [‘input\_ids’, ‘token\_type\_ids’, ‘labels’],  
num\_rows: 61  
})

where ‘input\_ids’ are the two sentences seperated by a [SEP] token like this:

[102, 196, 10094, 30925, 232, 23180, 1555, 853, 16778, 2517, 7526, 223, 24805, 17632, 2380, 125, 5367, 17157, 30881, 223, 5438, 1556, 304, 125, 249, 10063, 2292, 6200, 2379, 11550, 216, 199, 196, 10094, 30925, 232, 24557, 14324, 103, 478, 7895, 195, 510, 18411, 406, 5367, 17157, 106, 212, 21300, 2272, 394, 30489, 105, 24889, 215, 513, 128, 387, 190, 3218, 10260, 9470, 16707, 4635, 566, 103]

The ‘token\_type\_ids’ are zeros for the first sentence and ones for the second sentence.

The ‘labels’ are just 0s or 1s.

Now I want to train a NSP-Task based on my custom dataset (that will be larger in the end).

Can I use the [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer)-API to easily train a [BertForNextSentencePrediction](https://huggingface.co/docs/transformers/v4.22.1/en/model_doc/bert#transformers.BertForNextSentencePrediction)?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![cog](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@cog](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fcog)%5C
**Post date:** [September 27, 2022, 11:12am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F6 "2022-09-27T11:12:21Z")

</div>

yes. you can train BertForNextSentanceprediction with trainer.

just define model, and use Trainer.

Important thing is make Training arguments to fit BertForNextSentanceprediction class args.

Also, make sure dataloader output shape fit to model’s require input data.

Thank to nielsr and HF team, Here some [tutorial about Fine-tuning bert](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/BERT/Fine_tuning_BERT_(and_friends)_for_multi_label_text_classification.ipynb).

There code use Trainer for fine-tuning BERT, so you can use similar function, method etc… to your works.

regards.

---

<div class="post-metadata">

**Author:** ![nicir](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicir/32/5702_2.png) [@nicir](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicir)%5C
**Post date:** [September 29, 2022, 2:54pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbert-next-sentence-prediction-how-to-do-predictions%2F6927%2F7 "2022-09-29T14:54:32Z")

</div>

By Training arguments you mean the size of the tensor, e.g. of the logits?

Or do you mean that the dataset for Fine-Tuning should exactly match the form that given here: [BertForNextSentencePrediction](https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertForNextSentencePrediction)? (Like input\_ids, attention\_mask, output\_hidden\_states, …)

I will definitely do that tutorial, thank you so much!
