Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KoBERT를 이용한 유사도 분석 #24

Closed
HyeyeonKoo opened this issue Jul 7, 2020 · 5 comments
Closed

KoBERT를 이용한 유사도 분석 #24

HyeyeonKoo opened this issue Jul 7, 2020 · 5 comments
Labels
question Further information is requested

Comments

@HyeyeonKoo
Copy link

HyeyeonKoo commented Jul 7, 2020

안녕하세요.

KoBERT에 이미 구축된 임베딩을 이용해 문장 간 유사도 분석을 하고 싶습니다.
인터넷을 사용할 수 없는 상황이라 다음과 같이 model, vocab을 로드해서 사용하고 있습니다.

model, vocab = get_kobert_model(u'...\KoBERT\pytorch_kobert_2439f391a6.params',
                               u'...\KoBERT\kobert_news_wiki_ko_cased-1087f8699e.spiece',
                               'cpu')

분류 예제를 보아도 특정 문장을 벡터화 할 수 있는 부분을 찾기가 어려워서 글을 남깁니다.
문장을 벡터화하고 유사도를 비교할 방법이 있을까요?

@haven-jeon
Copy link
Collaborator

haven-jeon commented Jul 7, 2020

네..

https://github.com/SKTBrain/KoBERT/blob/master/scripts/NSMC/naver_review_classifications_pytorch_kobert.ipynb 에서 아래 코드 부분을 참고하시면 될거 같습니다.

_, pooler = self.bert(input_ids = token_ids, token_type_ids = segment_ids.long(), attention_mask = attention_mask.float().to(token_ids.device))

pooler를 문장 벡터라 볼 수 있습니다.

이렇게 도출된 벡터간의 유사도를 보려면... 코사인 유사도 정도 사용하면 될듯 합니다.

@haven-jeon haven-jeon added the question Further information is requested label Jul 7, 2020
@HyeyeonKoo
Copy link
Author

답변 감사합니다.
그래도 이해가 명확히 되지 않아서 한 번 더 답변을 부탁드립니다.

말씀해주신 pooler를 만들기 위해서는 token_ids, token_type_ids, attention_mask가 필요합니다.
attention_mask의 경우 함수가 정의되어 있지만 다른 부분은 어떻게 생성하는지 잘 이해가 되지 않습니다.

README에서 How to Use의 Using with PyTorch 부분을 참고했을 때에도 sequence_output, pooled_output을 얻기 위해 input_ids, input_mask, token_type_ids세가지를 만들어야 하는데, 예제에서는 임의로 만든 것으로 보입니다.

sequence_output, pooled_output = model(input_ids, input_mask, token_type_ids)

How to Use의 예제 결과처럼 "안녕"과 같은 특정 문장의 벡터를 보려면 input_ids, input_mask, token_type_ids 세 가지 인자를 어떻게 만들어야 하나요?

@haven-jeon
Copy link
Collaborator

노트북 BERTDataset에서 nlp.data.BERTSentenceTransform 를 사용하시면 됩니다. 전반적으로 하시고자 하는 작업은 노트북의 코드를 잘 분석해보시면 쉽게 가능하실거라 생각되네요.

@HyeyeonKoo
Copy link
Author

그렇군요. 감사합니다!

@Haebuk
Copy link

Haebuk commented Sep 23, 2021

안녕하세요.
동일한 task를 진행중인데, 현재 제 데이터에는 label이 없습니다.
KoBERT를 통해 마치 Word2Vec처럼 문장을 입력했을 때 유사도를 계산할 수 있는 임베딩 벡터로 바꿔주고 싶은데
어떻게 하면 좋을까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants