Sentence-transformers: Question: Why BERT underperforms

Created on 12 Dec 2019  路  3Comments  路  Source: UKPLab/sentence-transformers

I was wondering what your thoughts were on why BERT underperforms at sentence embeddings (i.e. averaging the last output layer or using the CLS token)?

Most helpful comment

Hi @aclifton314
See this comment from Jacob Devlin:
Comment

Cosine-Similarity / Manhatten-Distance / Euclidean-Distance treat all dimension equally, with the same weight. I.e., to get well working sentence embeddings for unsupervised tasks, it is important that all dimensions "make sense" and have the "same scale".

BERT was not optimized for this, that all output-dimensions must contribute equally. For supervised tasks, the classifier learns which dimension contribute and what the "scale" of those are. But for unsupervised tasks, we sadly don't have this luxury.

I think that is the reason why BERT out-of-the-box does not produce well working sentence representations.

Best
Nils Reimers

All 3 comments

Hi @aclifton314
See this comment from Jacob Devlin:
Comment

Cosine-Similarity / Manhatten-Distance / Euclidean-Distance treat all dimension equally, with the same weight. I.e., to get well working sentence embeddings for unsupervised tasks, it is important that all dimensions "make sense" and have the "same scale".

BERT was not optimized for this, that all output-dimensions must contribute equally. For supervised tasks, the classifier learns which dimension contribute and what the "scale" of those are. But for unsupervised tasks, we sadly don't have this luxury.

I think that is the reason why BERT out-of-the-box does not produce well working sentence representations.

Best
Nils Reimers

@nreimers Got it! Thanks!

Hi,
How can I check cosine similarity between 2 sentences whose embeddings are scored at different time?

Below code is not working and gives error at cdist(a1, a2, 'cosine')[0][0]

sentence_embeddings = model.encode(['This framework generates embeddings for each input sentence']])
a1 = 1
for sentence, embedding in zip(sentences, sentence_embeddings):
print("Sentence:", sentence)
print("Embedding:", embedding)
a1 = embedding
print("")

sentence_embeddings = model.encode(['Sentences are passed as a list of string.']])
a2 = 1
for sentence, embedding in zip(sentences, sentence_embeddings):
print("Sentence:", sentence)
print("Embedding:", embedding)
a2 = embedding
print("")

import scipy.spatial
yy = scipy.spatial.distance.cdist(a1, a2, "cosine")[0]
print(yy)

Y = cdist(a1, a2, 'cosine')[0][0]
print(Y)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhilipMay picture PhilipMay  路  3Comments

AMChierici picture AMChierici  路  5Comments

ec1841 picture ec1841  路  4Comments

anatoly-khomenko picture anatoly-khomenko  路  5Comments

shivprasad94 picture shivprasad94  路  3Comments