bab2min / tomotopy

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

DTM model의 DTModel.make_doc과 DTModel.infer에 관한 질문 #184

Closed tkddnd0214 closed 1 year ago

tkddnd0214 commented 1 year ago

안녕하십니까. tomotopy의 dtm모델 학습시킨 후, 해당 모델에 새로운 문서를 추가해 분석해보는 과정에서 에러가 나서 문의드립니다.

새로운 문건(아래 코드 temp) 한 건을 모델에 집어넣은 후, topic_dist 등을 확인해 보고자 'DTModel.infer' Method를 사용하였는데, tomotopy.Document type의 형태로 'infer' Method의 인자로 사용하였는데도 tomotopy.Document type or list of tomotopy.Document을 인자로 사용하라는 에러 문구가 나와 문의드립니다. 해결방안이 있을지 문의드립니다. (그리고 make_doc에서 여러건의 문건을 한번에 넣을려면 데이터 형식을 어떻게 구성해야 하는지도 알려주시면 좋겠습니다.)

코드와 그에 따른 결과문은 아래와 같습니다.

답변 부탁드리겠습니다. 감사합니다.

-Code-

temp

-Result-

['모바일',
 '클라우드',
 '컴퓨팅', ··· ]

-Code-

doc_inst = mdl.make_doc(temp,temp_timepoint[0])
mdl.infer(doc_inst)

-Result-

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Input In [58], in <cell line: 1>()
----> 1 mdl.infer(doc_inst)

Exception: `doc` must be tomotopy.Document type or list of tomotopy.Document

-Code-

doc_inst.__class__

-Result-

tomotopy.Document
tkddnd0214 commented 1 year ago

혹시 infer 인자중에 transform 이라는 항목이 있던데 dtm 모델에 맞는 transform을 해야하는 것일까요?

bab2min commented 1 year ago

안녕하세요 @tkddnd0214 , 답변이 늦었네요. make_doc을 사용하고 infer를 사용하면 잘 작동되어야하는게 맞습니다.

>>> doc_inst = mdl.make_doc(["word1", "word2", "word3"], timepoint=0)
>>> topic_dist, ll = mdl.infer(doc_inst)
>>> topic_dist
array([0.025, 0.275, 0.275, 0.025, 0.025, 0.025, 0.025, 0.025, 0.025,
       0.275], dtype=float32)
>>> ll
-36.409915924072266

위와 같이 에러가 뜬다면 뭔가 doc_inst를 생성하는 과정에서 문제가 생겼을것 같은데요, 지금 알려주신 코드만으로는 무엇이 원인인지 알기 어렵네요. 괜찮으시다면 위 에러를 재현가능한 코드를 공유해주시면 조사해보도록 하겠습니다.

그리고 make_doc은 문헌 하나를 생성하는것이기 때문에, 여러 문헌을 생성하려고 하면 반복문을 통해 여러 번 make_doc을 호출하셔야 합니다.

doc_insts = [mdl.make_doc(words) for words in list_of_words]
tkddnd0214 commented 1 year ago

안녕하십니까, @bab2min 답변 보면서 다시 작업해보던 와중 mdl.infer(doc_inst) 를 mdl.infer([doc_inst])로 수정하니 작동이 되었습니다. 친절한 답변 감사드립니다.