bert icon indicating copy to clipboard operation
bert copied to clipboard

Sentence embedding for STS task by fine-tuning bert

Open cdluminate opened this issue 7 years ago • 67 comments

I tried to fine-tune the BERT model as an embedding model, which maps sentences to a space where the cosine similarity between two sentence embedding vectors can be interpreted as the sentence similarity level. Specifically, the embedding vectors (corresponds to [CLS]) of every sentence pair is first normalized to have unit L-2 norm. Next, the dot product (i.e. cosine similarity) between each embedding pair is multiplied by 5, then aligned with the labels by using MSE loss. Learning rate has been changed to 1e-5 to mitigate convergence problem.

After 20 epochs of fine-tuning, the resulting 12-layer model only get a 0.66 pearson correlation coefficient on the validation set, which performs far worse than a BERT model fine-tuned in the simple regression manner (12-layer, pearson = 0.89).

My question is, is there any suggestion about fine-tuning BERT for sentence embeddings? Thanks!

cdluminate avatar Dec 17 '18 12:12 cdluminate

method PPMCC (STS-B dev)
bert, no FT, cosine similarity between sentence embedding ([CLS]) 0.29
bert, FT, simple regression 0.89
bert, FT, cosine similarity between sentence embedding ([CLS]) 0.66
bert, no FT, cosine similarity between mean-pooled sequence embeddings (mean_pool([CLS], tok1, ..., [SEP])) 0.59
average word vector (spaCy, en_core_web_lg) 0.54

cdluminate avatar Dec 17 '18 13:12 cdluminate

From here :

I quote from google-research/bert#164 (comment)

And even if they are decent representations when fed into a DNN trained for a downstream task, it doesn't mean that they will be meaningful in terms of cosine distance. (Since cosine distance is a linear space where all dimensions are weighted equally).

Suggestion from my side, if you want to use cosine distance anyway, then please focus on the rank not the absolute value. Namely, do not use:

cosine(A, B) > 0.9 then A and B are similar

please consider the following instead:

if cosine(A, B) > cosine(A, C) then A is more similar to B than C.

You can't use cosine distance directly between the embeddings produced by BERT. You need to apply another layer between the distance and BERT.

