本文介绍了LSTM如何将字符嵌入向量转换成句子向量以进行句子分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用字符嵌入为句子分类建立一个LSTM模型.

I want to build a LSTM model for sentence classification using character embeddings.

我知道如何使用单词嵌入来实现,该模型可以从单词索引中学习嵌入,但是不确定如何使用字符嵌入.

I know how to do it using word embeddings where the model can learn the embeddings from word indexes but not sure how to do it with character embeddings.

用于单词嵌入:

sentence_list = ['this is a dog', 'the cat and the mouse']
label = [1,0]
word_dict = {'this':1,
             'is':2,
             'a':3,
             'dog':4,
             'the':5,
             'cat':6,
             'and':7,
             'mouse':8}

# set vector length = 9
vectors = [[1,2,3,4,0,0,0,0,0]
              [0,0,0,0,5,6,7,5,8]]
model.fit(vectors,label)

因此可以随时将其安装到LSTM模型中.

so this is ready to be fitted into a LSTM model.

对于基于字符的矢量我们该怎么做?

How do we do it for character based vectors?

例如:如果我有这个汉字字典:

for example:if i have this character dictionary:

 char_dict = {'t':1,
             'h':2,
             'i':3,
             's':4,
             'a':5,
             'd':6,
             'o':7,
             'g':8}

如何将其格式化为LSTM分类模型可读?更具体地说,我们如何组合多个字符向量以馈入LSTM模型?

How do i format this to be readable for the LSTM classification model?More specifically, how do we combine multiple character vectors to be fed into the LSTM model?

推荐答案

完全相同.完全没有区别.

It's exactly the same. No difference at all.

将句子转换为索引向量并拟合.

Transform the sentences into vectors of indices and go fit.

重要的事情:

不要用0开头的句子,您的vectors应该是:

Don't make sentences starting with 0, your vectors should be:

vectors = [[1,2,3,4,0,0,0,0,0]
          [5,6,7,5,8,0,0,0,0]]

至少有空格和标点符号:

Have indices for spaces (at least) and punctuation:

 char_dict = {'t':1,
         'h':2,
         'i':3,
         's':4,
         'a':5,
         'd':6,
         'o':7,
         'g':8
         ' ':9,
         '.':10,
         'c':11}

sentences = ['this is a dog', 'that is a cat.']
vectors = [
              [char_dict[ch] for ch in sentence] for sentence in sentences
          ]

vectors = [
              [1, 2, 3, 4, 9, 3, 4, 9, 5,  9, 6, 7,  8],
              [1, 2, 5, 1, 9, 3, 4, 9, 5, 11, 5, 1, 10]
          ]

这篇关于LSTM如何将字符嵌入向量转换成句子向量以进行句子分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 02:53