<p>As mentioned in other answers, BERT was not meant to produce sentence level embeddings. Now, let's work on the how we can leverage power of BERT for computing context-sensitive sentence level embeddings.</p>
<p>BERT does carry the context at word level, here is an example:</p>
<p>This is a wooden <strong>stick</strong>.
<strong>Stick</strong> to your work.</p>
<p>Above two sentences carry the word 'stick', BERT does a good job in computing embeddings of stick as per sentence(or say, context).</p>
<p>Now, let's move to one another example:</p>
<p>--What is your age?</p>
<p>--How old are you?</p>
<p>Above two sentences are contextually very similar, so, we need a model that can accept a sentence or text chunk or paragraph and produce right embeddings collectively. Here is how it can be achieved.</p>
<p>Method 1:</p>
<p>Use pre-trained sentence_transformers, here is <a href="https://huggingface.co/models?pipeline_tag=sentence-similarity&amp;sort=downloads" rel="nofollow noreferrer">link</a> to huggingface hub.</p>
<pre><code>from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim


model = SentenceTransformer(r&quot;sentence-transformers/paraphrase-MiniLM-L6-v2&quot;)


embd_a = model.encode(&quot;What is your age?&quot;)
embd_b = model.encode(&quot;How old are you?&quot;)


sim_score = cos_sim(embd_a, embd_b)

print(sim_score)

output: tensor([[0.8648]])
</code></pre>
<p>Now, there may be a question on how can we train our on sentence_transformer, specific to a domain. Here we go,</p>
<ol>
<li>Supervised approach:</li>
</ol>
<p>A common challenge for Data Scientist or MLEngineers is to get rightly annotated data, mostly it is hard to get it in good volume, but say, if you have it here is how we can train our on sentence_transformer (don't worry, there is an unsupervised approach too).</p>
<pre><code>model = SentenceTransformer('distilbert-base-nli-mean-tokens') 

train_examples = [InputExample(texts=['My first sentence', 'My second sentence'], label=0.8),
InputExample(texts=['Another pair', 'Unrelated sentence'], label=0.3)]

train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=16)

train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=16)
train_loss = losses.CosineSimilarityLoss(model)

#Tune the model
model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=1, warmup_steps=100)
</code></pre>
<p>More details <a href="https://www.sbert.net/docs/training/overview.html" rel="nofollow noreferrer">here</a>.</p>
<blockquote>
<p>Tip: If you have a set of sentences that are similar to each other, say, you have a CSV, where column A and B contains sentences similar to each other(I mean each row will have a pair of sentences which are similar to each other), just load the csv and assign random values between 0.85 to 0.95 as similarity score and proceed.</p>
</blockquote>
<ol start="2">
<li>Unsupervised approach</li>
</ol>
<p>Say you don't have a huge set of annotated data, but you want to train a domain specific sentence_transformer, here is how we do it. Even for unsupervised training, data will be required, i.e. list of sentences/paragraphs, but need not to be annotated. Say, you don't have any data at all, still there is a work round (please visit last part of the answer).</p>
<p>Multiple approaches are available for unsupervised training, listing two of the most prominent ones. To see list of all available approaches, please visit <a href="https://www.sbert.net/examples/unsupervised_learning/README.html" rel="nofollow noreferrer">here</a>.</p>
<p><strong>TSDAE</strong> <a href="https://arxiv.org/pdf/2104.06979.pdf" rel="nofollow noreferrer">link</a> to research paper.</p>
<pre><code>from sentence_transformers import SentenceTransformer, LoggingHandler
from sentence_transformers import models, util, datasets, evaluation, losses
from torch.utils.data import DataLoader

# Define your sentence transformer model using CLS pooling
model_name = 'bert-base-uncased'
word_embedding_model = models.Transformer(model_name)
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), 'cls')
model = SentenceTransformer(modules=[word_embedding_model, pooling_model])

# Define a list with sentences (1k - 100k sentences)
train_sentences = [&quot;Your set of sentences&quot;,
                   &quot;Model will automatically add the noise&quot;, 
                   &quot;And re-construct it&quot;,
                   &quot;You should provide at least 1k sentences&quot;]

# Create the special denoising dataset that adds noise on-the-fly
train_dataset = datasets.DenoisingAutoEncoderDataset(train_sentences)

# DataLoader to batch your data
train_dataloader = DataLoader(train_dataset, batch_size=8, shuffle=True)

# Use the denoising auto-encoder loss
train_loss = losses.DenoisingAutoEncoderLoss(model, decoder_name_or_path=model_name, tie_encoder_decoder=True)

# Call the fit method
model.fit(
    train_objectives=[(train_dataloader, train_loss)],
    epochs=1,
    weight_decay=0,
    scheduler='constantlr',
    optimizer_params={'lr': 3e-5},
    show_progress_bar=True
)

