bab2min / tomotopy

Python package of Tomoto, the Topic Modeling Tool
https://bab2min.github.io/tomotopy
MIT License
548 stars 62 forks source link

DTModel에서 coherence 구하기 #164

Closed Kwon-subin closed 1 year ago

Kwon-subin commented 2 years ago

안녕하세요 저는 현재 한국의 대학교에서 학부생으로 재학 중에 있습니다. 지식이 부족한 탓에 예제 코드를 엄청 참고해서 하고있는데, tomotopy 로 Dynamic Topic Modeling을 하려고합니다. 그런데, DTM 으로 학습된 모델로 coherence를 계산하려고 하니, timepoint 값이 없다고 나와서 제가 직접 coherence.py 파일의 해당 라인에 timepoint를 야매로 직접 입력하여 넘겼는데 또 다음과 같은 ValueError 가 뜹니다. 오류는 coherence.py 파일에서 발생했습니다.

c.append(super().getscore((w for w, in self._topic_model.get_topic_words(k,timepoint=13, top_n=self._top_n)))) ValueError: must topic_id < t

파라미터 중 timepoint = 13은 제가 직접 입력한 값입니다.

혹시 여기서 더 어떻게 해야할까요?? 그리고 DTM 모델로 coherence 값을 이렇게 계산해도 되는건가요..?

bab2min commented 2 years ago

안녕하세요 @Kwon-subin DTM 모델은 시간에 따라 토픽이 변하는 모델입니다. 따라서 토픽의 분포는 시간의 영향을 받으므로, 특정 토픽의 단어 분포를 구하려면 그 시간도 함께 명시해주어야 합니다.

https://github.com/bab2min/tomotopy/blob/d30964ce0610a5e34d3645cfc8c26d99536cac03/tomotopy/coherence.py#L148-L158

그런데 제보해주신것처럼 현재 Coherence 클래스에서는 topic_id만 입력받고 따로 timepoint를 처리하지 않아서 문제가 생기고 있네요~ 이 부분은 추후에 패치가 필요할 것으로 보이네요.

일단 패치 전에 돌려보시려면

    for k in range(self._topic_model.k):
        for t in range(self._topic_model.num_timepoints):
            c.append(super().get_score((w for w, _ in self._topic_model.get_topic_words(k, timepoint=t, top_n=self._top_n))))
    return sum(c) / len(c)

와 같이 수정하시면 되겠습니다. 이렇게 하면 DTM 모델 내의 각 토픽의 모든 시점에 대해 각각 coherence를 구하고 이를 평균낸 값을 반환해줍니다.