일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- KeyBert
- 자연어 모델
- Tableu
- SBERT
- Roberta
- 개체명 인식
- 붕괴 스타레일
- 블루 아카이브
- 피파온라인 API
- BERTopic
- 다항분포
- 클래스 분류
- 원신
- 데이터넥스트레벨챌린지
- 코사인 유사도
- geocoding
- 토픽 모델링
- 블루아카이브 토픽모델링
- 옵티마이저
- 트위치
- 문맥을 반영한 토픽모델링
- 조축회
- 구글 스토어 리뷰
- 데이터리안
- NLP
- 데벨챌
- 포아송분포
- Optimizer
- LDA
- CTM
- Today
- Total
분석하고싶은코코
NLP - CTM(Contextualized Topic Models) 토픽 모델링 본문
토픽 모델링 방법 중 하나인 CTM에 대해서 다뤄보겠습니다.
CTM 레퍼런스 : https://contextualized-topic-models.readthedocs.io/en/latest/introduction.html
Contextualized Topic Models — Contextualized Topic Models 2.5.0 documentation
Contextualized Topic Models (CTM) are a family of topic models that use pre-trained representations of language (e.g., BERT) to support topic modeling. See the papers for details: Topic Modeling with Contextualized Embeddings Our new topic modeling family
contextualized-topic-models.readthedocs.io
Contextualized Topic Models는 문맥을 반영한 모델링을 이야기합니다. 문맥을 반영한 토픽 모델링은 BERT의 문서 임베딩의 표현력과 기존 토픽 모델의 비지도 학습 능력을 결합하여 문서에서 주제(토픽)을 가져오는 모델을 이야기합니다. CTM에서는 Combine-Text-Model과 ZeroShot-Text-Model 두 가지가 있습니다. CobineTM은 상황별 임베딩과 오래된 단어 모음을 결합하여 보다 일관된 주제를 만드는 모델이고, ZeroShotTM은 테스트 데이터에 누락된 단어가 있을 수 있는 작업에 대한 완벽한 주제 모델이라고 소개하고 있습니다.
CTM에서 성능에 가장 큰 영향을 주는 것은 전처리라고 소개하고 있습니다. 즉, 다른 단어여도 같은 뜻을 갖는 단어이거나 특수문자가 붙어서 다른 단어로 인식하지 않게 해주는 정규화 작업이 중요하다고 말하고 있습니다. 또한 단어 집합의 크기는 2,000개 이내에서 좋은 성능을 보인다고 설명하고 있습니다. 그래서 기본적인 모듈에서 제공해주는 Vocab크기는 2,000이 default입니다.
from contextualized_topic_models.models.ctm import CombinedTM, ZeroShotTM
from contextualized_topic_models.utils.data_preparation import TopicModelDataPreparation, bert_embeddings_from_list
from contextualized_topic_models.utils.preprocessing import WhiteSpacePreprocessing
from sklearn.feature_extraction.text import CountVectorizer
from konlpy.tag import Mecab
import pandas as pd
df = pd.read_csv('reivews_df_preprocssing_ver.csv')
documents = df['content'].values
b_a_documnets = df[df['app_name']=='블루아카이브']['content'].values
preprocessed_documents = []
for line in tqdm(b_a_documnets):
# 빈 문자열이거나 숫자로만 이루어진 줄은 제외
if line and not line.replace(' ', '').isdecimal():
preprocessed_documents.append(line)
class CustomTokenizer:
def __init__(self, tagger):
self.tagger = tagger
def __call__(self, sent):
word_tokens = self.tagger.nouns(sent)
result = [word for word in word_tokens if len(word) > 1]
return result
custom_tokenizer = CustomTokenizer(Mecab())
vectorizer = CountVectorizer(tokenizer=custom_tokenizer, max_features=2000)
train_bow_embeddings = vectorizer.fit_transform(preprocessed_documents)
vocab = vectorizer.get_feature_names_out()
id2token = {k: v for k, v in zip(range(0, len(vocab)), vocab)}
MODEL_NAME = "sentence-transformers/xlm-r-100langs-bert-base-nli-stsb-mean-tokens"
train_contextualized_embeddings = bert_embeddings_from_list(preprocessed_documents, MODEL_NAME)
qt = TopicModelDataPreparation()
training_dataset = qt.load(train_contextualized_embeddings, train_bow_embeddings, id2token)
# ctm_combine = CombinedTM(bow_size=len(vocab), contextual_size=768, n_components=10, num_epochs=20)
# ctm_combine.fit(training_dataset)
ctm_zeroshot = ZeroShotTM(bow_size=len(vocab), contextual_size=768, n_components=10, num_epochs = 20)
ctm_zeroshot.fit(training_dataset)
# ctm_combine.get_topics(8)
ctm_zeroshot.get_topics(8)
관련 레퍼런스
ZeroShot? - https://velog.io/@nomaday/n-shot-learning
'머신러닝&딥러닝 > NLP' 카테고리의 다른 글
모바일 4가지 게임 리뷰 토픽 모델링 분석(1) (0) | 2023.10.10 |
---|---|
NLP - 버토픽(BERTopic) (1) | 2023.10.10 |
NLP - KeyBert 토픽 모델링 (1) | 2023.10.09 |
NLP - 한국어 자연어 모델(Korean Language Model) (1) | 2023.10.06 |
NLP - SBERT(Sentence-Bert) (0) | 2023.10.05 |