워드임베딩으로 유사도를 구할 때 유의사항

몽뭉뭉·2023년 12월 31일
1

koala 프로젝트

목록 보기
2/7
post-thumbnail

개요

지금 하고 있는 프로젝트가 논문의 참고문헌을 쉽게 찾아주는 챗봇을 만드는 프로젝트이다. 과정은 논문을 작성할 때 쓰고자 하는 문장을 입력하면 기존의 데이터베이스에 있는 논문과 해당 문장 간의 유사도를 구해서 가장 높은 순으로 논문을 알려주는 것이다.


처음엔 당연히 pre-trained 모델로 임베딩을 하면 이미 사전 훈련이 어느정도 되어있으니 의미파악까지 어느정도 한다고 생각했다. 따라서 무겁더라도 성능이 좋은 모델을 사용하고자 했다. 이 글 을 참고해서 테스트를 진행해보니 이는 착각이었다는 것을 깨달았다.

아래는 시도한내용이다.

[사용한 문장]

sentence1=["I enjoy hiking in the mountains."]
sentence2=["Mountain treks appeal to me."]
sentence3=["I dislike hiking in the mountains."]

문장을 살펴보면 1번과 2번은 유사한 내용이지만 구조가 다름을 알 수 있다.
반면 2번과 3번은 내용은 다르지만 구조가 유사함을 알 수 있다.

임베딩 모델 변경에 따른 결과이다.



사용모델: jinaai/jina-embeddings-v2-small-en

해당 모델은 이 글을 쓴 시점에 hugging face leader board 41위에 해당하는 모델이다. 이 모델은 sequence length 가 8192로 길기 때문에 사용했다.

The text embedding set trained by Jina AI, Finetuner team.

위를 통해 살펴본 결과는 해당 모델은 1번과 3번이 더 유사하고 보았다. 결국 문장의 구조가 우선 시 된 것이다.

사용모델: jinaai/jina-embeddings-v2-base-en

해당 모델은 small 모델보다 더 많은 데이터를 학습시켰다. 처음에 이 모델을 사용하다 너무 모델이 무거워서 small모델을 사용하여 임베딩을 진했다. 따라서 궁금증으로 이 모델로 바꿔서 테스트를 진행했다.

결과는 놀랍게도 base 모델은 1번과 2번이 더 유사하다고 보았다. 해당 모델은 미약한 차이이긴 하지만 의미를 더 중요하게 생각했다.

사용모델:all-mpnet-base-v2

해당 모델은 sentence bert모델로 문장의 의미론적 추론을 개선 시킨 모델이라고 한다. 이 내용은 알고 있었지만 jina모델이 훨씬 최신 모델이기 때문에 의미론적 추론도 당연히 되고 이 모델보다 훨씬 좋을 것이라고 생각했다.


결과를 살펴보면 확실하게 의미론적 추론을 하고 있는 것을 알 수 있다.


다음은 우리 프로젝트에 좀 더 적합하게 긴 텍스트로 문장을 구성한 후 비교해 보았다. 해당 텍스트는 chat gpt에게 만들어 달라했다

[chat gpt에게 요구한 사항]
1. 1번과 2번의 의미가 유사하되 사용하는 단어는 다르게 바꿔주고 1번과 3번은 의미는 상반되는 단어가 유사하고 구조도 유사하게 바꿔줘
2. 해당 문장들의 길이를 세문장 정도 추가해서 더 길게 만들어줘3.1번과 3번의 사용되는 단어와 문장의 구조가 더 유사하게 바꿔줘

[만들어준 문장]

sentence1="I find solace in traversing forest trails, where the rustling leaves and chirping birds create a symphony of nature. The quietude and the lush greenery of the wilderness rejuvenate my spirit and prepare me for the challenges ahead. These weekend escapades into the natural world have become my sanctuary, a haven where I disconnect from the noise and pace of daily life. Walking among the towering trees, I feel a deep connection with the earth. The tranquility of the forest washes over me, bringing a sense of peace and contentment."

sentence2="I am drawn to the solitude of woodland paths, where the whispers of nature speak louder than the noise of the city. Exploring the untamed outdoors during my leisure time invigorates my soul and gets me ready for new challenges. These adventures in the wild are my retreat, a chance to immerse myself in the beauty and serenity of the natural world. Each step on the forest floor is a step away from the stresses of everyday life. In the embrace of the woods, I find clarity and a renewed sense of purpose."

sentence3="I find solace in navigating city streets, where the hustle and bustle of urban life create a symphony of activity. The vibrancy and the diverse tapestry of the city rejuvenate my spirit and prepare me for the challenges ahead. These weekend escapades into the urban jungle have become my sanctuary, a haven where I connect with the energy and pace of city life. Walking among the towering buildings, I feel a deep connection with the pulse of the city. The dynamism of the urban landscape washes over me, bringing a sense of excitement and contentment."


사용모델: jinaai/jina-embeddings-v2-small-en


짧은 문장만 사용했을 때는 1번과 3번을 더 유사하게 보았는데 문장을 추가하니 small 모델도 1번과 2번이 유사하다고 보았다.

사용모델: jinaai/jina-embeddings-v2-base-en

base모델은 오히려 small모델 보다 1번과 2번의 유사도를 더 적게 보았고 1번과 3번의 유사도를 더 높게 보았다.

사용모델:all-mpnet-base-v2


sbert모델은 위의 두 모델들과 달리 구조보다 의미적으로 유사성을 더 높게 보았고 위의 두 모델과 달리 유사도의 차이가 컸다.


결론

워드 임베딩을 할 때 의미적인 요소가 들어갈 것이라 생각했지만 좋은 모델을 사용했음에도 의미적인 요소는 크게 반영되지 않음. 문장의 구조가 임베딩 성능에 있어서 상당부분 차지함을 알 수 있다.

이처럼 의미적인 요소를 고려하여 유사도를 구할 때는 sentence bert모델을 사용하는게 훨씬 효과적임을 알 수 있다. 또 코사인 유사도와 같은 유사도 알고리즘이 사람이 생각하는 "유사" 라는 개념과 다르게 작용함을 알 수 있다. 사람이 문장이 유사하다고 할 때는 의미가 먼저 떠오르기 마련인데 유사도 알고리즘은 그렇지 못하다.

이처럼 문장의 의미를 비교하는 task를 수행할 때는sentence transformer 계열의 모델을 Contrastive learning 방식에 해당하는 SimCSE 등을 사용해 파인튜닝하고, 그 모델을 사용하여 임베딩 벡터를 생성해 코사인 유사도를 구하는 것이 효과적이다.

에전에 적은 fine tuning 꿀팁 글에서 적은게 드디어 이해가 가는 순간이다...

그럼 프로젝트를 갈아 엎게 되서 다시 프로젝트 하러 가야겠다...

0개의 댓글