【发布时间】:2021-06-01 02:47:21
【问题描述】:
我刚开始使用 word2vec 模型,我想根据我的问题数据制作不同的集群。
所以要制作集群,我必须这样做
创建词嵌入模型 从模型中获取词向量 从词向量创建句子向量 使用 Kmeans 对问题数据进行聚类
所以要得到word2vec词向量,one of the article says
def get_word2vec(tokenized_sentences):
print("Getting word2vec model...")
model = Word2Vec(tokenized_sentences, min_count=1)
return model.wv
然后创建句子向量和 Kmeans。
and other article says,得到word2vec模型后,我必须建立词汇,然后需要训练模型。然后创建句子向量然后Kmeans/
def get_word2vec_model(tokenized_sentences):
start_time = time.time()
print("Getting word2vec model...")
model = Word2Vec(tokenized_sentences, sg=1, window=window_size,vector_size=size, min_count=min_count, workers=workers, epochs=epochs, sample=0.01)
log_total_time(start_time)
return model
def get_word2vec_model_vector(model):
start_time = time.time()
print("Training...")
# model = Word2Vec(tokenized_sentences, min_count=1)
model.build_vocab(sentences=shuffle_corpus(tokenized_sentences), update=True)
# Training the model
for i in tqdm(range(5)):
model.train(sentences=shuffle_corpus(tokenized_sentences), epochs=50, total_examples=model.corpus_count)
log_total_time(start_time)
return model.wv
def shuffle_corpus(sentences):
shuffled = list(sentences)
random.shuffle(shuffled)
return shuffled
这就是我的 tokenized_sentences 的样子
8857 [, , , year, old]
11487 [, , birthday, canada, cant, share, job, friend]
20471 [, , chat, people, also, talk]
5877 [, , found]
Q1)第二种方法出现以下错误
---> 54 model.build_vocab(sentences=shuffle_corpus(tokenized_sentences), update=True)
55 # Training the model
56 for i in tqdm(range(5)):
~\AppData\Local\Programs\Python\Python38\lib\site-packages\gensim\models\word2vec.py in build_vocab(self, corpus_iterable, corpus_file, update, progress_per, keep_raw_vocab, trim_rule, **kwargs)
477
478 """
--> 479 self._check_corpus_sanity(corpus_iterable=corpus_iterable, corpus_file=corpus_file, passes=1)
480 total_words, corpus_count = self.scan_vocab(
481 corpus_iterable=corpus_iterable, corpus_file=corpus_file, progress_per=progress_per, trim_rule=trim_rule)
~\AppData\Local\Programs\Python\Python38\lib\site-packages\gensim\models\word2vec.py in _check_corpus_sanity(self, corpus_iterable, corpus_file, passes)
1484 """Checks whether the corpus parameters make sense."""
1485 if corpus_file is None and corpus_iterable is None:
-> 1486 raise TypeError("Either one of corpus_file or corpus_iterable value must be provided")
1487 if corpus_file is not None and corpus_iterable is not None:
1488 raise TypeError("Both corpus_file and corpus_iterable must not be provided at the same time")
TypeError: Either one of corpus_file or corpus_iterable value must be provided
和
Q2) 是否有必要构建词汇然后训练数据?或者获取模型是我唯一需要做的事情?
【问题讨论】:
标签: python machine-learning gensim word2vec