第2关:Doc2vec 实战
时间: 2023-11-28 09:41:33 浏览: 56
在Doc2vec实战中,我们使用Gensim的Doc2Vec技术对中文文本数据进行量化处理和文档多分类。相比于Sklearn的CountVectorizer和TfidfTransformer,Doc2Vec可以更好地处理文本数据的语义信息。具体步骤如下:
1. 加载数据集并进行预处理,包括分词、去除停用词等。
相关问题
AttributeError: 'Doc2Vec' object has no attribute 'dv'
这个错误通常是因为在使用gensim库中的Doc2Vec模型时,尝试访问不存在的属性'dv'。这个错误可能是由于gensim版本更新而导致的,因为gensim 4.0.0版本中已经将'dv'属性更改为'vectors'属性。因此,如果你使用的是gensim 4.0.0或更高版本,你需要将'dv'更改为'vectors'。
以下是一个例子,展示了如何使用gensim 4.0.0版本中的Doc2Vec模型来避免这个错误:
```python
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
# 创建一个简单的语料库
data = ["I love machine learning. Its awesome.",
"I love coding in python",
"I love building chatbots",
"they chat amagingly well"]
# 对语料库中的每个文档进行标记
tagged_data = [TaggedDocument(words=d.split(), tags=[str(i)]) for i, d in enumerate(data)]
# 训练Doc2Vec模型
model = Doc2Vec(tagged_data, vector_size=20, min_count=1, epochs=5)
# 获取文档向量
doc_vector = model.infer_vector(["I", "love", "chatbots"])
# 获取与文档向量最相似的文档
sims = model.dv.most_similar([doc_vector], topn=2)
print(sims)
```
如果你使用的是gensim 4.0.0或更高版本,你需要将'model.dv'更改为'model.vectors',如下所示:
```python
sims = model.vectors.most_similar([doc_vector], topn=2)
```
AttributeError: 'Doc2Vec' object has no attribute 'iter'
引用[1]:报错为: File "/home/sunxiangguo/PycharmProjects/personality/cnn.py", line 85, in <module> tokenizer.fit_on_texts(text_list) File "/home/sunxiangguo/anaconda2/lib/python2.7/site-packages/keras/preprocessing/text.py", line 119, in fit_on_texts self.split) File "/home/sunxiangguo/anaconda2/lib/python2.7/site-packages/keras/preprocessing/text.py", line 38, in text_to_word_sequence text = text.translate(maketrans(filters, split * len(filters))) TypeError: character mapping must return integer, None or unicode 。[1]这个错误是由于在文本预处理过程中,字符映射函数返回了一个不合法的值,导致了类型错误。你可以检查一下你的字符映射函数的实现,确保它返回的是整数、None或者Unicode类型的值。
引用[2]:二、属性错误 1.报错:AttributeError: ‘Word2Vec’ object has no attribute ‘most_similar’ 2. 错误的源码: # 模型训练主程序 logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) sentences_1 = word2vec.LineSentence('no_stop.txt') model_1 = word2vec.Word2Vec(sentences_1) # model.wv.save_word2vec_format('test_01.model.txt', 'test_01.vocab.txt', binary=False) # 保存模型,后面可直接调用 # model = word2vec.Word2Vec.load("test_01.model") # 调用模型 # 计算某个词的相关词列表 a_1 = model_1.most_similar(u"空间", topn=20) print(a_1) 。[2]这个错误是由于你正在尝试使用Word2Vec对象的'most_similar'属性,但是该属性在你的代码中不存在。你可以检查一下你的Word2Vec对象的属性和方法,确保你使用的是正确的属性和方法。
引用[3]:修正: from keras.preprocessing.text import Tokenizer texts=data.x_train sample_index=0 text_list = texts[sample_index][0] # 这是一个句子列表,里面是unicode tokenizer = Tokenizer(word_num_per_sent) tokenizer.fit_on_texts([s.encode('ascii') for s in text_list]) #tokenizer.fit_on_texts(text_list) 。[3]这个修正是针对使用Tokenizer对象进行文本处理的代码。你可以尝试将文本列表中的每个句子转换为ASCII编码,然后再进行文本处理。这样可以避免出现类型错误。
问题:AttributeError: 'Doc2Vec' object has no attribute 'iter'
回答: 这个错误是由于你正在尝试使用Doc2Vec对象的'iter'属性,但是该属性在你的代码中不存在。你可以检查一下你的Doc2Vec对象的属性和方法,确保你使用的是正确的属性和方法。