Keras LSTM/GRU language mod的输入形状

2024-04-20 07:13:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在凯拉斯训练一个单词级的语言模型。

我有我的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维。我不知道第三维度应该是什么。


Tags: 模型add语言trueinputmodelreturn单词
1条回答
网友
1楼 · 发布于 2024-04-20 07:13:11

这取决于你想做什么。我猜你的形状数据(90582517)是一组90582个样本,每个样本有517个单词。如果是的话,你必须把你的单词转换成单词向量(=嵌入),这样它们才有意义。然后您将得到可以由GRU处理的形状(90582517,embedding廑im)。

Keras Embedding layer可以帮你。在第一个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)

相关问题 更多 >