NLP/Paper Review

Sentence-BERT : Sentence Embedding using siamese BERT-Networks(SBERT) 리뷰

joannekim0420 2021. 10. 5. 17:31
728x90

Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks

 

ABSTRACT 

siamese and triplet network 구조를 이용하여 pretrianed 된 BERT 모델로 semantically meaningful sentence mebeddings를 얻는 모델. 

 

BERT architecture

기존 Bert의 한계

Computationally expensive

  EX) Sentence Similarity 

                  n = 10,000

          n·(n−1)/2 = 10000*9999/2

Bad sentence embeddings

  EX) Clustering & Semantic search

  - CLS token

  - average/max pooling of BERT output layer

  - worse than GloVe embeddings

 

첫 번째로 저자는 sentence bert가 나오게 된 계기를 bert 모델의 한계에서 찾는다. BERT는 문장 유사도를 구하는 task에서 하나의 bertsentence 12, 두 개의 문장을 넣어 유사도를 평가하기 때문에 굉장히 많은 연산을 필요로 한다.

예를 들어, 만 문장 사이에서 한 문장과 유사한 문장을 구하고자 한다면 총 10000 곱하기 9999 나누기 2와 같은 computation 이 필요로 합니다. 두 번째로 언급한 bert의 한계는 bertoutput으로 나온 각 토큰들의 embedding 값들이 과연 문장을 대표할 수 없다는 것이다.

이에 관련된 연구들이 진행되고 있지만 대표적으로는 CLS 토큰을 문장을 대표하는 벡터로 사용하기도 하고, 전체 outpumean 또는 max pooling 한 값을 사용한다. 하지만 이러한 방법은 문장을 제대로 대표할 수 있는 벡터가 아니고 GloVe embedding 결과 보다 낮다. 

 

 

Related Work

Sentence Embedding Models
-Skip-Thought
-InferSent
-Universal Sentence Encoder
 
The task on which sentence embeddings are trained significantly impacts their quality
(Learning Distributed Representations of Sentences from Unlabelled Data)
Previous neural sentence embedding methods started the training from a random initialization
 

Sentence embedding은 기존에도 활발히 연구되던 분야이다. sentence embedding을 구하는 방식에는 카운트 기반, 예측기반, 통계기반 , 딥러닝 모델 등 여러 방식이 존재하고, 대표적으로 Skip-thought, InferSent, Universal Sentence Encoder 등이 있다. 기존 sentence embedding은 어떤 데이터셋에서 train 됐냐에 따라 성능이 달라지고, 기존 딥러닝 기반 sentence embedding 방법들은 random initialization에서부터 훈련을 시작한다는 특징을 갖고 있다.

 

SBERT architecture

앞서 얘기했던 bert의 한계와 기존 sentence embedding의 문제를 해결하기 위해 저자는 sentence BERT , sBERT 모델은 제안한다. Sbert에서의 object function3가지, Classification objective function, regression objective function, triplet objective function 로 나누었다.

SBERT는 두 개의 bert encoder를 같은 fixed weight 들을 갖는 Siamese network 로 연결하여 마지막에 pooling layer를 얹어 하나의 fix된 길이인 sentence embedding 을 구한다. Classification objective function에서는 각각 구한 sentence vector uv, 그리고 두 개의 차이값을 모두 concat 한 값을 softmax 하고 Inference object function에서는 각 두 sentence embedding 벡터의 cosine similarity 로 학습한다.

 

 

 

 

세 번째 objective functiontriplet objective function에 대해서 간단하게 설명드리면, 어떤 anchor sentence A 에 대해 positive sentencepnegative sentencen 과의 유사도로 loss function을 계산합니다. 다시 말해, anchor 문장과 positive 관계인 문장이 거리는 짧아지도록 하고 negative 관계인 문장과는 거리가 멀어지도록 훈련하는 방식입니다. (해당 loss function식이 최소화 되도록)

 

Training Details

