如何使用word2vec和keras对句子进行分类?

时间:2017-03-17 11:58:57

标签: keras recurrent-neural-network gensim word2vec keras-layer

我有2个句子列表。第一个列表包含不同的问题,第二个列表包含不同的语句。

小例子:

1st list:
[
    "What are cultures that are associated with core values?",
    "How do bumblebees fly?",
    "It is possible that avocado seed can be a tea?",
    ...
]

2nd list:
[
    "The population was 388 at the 2010 census.",
    "Grevillea helmsiae is a tree which is endemic to Queensland in Australia.",
    "He played youth football for Tynecastle Boys Club.",
    ...
]

我想编写能够对这两种句子进行分类的程序。为此,我可以创建神经网络并在我的2个列表上训练它。我猜,这必须是循环神经网络。

我已将每个句子转换为word2vec向量数组。现在我想用LSTM层建立keras递归神经网络。但我不知道如何正确地做到这一点。你能为这个问题写出keras模型吗?

更新

通过word2vec转换后的上述句子的形式是这样的:

[
    [vector_of_floats_for_word_"what", vector_of_floats_for_word_"are", vector_of_floats_for_word_"cultures", vector_of_floats_for_word_"that", ...],
    [vector_of_floats_for_word_"how", vector_of_floats_for_word_"do", vector_of_floats_for_word_"bumblebees", ...]
]

等等。每个向量有300个维度。

这是我的模特:

X = []
Y = []

for i in range(1000):
    X.append(questions_vectors[i])
    Y.append([1, 0])
    X.append(statements_vectors[i])
    Y.append([0, 1])

model = Sequential()
model.add(LSTM(128, input_shape=(2000, None, 300)))
model.add(Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.01))

在那里你可以看到魔法数字2000和300. 2000是1000个问题+ 1000个语句,300个单词矢量长度

但我确定我的模特错了。我也得到了错误:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

0 个答案:

没有答案