model.save('output/tsdae-model')
</code></pre>
<p><strong>SimCSE</strong> <a href="https://arxiv.org/pdf/2104.08821.pdf" rel="nofollow noreferrer">link</a> to research paper</p>
<pre><code>from sentence_transformers import SentenceTransformer, InputExample
from sentence_transformers import models, losses
from torch.utils.data import DataLoader

# Define your sentence transformer model using CLS pooling
model_name = 'distilroberta-base'
word_embedding_model = models.Transformer(model_name, max_seq_length=32)
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
model = SentenceTransformer(modules=[word_embedding_model, pooling_model])

# Define a list with sentences (1k - 100k sentences)
train_sentences = [&quot;Your set of sentences&quot;,
                   &quot;Model will automatically add the noise&quot;,
                   &quot;And re-construct it&quot;,
                   &quot;You should provide at least 1k sentences&quot;]

# Convert train sentences to sentence pairs
train_data = [InputExample(texts=[s, s]) for s in train_sentences]

# DataLoader to batch your data
train_dataloader = DataLoader(train_data, batch_size=128, shuffle=True)

# Use the denoising auto-encoder loss
train_loss = losses.MultipleNegativesRankingLoss(model)

# Call the fit method
model.fit(
    train_objectives=[(train_dataloader, train_loss)],
    epochs=1,
    show_progress_bar=True
)

model.save('output/simcse-model')
</code></pre>
<blockquote>
<p>Tip: If you carefully observer, major difference is in the loss function used. To see a list of all the loss function applicable to such training scenarios, visit <a href="https://www.sbert.net/docs/package_reference/losses.html" rel="nofollow noreferrer">here</a>. Also, with all the experiments I did, I found that TSDAE is more useful, when you want decent precision and good recall. However, SimCSE can be used when you want very high precision and low recall.</p>
</blockquote>
<p>Now, if you don't have sufficient data to fine tune the model, but you find a BERT model trained on your domain, you can directly leverage that by adding pooling and dense layers. Please do research on what is 'pooling', to have better understanding on what you are doing.</p>
<pre><code>from sentence_transformers import SentenceTransformer, models
from torch import nn

word_embedding_model = models.Transformer('bert-base-uncased', max_seq_length=256)
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
dense_model = models.Dense(in_features=pooling_model.get_sentence_embedding_dimension(), out_features=256, activation_function=nn.Tanh())

model = SentenceTransformer(modules=[word_embedding_model, pooling_model, dense_model])
</code></pre>
<blockquote>
<p>Tip: With above approach, if you start getting extreme high cosine score, it is an alarm to do negative testing. Sometime, simply adding pooling layers may not help, you must take few examples and check similarity scores for the inputs that are not similar (it is possible that even for dissimilar sentences, this may show good similarity, and that is the time you should stop and try to collect some data and do unsupervised training)</p>
</blockquote>
<p>People who are interested in going deeper, here is a list of topics that may help you.</p>
<ol>
<li>Pooling</li>
<li>Siamese Networks</li>
<li>Contrastive Loss</li>
</ol>
<p>:) :)</p>
BERT sentence embeddings from transformers
<p>I'm trying to get sentence vectors from hidden states in a BERT model.  Looking at the huggingface BertModel instructions <a href="https://huggingface.co/bert-base-multilingual-cased?text=This%20sentence%20etc" rel="noreferrer">here</a>, which say:</p>
<pre><code>from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased')
model = BertModel.from_pretrained(&quot;bert-base-multilingual-cased&quot;)
text = &quot;Replace me by any text you'd like.&quot;
encoded_input = tokenizer(text, return_tensors='pt') 
output = model(**encoded_input)
</code></pre>
<p>So first note, as it is on the website, this does /not/ run. You get:</p>
<pre><code>&gt;&gt;&gt; Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
TypeError: 'BertTokenizer' object is not callable
</code></pre>
<p>But it looks like a minor change fixes it, in that you don't call the tokenizer directly, but ask it to encode the input:</p>
<pre><code>encoded_input = tokenizer.encode(text, return_tensors=&quot;pt&quot;)
output = model(encoded_input)
</code></pre>
<p>OK, that aside, the tensors I get, however, have a different shape than I expected:</p>
<pre><code>&gt;&gt;&gt; output[0].shape
torch.Size([1,11,768])
</code></pre>
<p>This is a lot of layers.  Which is the correct layer to use for sentence embeddings?  <code>[0]</code>?  <code>[-1]</code>?  Averaging several?  I have the goal of being able to do cosine similarity with these, so I need a proper 1xN vector rather than an NxK tensor.</p>
<p>I see that the popular <a href="https://github.com/hanxiao/bert-as-service#building-a-qa-semantic-search-engine-in-3-minutes" rel="noreferrer">bert-as-a-service project</a> appears to use <code>[0]</code></p>
<p>Is this correct? Is there documentation for what each of the layers are?</p>