【发布时间】:2020-06-13 10:38:34
【问题描述】:
我正在尝试在标记文档上训练 Gensim Doc2Vec 模型。我有大约 4000000 个文件。以下是我的代码:
import pandas as pd
import multiprocessing
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
import logging
from tqdm import tqdm
from gensim.models import Doc2Vec
from gensim.models.doc2vec import TaggedDocument
import os
import re
def text_process(text):
logging.basicConfig(format="%(levelname)s - %(asctime)s: %(message)s", datefmt='%H:%M:%S', level=logging.INFO)
stop_words_lst = ['mm', 'machine', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'first', 'second', 'third', 'plurality', 'one', 'more', 'least', 'at', 'example', 'memory', 'exemplary', 'fourth', 'fifth', 'sixth','a', 'A', 'an', 'the', 'system', 'method', 'apparatus', 'computer', 'program', 'product', 'instruction', 'code', 'configure', 'operable', 'couple', 'comprise', 'comprising', 'includes', 'cm', 'processor', 'hardware']
stop_words = set(stopwords.words('english'))
temp_corpus =[]
text = re.sub(r'\d+', '', text)
for w in stop_words_lst:
stop_words.add(w)
tokenizer = RegexpTokenizer(r'\w+')
word_tokens = tokenizer.tokenize(text)
lemmatizer= WordNetLemmatizer()
for w in word_tokens:
w = lemmatizer.lemmatize(w)
if w not in stop_words:
temp_corpus.append(str(w))
return temp_corpus
chunk_patent = pd.DataFrame()
chunksize = 10 ** 5
cores = multiprocessing.cpu_count()
directory = os.getcwd()
for root,dirs,files in os.walk(directory):
for file in files:
if file.startswith("patent_cpc -"):
print(file)
#f=open(file, 'r')
#f.close()
for chunk_patent_temp in pd.read_csv(file, chunksize=chunksize):
#chunk_patent.sort_values(by=['cpc'], inplace=True)
#chunk_patent_temp = chunk_patent_temp[chunk_patent_temp['cpc'] == "G06K7"]
if chunk_patent.empty:
chunk_patent = chunk_patent_temp
else:
chunk_patent = chunk_patent.append(chunk_patent_temp)
train_tagged = chunk_patent.apply(lambda r: TaggedDocument(words=text_process(r['text']), tags=[r.cpc]), axis=1)
print(train_tagged.values)
if os.path.exists("cpcpredict_doc2vec.model"):
doc2vec_model = Doc2Vec.load("cpcpredict_doc2vec.model")
doc2vec_model.build_vocab((x for x in tqdm(train_tagged.values)), update=True)
doc2vec_model.train(train_tagged, total_examples=doc2vec_model.corpus_count, epochs=50)
doc2vec_model.save("cpcpredict_doc2vec.model")
else:
doc2vec_model = Doc2Vec(dm=0, vector_size=300, min_count=100, workers=cores-1)
doc2vec_model.build_vocab((x for x in tqdm(train_tagged.values)))
doc2vec_model.train(train_tagged, total_examples=doc2vec_model.corpus_count, epochs=50)
doc2vec_model.save("cpcpredict_doc2vec.model")
我曾尝试修改 Doc2vec 参数,但没有任何运气。
在相同的数据上,我训练了 Word2vec 模型,与 doc2vec 模型相比,它要准确得多。此外,word2vec 模型的“most_similar”结果与 doc2vec 模型非常不同。
以下是搜索最相似结果的代码:
from gensim.models import Word2Vec
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
import logging
from gensim.models import Doc2Vec
import re
def text_process(text):
logging.basicConfig(format="%(levelname)s - %(asctime)s: %(message)s", datefmt='%H:%M:%S', level=logging.INFO)
stop_words_lst = ['mm', 'machine', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'first', 'second', 'third', 'example', 'memory', 'exemplary', 'fourth', 'fifth', 'sixth','a', 'A', 'an', 'the', 'system', 'method', 'apparatus', 'computer', 'program', 'product', 'instruction', 'code', 'configure', 'operable', 'couple', 'comprise', 'comprising', 'includes', 'cm', 'processor', 'hardware']
stop_words = set(stopwords.words('english'))
#for index, row in df.iterrows():
temp_corpus =[]
text = re.sub(r'\d+', '', text)
for w in stop_words_lst:
stop_words.add(w)
tokenizer = RegexpTokenizer(r'\w+')
word_tokens = tokenizer.tokenize(text)
lemmatizer= WordNetLemmatizer()
for w in word_tokens:
w = lemmatizer.lemmatize(w)
if w not in stop_words:
temp_corpus.append(str(w))
return temp_corpus
model = Word2Vec.load("cpc.model")
print(model.most_similar(positive=['barcode'], topn=30))
model1 = Doc2Vec.load("cpcpredict_doc2vec.model")
pred_tags = model1.most_similar('barcode',topn=10)
print(pred_tags)
进一步,上述的输出引用如下:
[('indicium', 0.36468246579170227), ('symbology', 0.31725651025772095), ('G06K17', 0.29797130823135376), ('dataform', 0.29535001516342163), ('rogue', 0.29372256994247437), ('certification', 0.29178398847579956), ('reading', 0.27675414085388184), ('indicia', 0.27346929907798767), ('Contra', 0.2700084149837494), ('redemption', 0.26682156324386597)]
[('searched', 0.4693435728549957), ('automated', 0.4469209909439087), ('production', 0.4364866018295288), ('hardcopy', 0.42193126678466797), ('UWB', 0.4197841286659241), ('technique', 0.4149003326892853), ('authorized', 0.4134449362754822), ('issued', 0.4129987359046936), ('installing', 0.4093806743621826), ('thin', 0.4016669690608978)]
【问题讨论】:
标签: python nltk gensim word2vec doc2vec