bertForNextSentencePrediction
Author: ankitsharma1999
Pull request: no

I copied the code from [PyTorch's official site](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_bert/) for `bertForNextSentencePrediction`. I get the next_sent_classif_logits as `tensor([[ 5.2880, -6.0952]])`. How do I get the next sentence from these values?


Comments:
It's a classification task - is the given sentence the next sentence? It's not going to generate the next sentence for you, as BERT is not a classical language model

So, how do I know from these values whether the next sentence should be classified as the next sentence or not?

Softmax over it will give you the probabilities - i'm guessing the first is yes next sentence, but you can probably play around with some toy examples to know which dimension is which.

Thank you for your insight.

Here is a toy example using BertForNextSentencePrediction

```
import torch
import pytorch_pretrained_bert
from pytorch_pretrained_bert import BertTokenizer, BertAdam, BertForNextSentencePrediction

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForNextSentencePrediction.from_pretrained('bert-base-uncased')

#  Prepare tokenized input
text1 = "what does a technical SEO do?"
text2 = "A technical seo optimizes websites blah."
# 0=Good / 1 = Bad
label = 0

text1_toks = ["[CLS]"] + tokenizer.tokenize(text1) + ["[SEP]"]
text2_toks = tokenizer.tokenize(text2) + ["[SEP]"]

indexed_tokens = tokenizer.convert_tokens_to_ids(text1_toks + text2_toks)
segments_ids = [0]*len(text1_toks) + [1]*len(text2_toks)

tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])

# Load bertForNextSentencePrediction
bert_optimizer = BertAdam(model.parameters(), 
                               lr = 0.002, 
                               warmup = 0.1, 
                               max_grad_norm=-1, 
                               weight_decay=-0.0001,
                               t_total = 1
                              )

print(text1_toks + text2_toks)
print(segments_ids)
print()


# Example Evaluate
model.eval()
# Predict the next sentence classification logits
with torch.no_grad():
    prediction = model(tokens_tensor, segments_tensors)

softmax = torch.nn.Softmax(dim=1)
prediction_sm = softmax(prediction)
print ("Good/Bad:", prediction_sm[0].tolist())

# Example Train
model.train()
loss = model(tokens_tensor, segments_tensors, next_sentence_label=torch.tensor([label]))
print("Loss with label {}:".format(label),loss.item())
loss.backward()
bert_optimizer.step()
```

> Here is a toy example using BertForNextSentencePrediction
> 
> ```
> import torch
> import pytorch_pretrained_bert
> from pytorch_pretrained_bert import BertTokenizer, BertAdam, BertForNextSentencePrediction
> 
> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
> model = BertForNextSentencePrediction.from_pretrained('bert-base-uncased')
> 
> #  Prepare tokenized input
> text1 = "what does a technical SEO do?"
> text2 = "A technical seo optimizes websites blah."
> # 0=Good / 1 = Bad
> label = 0
> 
> text1_toks = ["[CLS]"] + tokenizer.tokenize(text1) + ["[SEP]"]
> text2_toks = tokenizer.tokenize(text2) + ["[SEP]"]
> 
> indexed_tokens = tokenizer.convert_tokens_to_ids(text1_toks + text2_toks)
> segments_ids = [0]*len(text1_toks) + [1]*len(text2_toks)
> 
> tokens_tensor = torch.tensor([indexed_tokens])
> segments_tensors = torch.tensor([segments_ids])
> 
> # Load bertForNextSentencePrediction
> bert_optimizer = BertAdam(model.parameters(), 
>                                lr = 0.002, 
>                                warmup = 0.1, 
>                                max_grad_norm=-1, 
>                                weight_decay=-0.0001,
>                                t_total = 1
>                               )
> 
> print(text1_toks + text2_toks)
> print(segments_ids)
> print()
> 
> 
> # Example Evaluate
> model.eval()
> # Predict the next sentence classification logits
> with torch.no_grad():
>     prediction = model(tokens_tensor, segments_tensors)
> 
> softmax = torch.nn.Softmax(dim=1)
> prediction_sm = softmax(prediction)
> print ("Good/Bad:", prediction_sm[0].tolist())
> 
> # Example Train
> model.train()
> loss = model(tokens_tensor, segments_tensors, next_sentence_label=torch.tensor([label]))
> print("Loss with label {}:".format(label),loss.item())
> loss.backward()
> bert_optimizer.step()
> ```

Thanks for this, any idea how do this in batches? How we are supposed to pad different input lenghts?