# How to order sentences based on pairwise probabilities?

Iām generating pairwise sentence order probabilities in the following way:

``````import itertools
import random

import numpy as np
import tensorflow as tf
from transformers import BertTokenizer, TFBertForNextSentencePrediction

np.set_printoptions(suppress=True)
cache_dir = '/path/to/cache/dir'
pretrained_weights = 'bert-base-multilingual-cased'
tokenizer = BertTokenizer.from_pretrained(
pretrained_weights,
cache_dir=cache_dir,
)
models = TFBertForNextSentencePrediction.from_pretrained(
pretrained_weights,
cache_dir=cache_dir,
)
sentences = """
In āThe Necklaceā by Guy de Maupassant, the main character, Mathilde, has always dreamed of
being an aristocrat but lives in poverty.
Embarrassed about her lack of fine possessions, she borrows a necklace from a wealthy
friend but loses it.
The story is known for its subversive and influential twist ending
"""
sentences = [s.strip() for s in sentences.strip().split('.')]
random.shuffle(sentences)
print(sentences)
pairs = itertools.permutations(sentences, 2)
encoded = tokenizer.batch_encode_plus(pairs, return_tensors='np', padding=True)
outputs = models(encoded)
probs = tf.keras.activations.softmax(outputs[0], axis=-1)
for i, s in enumerate(sentences, 1):
print(f's{i}: {s}')
for s, prob in zip(itertools.permutations(['s1', 's2', 's3'], 2), probs):
print(s, prob)
``````

Iām not sure how to interpret the resulting probabilities to generate the ordered sentences.

``````s1: Embarrassed about her lack of fine possessions, she borrows a necklace from a wealthy
friend but loses it
s2: In āThe Necklaceā by Guy de Maupassant, the main character, Mathilde, has always dreamed of
being an aristocrat but lives in poverty
s3: The story is known for its subversive and influential twist ending
('s1', 's2') tf.Tensor([0.9987061  0.00129389], shape=(2,), dtype=float32)
('s1', 's3') tf.Tensor([0.9514299  0.04857007], shape=(2,), dtype=float32)
('s2', 's1') tf.Tensor([0.9994491  0.00055089], shape=(2,), dtype=float32)
('s2', 's3') tf.Tensor([0.94130975 0.05869029], shape=(2,), dtype=float32)
('s3', 's1') tf.Tensor([0.15520796 0.84479207], shape=(2,), dtype=float32)
('s3', 's2') tf.Tensor([0.98460925 0.01539072], shape=(2,), dtype=float32)
``````

Suggestions for better ways to order sentences are fine by me.