我正在做阿拉伯方言文本分类,我已经使用Word2Vec来训练模型,到目前为止我得到了如下结果:
def read_input(input_file):
with open (input_file, 'rb') as f:
for i, line in enumerate (f):
yield gensim.utils.simple_preprocess (line)
documents = list (read_input (data_file))
logging.info ("Done reading data file")
model = gensim.models.Word2Vec (documents, size=150, window=10, min_count=2, workers=10)
model.train(documents,total_examples=len(documents),epochs=10)
我现在该做什么来预测一个新的文本,如果它是任何一个我有5种方言?
另外,我环顾四周发现了以下代码:
# load the pre-trained word-embedding vectors
embeddings_index = {}
for i, line in enumerate(open('w2vmodel.vec',encoding='utf-8')):
values = line.split()
embeddings_index[values[0]] = numpy.asarray(values[1:], dtype='float32')
# create a tokenizer
token = text.Tokenizer()
token.fit_on_texts(trainDF['text'])
word_index = token.word_index
# convert text to sequence of tokens and pad them to ensure equal length vectors
train_seq_x = sequence.pad_sequences(token.texts_to_sequences(train_x), maxlen=70)
valid_seq_x = sequence.pad_sequences(token.texts_to_sequences(valid_x), maxlen=70)
# create token-embedding mapping
embedding_matrix = numpy.zeros((len(word_index) + 1, 300))
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
但是,当我运行它并加载经过训练的word2vec模型时,它给了我这个错误:
ValueError: could not convert string to float: '\x00\x00\x00callbacksq\x04)X\x04\x00\x00\x00loadq\x05cgensim.utils'
注意:
实际上,还有一段我没有在这里发布的代码,我想在神经网络中使用word2vec,我有神经网络的代码,但是我不知道如何使我从word2vec获得的特性成为神经网络的输入,并以标签作为输出。是否有可能将word2vec连接到深层神经网络,以及如何连接?
发布于 2019-05-30 16:38:52
Word2vec本身并不能将文本分类为方言,所以您还没有在这里勾勒出一个看似合理的完整方法。
是什么使您认为word2vec可以或应该作为此任务的一部分使用?(如果有一种激励性的操作理论,或者其他一些给你这个想法的公开的先例,那将有助于指导你还应该做什么。)
你的训练数据是什么样的?
如果是大量的示例文本,并有准确的标签说明每个文本来自哪个方言,那么您是否尝试过对文本进行简单的单词或字符包表示的分类器?(通过发现方言和文本中准确的单词、词组或单词片段之间的关系,这样的分类器可能比word2vec更有效。Word2vec忽略了单词片段,并将相似意思的单词的向量紧密结合起来,掩盖了单词拼写或单词选择方面的细微差异。)
你也可以尝试:
(关于所示代码,分别:
train(documents,...)
(实例化调用),则不需要调用documents
,因为启用信息日志记录和查看日志应该可以清楚地说明这一点。w2vmodel.vec
文件,因为gensim
包含直接读取此类文件的方法,例如.load_word2vec_format()
或者(如果完整的模型是gensim中的.save()
d ),只有.load()
。)
https://stackoverflow.com/questions/56367510
复制相似问题