본문 바로가기

프로그래밍/AI:ML:DL

[GENSIM] "You must specify either total_examples or total_words, for proper job parameters updation

반응형

간단한 gensim  doc2vec 코드를 실행하다가 다음과 같은 에러 메시지가 발생했다.

"You must specify either total_examples or total_words, for proper job parameters updation"

ValueError: You must specify either total_examples or total_words, for proper job parameters updationand progress calculations. The usual value is total_examples=model.corpus_count.


실행했던 코드는, 2015년 파이콘 코리아에서 lucypark 이 발표한 슬라이드 안의 코드로 ( https://www.lucypark.kr/docs/2015-pyconkr/#60 ) 코드는 다음과 같다.

# 사전 구축

doc_vectorizer = doc2vec.Doc2Vec(size=300, alpha=0.025, min_alpha=0.025, seed=1234)

doc_vectorizer.build_vocab(tagged_train_docs)


# Train document vectors!

for epoch in range(10):

    doc_vectorizer.train(tagged_train_docs)

    doc_vectorizer.alpha -= 0.002  # decrease the learning rate

    doc_vectorizer.min_alpha = doc_vectorizer.alpha  # fix the learning rate, no decay


코드 중에서도 doc_vectorizer.train 부분에서 total_examples 등의 추가 인자가 없어서 발생하는 에러인 것으로 보인다. 에러가 발생한 gensim 의 버전은 3.4.0 이다. 버전업 되면서 인자나 사용법이 변경된 것 같다.


구글에서 검색하여 보니, gensin github의 issue 에 비슷한 이슈 글타래가 검색되었고, 다음과 같은 수정사항을 따라하여 문제가 해결됐다.


https://github.com/RaRe-Technologies/gensim/issues/1956#issuecomment-371020349

@Keramatfar this is not a problem that your data in Persian, please share all.csv file (this needed for reproducing your error).

I already see several mistakes (looks like "old" approach")

model = gensim.models.Doc2Vec(size=300, window=10, min_count=5, workers=11,alpha=0.025, min_alpha=0.025) # use fixed learning rate model.build_vocab(it) for epoch in range(10): model.train(it, epochs=model.iter, total_examples=model.corpus_count) model.alpha -= 0.002 # decrease the learning rate model.min_alpha = model.alpha # fix the learning rate, no deca model.train(it)

should be

model = gensim.models.Doc2Vec(size=300, window=10, min_count=5, workers=11,alpha=0.025, min_alpha=0.025, iter=20) model.build_vocab(it) model.train(it, epochs=model.iter, total_examples=model.corpus_count)


코드를 보면, 위쪽에 잘못되었다고 하는 코드가 2015년 슬라이드에 있는 코드와 거의 일치한다. for 문으로 이터레이션하며 여러번 train 메소드를 호출하던 것을 train 메소드 안으로 숨겨버린 것 같다. 이터레이션 횟수는 Doc2Vec 생성자 인자로 들어간 것 같고.

수정된 코드로 잘 동작하였다.



728x90