【发布时间】:2021-08-09 08:47:10
【问题描述】:
我正在尝试将 Doc2Vec 方法放入数据框中,其中第一列包含文本,第二列包含标签(作者)。我找到了这篇文章https://towardsdatascience.com/multi-class-text-classification-with-doc2vec-logistic-regression-9da9947b43f4,真的很有帮助。但是,我对如何构建模型感到困惑
import tqdm
cores = multiprocessing.cpu_count()
model_dbow = Doc2Vec(dm=0, vector_size=300, negative=5, hs=0, min_count=2, sample=0, workers=cores)
model_dbow.build_vocab([x for x in tqdm(train_tagged.values)])
TypeError: 'module' 对象不可调用
您能帮我解决这个问题吗?
在此之前我也有这个代码
train, test = train_test_split(df, test_size=0.3, random_state=42)
import nltk
from nltk.corpus import stopwords
def tokenize_text(text):
tokens = []
for sent in nltk.sent_tokenize(text):
for word in nltk.word_tokenize(sent):
if len(word) < 2:
continue
tokens.append(word.lower())
return tokens
train_tagged = train.apply(
lambda r: TaggedDocument(words=tokenize_text(r['text']), tags=[r.author]), axis=1)
test_tagged = test.apply(
lambda r: TaggedDocument(words=tokenize_text(r['text']), tags=[r.author]), axis=1)
编辑:如果我从代码中删除 tqdm 是有效的,但我不确定这是否被接受。据我所知,tqdm 是 Python 的一个包,它使您能够立即为您的函数和循环创建进度条并估计 TTC(完成时间),所以我的意思是如果我删除它,输出就没有问题。对吧?
Edit2:另请参阅此问题My Doc2Vec code, after many loops of training, isn't giving good results. What might be wrong? 以改进教程的代码。再次感谢@gojomo
【问题讨论】:
-
单独注意,您在 towardsdatascience.com/…> 处复制的在线示例使用了一个过于复杂且容易出错的循环来多次调用
Doc2Vec.train()并自行管理alpha(不良)。事实上,如果你完全为你的模型复制它的循环(它以默认的alpha=0.025开头),你最终会将alpha递减为无意义的负值。有关详细信息,请参阅stackoverflow.com/a/62801053/130288。 -
@gojomo 非常感谢!你完全正确!