Training Doc2Vec on 20newsgroups dataset. Getting Exception AttributeError: 'str' object has no attribute 'words'
Training Doc2Vec on 20newsgroups dataset. Getting Exception AttributeError: 'str' object has no attribute 'words'
这里有一个类似的问题Gensim Doc2Vec Exception AttributeError: 'str' object has no attribute 'words',但没有得到任何有用的答案。
我正在尝试在 20newsgroups 语料库上训练 Doc2Vec。
以下是我构建词汇表的方式:
from sklearn.datasets import fetch_20newsgroups
def get_data(subset):
newsgroups_data = fetch_20newsgroups(subset=subset, remove=('headers', 'footers', 'quotes'))
docs = []
for news_no, news in enumerate(newsgroups_data.data):
tokens = gensim.utils.to_unicode(news).split()
if len(tokens) == 0:
continue
sentiment = newsgroups_data.target[news_no]
tags = ['SENT_'+ str(news_no), str(sentiment)]
docs.append(TaggedDocument(tokens, tags))
return docs
train_docs = get_data('train')
test_docs = get_data('test')
alldocs = train_docs + test_docs
model = Doc2Vec(dm=dm, size=size, window=window, alpha = alpha, negative=negative, sample=sample, min_count = min_count, workers=cores, iter=passes)
model.build_vocab(alldocs)
然后我训练模型并保存结果:
model.train(train_docs, total_examples = len(train_docs), epochs = model.iter)
model.train_words = False
model.train_labels = True
model.train(test_docs, total_examples = len(test_docs), epochs = model.iter)
model.save(output)
加载模型时出现问题:
screen
我试过了:
使用 LabeledSentence 代替 TaggedDocument
生成 TaggedDocument 而不是将它们附加到列表中
将 min_count 设置为 1 这样就不会忽略任何单词(以防万一)
python2 和 python3 也出现问题。
请帮我解决这个问题。
您已在非现场 (imgur)“screen”link 中隐藏了最重要的信息——触发错误的确切代码以及错误文本本身。 (这将是剪切并粘贴到问题中的理想文本,而不是其他看起来 运行 OK,不会触发错误的步骤。)
看看那个截图,有一行:
model = Doc2Vec("20ng_infer")
...这会触发错误。
请注意,作为 documented for the Doc2Vec()
initialization method 的参数中的 none 是一个纯字符串,就像上一行中的 "20ng_infer"
参数一样——所以这不太可能做任何有用的事情。
如果尝试加载以前用 model.save()
保存的模型,您应该使用 Doc2Vec.load()
– 这将采用一个字符串来描述从中加载模型的本地文件路径。所以尝试:
model = Doc2Vec.load("20ng_infer")
(另请注意,较大的模型可能会保存到多个文件中,所有文件都以您提供给 save()
的字符串开头,并且这些文件必须 kept/moved 在一起以再次重新 load()
他们在未来。)
这里有一个类似的问题Gensim Doc2Vec Exception AttributeError: 'str' object has no attribute 'words',但没有得到任何有用的答案。
我正在尝试在 20newsgroups 语料库上训练 Doc2Vec。 以下是我构建词汇表的方式:
from sklearn.datasets import fetch_20newsgroups
def get_data(subset):
newsgroups_data = fetch_20newsgroups(subset=subset, remove=('headers', 'footers', 'quotes'))
docs = []
for news_no, news in enumerate(newsgroups_data.data):
tokens = gensim.utils.to_unicode(news).split()
if len(tokens) == 0:
continue
sentiment = newsgroups_data.target[news_no]
tags = ['SENT_'+ str(news_no), str(sentiment)]
docs.append(TaggedDocument(tokens, tags))
return docs
train_docs = get_data('train')
test_docs = get_data('test')
alldocs = train_docs + test_docs
model = Doc2Vec(dm=dm, size=size, window=window, alpha = alpha, negative=negative, sample=sample, min_count = min_count, workers=cores, iter=passes)
model.build_vocab(alldocs)
然后我训练模型并保存结果:
model.train(train_docs, total_examples = len(train_docs), epochs = model.iter)
model.train_words = False
model.train_labels = True
model.train(test_docs, total_examples = len(test_docs), epochs = model.iter)
model.save(output)
加载模型时出现问题: screen
我试过了:
使用 LabeledSentence 代替 TaggedDocument
生成 TaggedDocument 而不是将它们附加到列表中
将 min_count 设置为 1 这样就不会忽略任何单词(以防万一)
python2 和 python3 也出现问题。
请帮我解决这个问题。
您已在非现场 (imgur)“screen”link 中隐藏了最重要的信息——触发错误的确切代码以及错误文本本身。 (这将是剪切并粘贴到问题中的理想文本,而不是其他看起来 运行 OK,不会触发错误的步骤。)
看看那个截图,有一行:
model = Doc2Vec("20ng_infer")
...这会触发错误。
请注意,作为 documented for the Doc2Vec()
initialization method 的参数中的 none 是一个纯字符串,就像上一行中的 "20ng_infer"
参数一样——所以这不太可能做任何有用的事情。
如果尝试加载以前用 model.save()
保存的模型,您应该使用 Doc2Vec.load()
– 这将采用一个字符串来描述从中加载模型的本地文件路径。所以尝试:
model = Doc2Vec.load("20ng_infer")
(另请注意,较大的模型可能会保存到多个文件中,所有文件都以您提供给 save()
的字符串开头,并且这些文件必须 kept/moved 在一起以再次重新 load()
他们在未来。)