So on top of BERT, you put your custom layer (I used LSTM, but a simple Feed Forward network might be enough depending on what you're trying to achieve), and use the output of this custom layer to compute the cosine distance.

astariul avatar Dec 18 '18 00:12 astariul

@Colanim Thank you very much for this hint. BTW have you validated the resulting embeddings on STS-B validation set after adding a custom layer such as LSTM? How much is the pearson correlation coefficient?

cdluminate avatar Dec 18 '18 01:12 cdluminate

I used a Siamese-type network, separating the sentence representation by BERT.

Basically, I input one sentence into BERT, get the feature vectors for this sentence, input this feature vectors into LSTM. Do the same for the second sentence, then compare the output of LSTMs with distance (I used Manhattan distance, but cosine distance works as well).

My vanilla network is less good than the end-to-end BERT : I could reach 0.78 (Pearson correlation). This is because in my model, BERT represent sentence one by one, and not the sentence pair together.

After a few improvements (ensembling), I could reach 0.82, but that's all.

Why exactly are you using cosine similarity ? I believe this approach is not good compared to the end-to-end BERT.

astariul avatar Dec 18 '18 01:12 astariul

I used a Siamese-type network, separating the sentence representation by BERT.

Basically, I input one sentence into BERT, get the feature vectors for this sentence, input this feature vectors into LSTM. Do the same for the second sentence, then compare the output of LSTMs with distance (I used Manhattan distance, but cosine distance works as well).

My vanilla network is less good than the end-to-end BERT : I could reach 0.78 (Pearson correlation). This is because in my model, BERT represent sentence one by one, and not the sentence pair together.

0.78 looks better than my method.

After a few improvements (ensembling), I could reach 0.82, but that's all.

Why exactly are you using cosine similarity ? I believe this approach is not good compared to the end-to-end BERT.

I have a dataset which comprises 1e6 sentences and I need to be able to calculate the similarity between any two samples. If I use the end-to-end BERT, I'll have to forward 1e12 sentence pairs which takes hundreds of years. But if I choose to calculate cosine similarity after extracting embeddings for every sentence, an amount of time could be saved. That's because matrix multiplication is much faster compared to BERT forward.

cdluminate avatar Dec 18 '18 01:12 cdluminate

0.78 looks better than my method.

This come from the fact that you use your distance metric directly on BERT embeddings. You need an additional layer.

I have a dataset which comprises 1e6 sentences and I need to be able to calculate the similarity between any two samples. If I use the end-to-end BERT, I'll have to forward 1e12 sentence pairs which takes hundreds of years. But if I choose to calculate cosine similarity after extracting embeddings for every sentence, an amount of time could be saved. That's because matrix multiplication is much faster compared to BERT forward.

Do you actually have a label for the 1e12 sentence pairs ?? You right, if you extract embeddings for every sentence and use it later to compare between 2 sentences, you will save time. However, by doing so, you will also loose accuracy, because BERT will treat each sentence independently and not as a pair anymore (so information between sentence parity will be lost).

Just out of curiosity, how long time is need to extract 1e6 sentences embeddings from BERT ? What about memory ?

astariul avatar Dec 18 '18 02:12 astariul

0.78 looks better than my method.

This come from the fact that you use your distance metric directly on BERT embeddings. You need an additional layer.

I have a dataset which comprises 1e6 sentences and I need to be able to calculate the similarity between any two samples. If I use the end-to-end BERT, I'll have to forward 1e12 sentence pairs which takes hundreds of years. But if I choose to calculate cosine similarity after extracting embeddings for every sentence, an amount of time could be saved. That's because matrix multiplication is much faster compared to BERT forward.

Do you actually have a label for the 1e12 sentence pairs ??

I don't have label, what I need to do is to predict similarity values for the 1e12 pairs.

You right, if you extract embeddings for every sentence and use it later to compare between 2 sentences, you will save time. However, by doing so, you will also loose accuracy, because BERT will treat each sentence independently and not as a pair anymore (so information between sentence parity will be lost).

Just out of curiosity, how long time is need to extract 1e6 sentences embeddings from BERT ? What about memory ?

Less than 8 hours. Using Titian X (Pascal) x1. Max sequence length was 32, IIRC.

cdluminate avatar Dec 18 '18 02:12 cdluminate

You indeed have a lot of data ^^ Then I think you can try adding a custom layer after BERT. But expect Pearson correlation decrease.

astariul avatar Dec 18 '18 02:12 astariul

fyi, starting from 1.6.1, bert-as-service supports serving fine-tuned model out of the box. See: https://github.com/hanxiao/bert-as-service#serving-a-fine-tuned-bert-model

To upgrade, please do:

pip install -U bert-serving-server bert-serving-client

hanxiao avatar Dec 19 '18 10:12 hanxiao

I tried to fine-tune the BERT model as an embedding model, which maps sentences to a space where the cosine similarity between two sentence embedding vectors can be interpreted as the sentence similarity level. Specifically, the embedding vectors (corresponds to [CLS]) of every sentence pair is first normalized to have unit L-2 norm. Next, the dot product (i.e. cosine similarity) between each embedding pair is multiplied by 5, then aligned with the labels by using MSE loss. Learning rate has been changed to 1e-5 to mitigate convergence problem.

After 20 epochs of fine-tuning, the resulting 12-layer model only get a 0.66 pearson correlation coefficient on the validation set, which performs far worse than a BERT model fine-tuned in the simple regression manner (12-layer, pearson = 0.89).

My question is, is there any suggestion about fine-tuning BERT for sentence embeddings? Thanks!

I am doing something similar. I used bert embedding with max pooling and compute cosine similarity, and it is better than using glove embedding.

I wonder what is your current approach to sentence similarity now. I have similar concern, and I do not want to input two sentences to bert model at a time cause that will dramatically increase my training sample number.

aluminumbox avatar Dec 21 '18 02:12 aluminumbox

I used a Siamese-type network, separating the sentence representation by BERT.

Basically, I input one sentence into BERT, get the feature vectors for this sentence, input this feature vectors into LSTM. Do the same for the second sentence, then compare the output of LSTMs with distance (I used Manhattan distance, but cosine distance works as well).

My vanilla network is less good than the end-to-end BERT : I could reach 0.78 (Pearson correlation). This is because in my model, BERT represent sentence one by one, and not the sentence pair together.

After a few improvements (ensembling), I could reach 0.82, but that's all.

Why exactly are you using cosine similarity ? I believe this approach is not good compared to the end-to-end BERT.

Did you use the last state of LSTM output, or you did a pooling over all the state of LSTM output? Thanks!

aluminumbox avatar Dec 21 '18 02:12 aluminumbox

I wonder what is your current approach to sentence similarity now. I have similar concern, and I do not want to input two sentences to bert model at a time cause that will dramatically increase my training sample number.

Have you seen the table below the original post? I also tried ELMo (without FT, without adding network layers) and mean-pooled sentence vectors yield a pearson score of 0.66

cdluminate avatar Dec 21 '18 03:12 cdluminate

I wonder what is your current approach to sentence similarity now. I have similar concern, and I do not want to input two sentences to bert model at a time cause that will dramatically increase my training sample number.

Have you seen the table below the original post? I also tried ELMo (without FT, without adding network layers) and mean-pooled sentence vectors yield a pearson score of 0.66

No, I haven't seen the table of the original post. I just evaluated it on my end-to-end task. The overall accuracy increase after I turned to bert embedding. So I assume that CLS embedding doesn't represent the sentence embedding? I am trying CNN or LSTM with pooling or last state. I wonder what is the default solution to calculate sentence similarity using bert embedding.

aluminumbox avatar Dec 21 '18 04:12 aluminumbox

Did you use the last state of LSTM output, or you did a pooling over all the state of LSTM output?

I just used the last state of the LSTM


I tried several things, and for my problem, the best solution was to use Reduce-mean pooling between all the feature vectors outputted by BERT. Then I fed this into a LSTM (I believe a simple Feed Forward network would work equally good, if not better) and get the last state of the LSTM.

This last state of the LSTM is my sentence representation. I can then compare it to other sentence representation with a distance. So at the end I have a Siamese network.

Note : CLS embedding is a better solution for sentence representation if you can fine tune your model.

astariul avatar Dec 21 '18 06:12 astariul

Did you use the last state of LSTM output, or you did a pooling over all the state of LSTM output?

I just used the last state of the LSTM

I tried several things, and for my problem, the best solution was to use Reduce-mean pooling between all the feature vectors outputted by BERT. Then I fed this into a LSTM (I believe a simple Feed Forward network would work equally good, if not better) and get the last state of the LSTM.

This last state of the LSTM is my sentence representation. I can then compare it to other sentence representation with a distance. So at the end I have a Siamese network.

Note : CLS embedding is a better solution for sentence representation if you can fine tune your model.

Ummm...I am confused. After reduce-mean pooling, you get a batch_size * bert_dim size output? So for each sample, you feed a vector to LSTM? How is this reduce-mean done in your solution?

aluminumbox avatar Dec 21 '18 07:12 aluminumbox

After reduce-mean pooling, you get a batch_size * bert_dim size output?

Right.

So for each sample, you feed a vector to LSTM?

Actually, my problem is about document representation. So I need to input several vectors to the LSTM. But it is working with only 1 vector (a document of only 1 sentence).

How is this reduce-mean done in your solution?

I used BERT-as-service.

astariul avatar Dec 21 '18 07:12 astariul

After reduce-mean pooling, you get a batch_size * bert_dim size output?

Right.

So for each sample, you feed a vector to LSTM?

Actually, my problem is about document representation. So I need to input several vectors to the LSTM. But it is working with only 1 vector (a document of only 1 sentence).

How is this reduce-mean done in your solution?

I used BERT-as-service.

Oh I see. It seems I should try several approaches to find the best one. My problem is that my training set is really small. I also thought about multiply CLS vector of two sentences directly, but that seems introduced many variables. Still trying to find the best approach.

aluminumbox avatar Dec 21 '18 07:12 aluminumbox

Keep us updated :)

astariul avatar Dec 21 '18 07:12 astariul

Hi @Colanim, sorry to crash into your conversation, but I had this question about your statement below,

After a few improvements (ensembling), I could reach 0.82, but that's all.

When you say ensembling, what exactly have you done? So is it like getting output from different models and doing a majority vote (or some similar heuristic), or is something fancier than that?

amankhandelia avatar Dec 21 '18 16:12 amankhandelia

Hi, Thanks you all for sharing your results here. I am working on the same task, calculating similarity scores of sentences. I am wondering if someone had tried to fine-tune BERT using Conneau et al. approach [arXiv:1705.02364 figure-1]?

ShunChi100 avatar Dec 21 '18 22:12 ShunChi100