以前と同じリンク内容の内容なのですが
#Doc2Vecで文書を学習させるコードを書いていこう。まずは必要ライブラリをimportする。 import sys import glob import traceback from os import listdir, path from pyknp import Jumanpp from gensim import models from gensim.models.doc2vec import LabeledSentence #次に、記事ファイルをダウンロードしたディレクトリから取得する関数を定義する。 def corpus_files(base_dir): findPath = path.join( base_dir, 'text/**/*.txt') # 「text」まで呼出元で指定させるほうがスマートだが paths = glob.glob( findPath,recursive=True) docs = [] ignore = ('LICENSE.txt','README.txt','CHANGES.txt') for p in paths: if path.basename(p) not in ignore: docs.append(p) return docs #そして、記事コンテンツをパスから取得する関数を定義する。 def read_document(path): with open(path, 'r',encoding="utf-8") as f: return f.read() #先程インストールした、JUMAN++を使って記事を単語リストに変換する関数を定義しよう。 def split_into_words(text): result = Jumanpp().analysis(text) return [mrph.midasi for mrph in result.mrph_list()] #次に、記事コンテンツを単語に分割して、Doc2Vecの入力に使うLabeledSentenceに変換する関数を定義しよう。 def doc_to_sentence(doc, name): words = split_into_words(doc) return LabeledSentence(words=words, tags=[name]) #これらの関数を組み合わせて、記事のパスリストから、記事コンテンツに変換し、単語分割して、センテンスのジェネレーターを返す関数を定義する。 def corpus_to_sentences(corpus): docs = [read_document(x) for x in corpus] for idx, (doc, name) in enumerate(zip(docs, corpus)): print('\rPreprocessing {}/{}'.format(idx, len(corpus))) try: yield doc_to_sentence(doc, name) except: print( traceback.format_exc() ) finally: print( 'end' ) #最後に、Doc2Vecパラメータを渡して、学習させよう。 print( corpus_files('./') ) corpus = corpus_files('./') sentences = corpus_to_sentences(corpus) model = models.Doc2Vec(sentences, dm=0, size=300, window=15, alpha=.025, min_alpha=.025, min_count=1, sample=1e-6) print('STRART') for epoch in range(20): print('Epoch: {}'.format(epoch + 1)) model.train(sentences) model.alpha -= (0.025 - 0.0001) / 19 model.min_alpha = model.alpha #ここで、デフォルトで設定されているdmに1を設定するとdmpvで学習されることになる。1以外であれば、DBoWで学習される。 #モデルの保存と読み込みは以下のようにして、saveメソッドとloadメソッドにファイル名を指定する。 model.save(r'C:/cygwin64/home/hoge/doc2vec.model') model = models.Doc2Vec.load('doc2vec.model') print('END')
このコードの部分で途中で止まってしまいます。
def corpus_to_sentences(corpus): docs = [read_document(x) for x in corpus] for idx, (doc, name) in enumerate(zip(docs, corpus)): print('\rPreprocessing {}/{}'.format(idx, len(corpus))) try: yield doc_to_sentence(doc, name) except: print( traceback.format_exc() ) finally: print( 'end' )
合っているかわかりませんが、一応エラーが出たのかを確認するため、tryで囲んで、printで出力するようにしたのですが、errorは特に出力されずに途中まで実行した後止まって動かなくなります
タスクマネージャーでプロセスが動いているか確認しましたが、CPU使用率やディスク使用率も100%で動かなくなっているということもなく通常の状態でした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/04 09:48
2018/01/04 09:53
2018/01/06 03:26
2018/01/06 05:57