새소식

반응형
AI/models

KeyBERT

  • -
728x90
반응형

KeyBERT 란?

KeyBERT is a minimal and easy-to-use keyword extraction technique that leverages BERT embeddings to create keywords and keyphrases that are most similar to a document.

 

  • 문서를 가장 잘 나타내는 키워드 또는 키구문을 찾아주는, 쉽게 사용 가능한 BERT-based 모델
    1. BERT로 문서 단위의 표현 추출 (document-embeddings)
    2. N-gram 단어와 구문을 위해 word embeddings
    3. cosine similarity 로 문서와 가장 유사한 단어/구문 찾기
  • MIT License 로 오픈소스 파이썬 모듈

 

Embedding Models

  • 임베딩 모델을 지정할 수 있다. (전체 모델)
  • English default model : all-MiniLM-L6-v2
  • multi-lingual documents 추천 모델 : paraphrase-multilingaul-MinLM-L12-v2
  • Flair / Spacy / Gensim / USE 등 가능 (스크립트 확인)

 

Getting Started

1) Installation

pip install keybert

 

2) Usage

from keybert import KeyBERT

doc = """
         Supervised learning is the machine learning task of learning a function that
         maps an input to an output based on example input-output pairs. It infers a
         function from labeled training data consisting of a set of training examples.
         In supervised learning, each example is a pair consisting of an input object
         (typically a vector) and a desired output value (also called the supervisory signal).
         A supervised learning algorithm analyzes the training data and produces an inferred function,
         which can be used for mapping new examples. An optimal scenario will allow for the
         algorithm to correctly determine the class labels for unseen instances. This requires
         the learning algorithm to generalize from the training data to unseen situations in a
         'reasonable' way (see inductive bias).
      """
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(doc, 
					keyphrase_ngram_range=(1, 1), 
                    stop_words='english'
                    highlight=True,
                    use_maxsum=True,
                    nr_candidates=20,
                    top_n=5,
                    use_mmr=True,
                    diversity=0.7
           )

 

  • keyphrase_ngram_range : 키워드/키구문의 길이 세팅 (click!)
    kw_model.extract_keywords(doc, keyphrase_ngram_range=(1, 1), stop_words=None)
              [('learning', 0.4604),
               ('algorithm', 0.4556),
               ('training', 0.4487),
               ('class', 0.4086),
               ('mapping', 0.3700)]
    kw_model.extract_keywords(doc, keyphrase_ngram_range=(1, 2), stop_words=None)
              [('learning algorithm', 0.6978),
               ('machine learning', 0.6305),
               ('supervised learning', 0.5985),
               ('algorithm analyzes', 0.5860),
               ('learning function', 0.5850)]
  • stop_words : 불용어
  • highlight : 문서 내에서 키워드 highlight 기능 (click!)

 

  • use_maxsum : 결과 다양성을 위한 파라미터
    • 코사인 유사도로 유사도를 계산할 때 중복 키워드가 많을 수 있는데 이 점을 해결하기 위한 방법이다.
    • 2 x top_n 단어에서 모든 top_n 조합 중 코사인 유사도에 의해 서로 가장 유사하지 않은 조합 추출
    • => 키워드 - 키워드 끼리의 거리는 최대로 해서 의미적으로 다양한 키워드를 추출한다.
  • use_mmr :  결과 다양성을 위한 파라미터
    • maximal marginal relevance
    • 코사인 유사도로 유사도를 계산할 때 중복 키워드가 많을 수 있는데 이 점을 해결하기 위한 방법이다.
    • MMR 은 문서와 가장 유사한 키워드를 선택하고, 문서와 비슷하면서 이미 선택한 키워드와 비슷하지 않은 새 키워드를 반복적으로 선택하는 방법이다.

 

 

References

 

반응형

'AI > models' 카테고리의 다른 글

Whisper  (0) 2023.06.10
Text-To-Speech models  (0) 2023.05.09
Vicuna : ChatGPT 90% 성능을 가진 오픈 소스 챗봇  (0) 2023.05.06
Bark : 트랜스포머 기반 text-to-audio 모델  (0) 2023.04.27
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.