Training Dataset
- SNLI (
Standford Natural Language Inference dataset (570,000 sentences pairs)
- Multi-Genre NLI (430,000 sentence pairs)
Default pooling method [MEAN]
Cosine Similarity

-> Fine-tuned on BERT/ RoBERTa

모델을 Training 할 때는 SNLI datasetMulti-Genre NLI 데이터셋 총 백만문장 pair 를 사용했으며 pooling strategymeandefaut로 사용하는데, 그 이유로는 Ablation study에서 CLS 토큰, Max, MEAN으로 실험을 했지만 MEAN 사용하는게 두 데이터셋에서 모두 제일 높았고, concat하는 방법 또한 각각 두 개의 벡터 그리고 벡터의 차 값을 이용하는게 제일 높기 때문이다.

또한, 맨허튼, 유클리드 거리, 코사인 유사도로 모두 실험했지만 모두 큰 차이를 보이지 않았고 메인으로 cosine similarity 를 이용했으며, random initializing 으로 시작하는 기존 연구들과 달리 BERT/ RoBERTa 에 대해서 fine-tuning 하는 방식으로 training을 했다.

 

 

Evaluation – Semantic Textual Similarity, unsupervised STS

Spearman rank correlation ρ between the cosine similarity of sentence representations and the gold labels

BERT Avg, BERT-CLS scored lower than GloVe Avg
SBERT ranked top on most datasets
BERT and RoBERTa had only minor difference
Universal Sentence Encoder was trained on various datasets including news, question-answer, discussion forums which are more suitable to SICK-R data

 

첫 번째 결과는 unsupervised semantic textual similarity 태스크로, STS 데이터셋에 훈련하지 않은 채 STS 데이터셋과 SICK 데이터셋에 평가한 결과이다. 기존 GloVe, BERTaverage , CLS vector, Infersent, Univeral Sentence Encoder보다 모두 더 높은 spearman 상관 계수 값을 갖는다. BERTCLS 토큰, mean 값이 모두 GloVe 벡터보다 못하다는 것을 알 수 있다.

BERT 모델과 RoBERTa 모델에도 실험했지만, 데이터셋에 따라서 sota 찍은 모델이 번갈아 가면서 나오는 것으로 보아 BERT RoBERTa 에 따른 차이는 없는 것을 알 수 있다.

유일하게 Sbert가 밀렸던 곳이 초록색으로 박스 친 SICK-R dataUniversal Sentence Encoder모델을 이용한 것인데, 이 이유로는 wikipediaNLI데이터로 학습된 sbert와 달리 universal sentence encodenews, question-answer,discussion forums 의 데이터셋에 학습했기 때문에 SICK-R 데이터와 더 도메인이 비슷해서 더 좋게 나온것이라고 추측할 수 있다.

 

 

Evaluation – Semantic Textual Similarity, supervised STS

 

First training on NLI, then training on STSb strategy leads to slight improvement of 1-2points than only training on STSb
No significant difference between BERT and RoBERTa.

두 번째 실험은 STS benchmark training 데이터셋에 training을 한 다음 test set 에 평가한 것이다. 같은 도메인을 갖는 데이터셋에 train 한 만큼 스피어맨 상관계수는 unsupervised 보다 높게 나오는 것을 확인할 수 있다. STS benchma가 데이터셋에만 train을 한 것보다 NLI데이터셋에서 train을 하고 STStrain한 결과값이 더 높게 나오지만, 특히 BERTSBERT보다 1~2점 차이로 더 높은 결과물을 낸다. 논문에서는 이 외에도 다른 데이터셋에 여러 실험을 진행하지만, sbert transfer learning과 맞지 않다는 이유 등으로 항상 SOTA를 찍진 못했다.

 

 

Computational Efficiency

 

InferSent(Bi-Lstm) is much faster than SBERT on CPU due to simple network.
Transformer network is better suited for GPU

실제로 각각 CPUGPU에서 Glove, infersent, universal sentence encoder SBERT를 비교했을 때 1초당 얼마나 많은 문장을 처리할 수 있는지 나타낸 표이다. CPU에서는 InferSent보다 느리지만 GPU에서 더 효율성을 얻은 transformer network 이 더 뛰어나다. (오른쪽 그림은 infersent network 의 architecture)

 

 

 

 

 

 

 

논문 : https://arxiv.org/pdf/1908.10084.pdf

 

SentenceTransformers Documentation — Sentence-Transformers documentation (sbert.net)