def build_lstm(output_dim, embeddings):
loss_function = "categorical_crossentropy"
# this is the placeholder tensor for the input sequences
sequence = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype="int32")
# this embedding layer will transform the sequences of integers
embedded = Embedding(embeddings.shape[0], embeddings.shape[1], input_length=MAX_SEQUENCE_LENGTH, weights=[embeddings], trainable=True)(sequence)
# 4 convolution layers (each 1000 filters)
cnn = [Convolution1D(filter_length=filters, nb_filter=1000, border_mode="same") for filters in [2, 3, 5, 7]]
# concatenate
merged_cnn = merge([cnn(embedded) for cnn in cnn], mode="concat")
# create attention vector from max-pooled convoluted
maxpool = Lambda(lambda x: keras_backend.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]))
attention_vector = maxpool(merged_cnn)
forwards = AttentionLSTM(64, attention_vector)(embedded)
backwards = AttentionLSTM(64, attention_vector, go_backwards=True)(embedded)
# concatenate the outputs of the 2 LSTM layers
bi_lstm = merge([forwards, backwards], mode="concat", concat_axis=-1)
after_dropout = Dropout(0.5)(bi_lstm)
# softmax output layer
output = Dense(output_dim=output_dim, activation="softmax")(after_dropout)
# the complete omdel
model = Model(input=sequence, output=output)
# try using different optimizers and different optimizer configs
model.compile("adagrad", loss_function, metrics=["accuracy"])
return model
评论列表
文章目录