3

我正在尝试在 Keras 中训练单词级别的语言模型。

我有我的 X 和 Y,两者都具有形状(90582L、517L)

当我尝试适合这个模型时:

print('Build model...')
model = Sequential()
model.add(GRU(512, return_sequences=True, input_shape=(90582, 517)))
model.add(Dropout(0.2))
model.add(GRU(512, return_sequences=True))
model.add(Dropout(0.2))
model.add(TimeDistributedDense(1))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(x_pad, y_pad, batch_size=128, nb_epoch=2)

我得到错误:

Exception: Error when checking model input: 
expected gru_input_7 to have 3 dimensions, but got array with shape (90582L, 517L)

我需要一些关于输入形状应该是什么的指导?我已经对各种组合进行了反复试验,但似乎我误解了一些基本的东西。

在 Keras 文本生成示例中,X 矩阵有 3 个维度。我不知道第三维应该是什么。

4

1 回答 1

6

这取决于你想要做什么。我猜你的形状数据 (90582, 517) 是一组 90582 个样本,每个样本有 517 个单词。如果是这样,您必须将您的单词转换为单词向量(=嵌入)才能使它们有意义。然后你将拥有可以由 GRU 处理的形状 (90582, 517, embedding_dim)。

Keras嵌入层可以为您做到这一点。在第一个 GRU 层之前将其添加为神经网络的第一层。

vocabulary_size = XXXXX     # give your vocabulary size here (largest word ID in the input)
embedding_dim = XXXX        # give your embedding dimension here (e.g. 100)

print('Build model...')
model = Sequential()
model.add(Embedding(vocabulary_size, embedding_dim, input_shape=(90582, 517)))
model.add(GRU(512, return_sequences=True))
model.add(Dropout(0.2))
model.add(GRU(512, return_sequences=True))
model.add(Dropout(0.2))
model.add(TimeDistributedDense(1))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(x_pad, y_pad, batch_size=128, nb_epoch=2)
于 2016-07-07T13:28:37.633 回答