【问题标题】:Error: 'module' object is not callable in Doc2Vec错误:“模块”对象在 Doc2Vec 中不可调用
【发布时间】: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 非常感谢!你完全正确!

标签: python nlp doc2vec


【解决方案1】:

您正在导入 tqdm 模块,而不是实际的类。

替换import tqdm

from tqdm import tqdm

【讨论】:

  • 好的,我会这样做,如果我删除 tqdm,输出有什么问题吗?请查看我更新的问题。
  • tqdm 只是一个进度可视化库,它不会改变你的代码逻辑。
【解决方案2】:

我找到了这个

我不确定 Doc2Vec

但是python中的这个错误是关于模块名称的

这个错误语句 TypeError: 'module' object is not callable 被引发,因为你对类名和模块名感到困惑。问题出在导入行。您正在导入一个模块,而不是一个类。发生这种情况是因为模块名称和类名称具有相同的名称。

如果你在一个名为 MyClass.py 的文件中有一个 MyClass 类,那么你应该这样写:

from MyClass import MyClass

源代码:http://net-informations.com/python/iq/typeerror.htm

【讨论】:

    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多