【发布时间】:2019-04-15 09:08:54
【问题描述】:
我一直在尝试在 Keras 中实现这个 BiLSTM:https://github.com/ffancellu/NegNN
这就是我所在的位置,而且它确实有效:
inputs_w = Input(shape=(sequence_length,), dtype='int32')
inputs_pos = Input(shape=(sequence_length,), dtype='int32')
inputs_cue = Input(shape=(sequence_length,), dtype='int32')
w_emb = Embedding(vocabulary_size+1, embedding_dim, input_length=sequence_length, trainable=False)(inputs_w)
p_emb = Embedding(tag_voc_size+1, embedding_dim, input_length=sequence_length, trainable=False)(inputs_pos)
c_emb = Embedding(2, embedding_dim, input_length=sequence_length, trainable=False)(inputs_cue)
summed = keras.layers.add([w_emb, p_emb, c_emb])
BiLSTM = Bidirectional(CuDNNLSTM(hidden_dims, return_sequences=True))(summed)
DPT = Dropout(0.2)(BiLSTM)
outputs = Dense(2, activation='softmax')(DPT)
checkpoint = ModelCheckpoint('bilstm_one_hot.hdf5', monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
early = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, verbose=1, mode='auto')
model = Model(inputs=[inputs_w, inputs_pos, inputs_cue], outputs=outputs)
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
model.fit([X_train, X_pos_train, X_cues_train], Y_train, batch_size=batch_size, epochs=num_epochs, verbose=1, validation_split=0.2, callbacks=[early, checkpoint])
在原始代码中,在Tensorflow中,作者使用了masking和softmax cross entropy with logits。我还不知道如何在 Keras 中实现这一点。如果您有任何建议,请不要犹豫。
我的主要问题是 return_sequences=True。作者似乎没有在他的 tensorflow 实现中使用它,当我将其设置为 False 时,我收到此错误:
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (820, 109, 2)
我也尝试过使用:
outputs = TimeDistributed(Dense(2, activation='softmax'))(BiLSTM)
返回和 AssertionError 没有任何信息。
有什么想法吗?
谢谢
【问题讨论】:
标签: tensorflow keras nlp