LDA 모델 학습 및 구현

개요

본 항목에서는 데이터 크롤러로 수집된 세종대학교와 관련된 모든 문서에 대하여 학습을 수행하고 토픽을 예측할 LDA 모델의 구현 과정에 대하여 서술한다.

학습 과정 명세

HyperParameter

LDA 모델을 학습시키기 위한 하이퍼 파리미터는 다음과 같다.

지속적으로 수정 예정

Corpus 변환 과정

LDA 모델이 학습할 수 있도록 DB 내에 존재하는 모든 문서 데이터를 Copus로 변환하는 작업을 수행해야 한다.

DB 내의 데이터에 대하여 전처리 과정 수행

각 문서를 코퍼스로 변환하기 전에 여러 조건을 검토하여 해당 데이터가 학습 모델에 적용시킬지에 대한 전처리 과정을 수행한다.

for post in posts:
		# 코퍼스 전처리 조건
		if post['info'].startswith("everytime") and len(post['post']) < EVERY_POST_LIMIT:
			continue
		if post['info'].startswith("navercafe") and len(post['title']) < NAVER_TITLE_LIMIT:
			continue
		if len(post['title'] + post['post']) < TOTAL_POST_LIMIT:
			continue
		if post['info'] in ["everytime_은밀한","main_bidding","everytime_끝말잇기 ", "everytime_퀴어 ","everytime_애니덕후 "]:
			continue
		if post['info'].startswith("everytime") and coll.find({"info":post['info']}).count() < 500:
			continue
		#
		token = post['token'] + post['tag']
		temp = pd.DataFrame({"text":[token]})
		df = df.append(temp, ignore_index = True)

확정된 문서를 바탕으로 코퍼스 변환 작업을 수행한다. 반환된 게시물의 토큰에 대하여 새로운 토큰을 모델의 딕셔너리에 추가하고, 해당 토큰을 딕셔너리 식별 값으로 변환하여 코퍼스를 생성한다.

df = get_posts_df(col, idx, split_doc, update)
tokenized_doc = df['text']
dictionary.add_documents(tokenized_doc)
corpus += [dictionary.doc2bow(text) for text in tokenized_doc]

학습

수행된 코퍼스와 사전에 결정된 하이퍼 파라미터 인자를 사용하여 LDA 학습을 수행한다.

ldamodel = LdaMulticore(
				corpus,
				num_topics = num_topics,
				id2word = dictionary,
				passes = PASSES,
				workers = WORKERS,
				iterations = ITERATION
				)

소스 코드 배포

실행 요구사항

Last updated