提供POS标签作为RNN的输入以及单词嵌入

时间:2019-04-08 09:17:53

标签: keras recurrent-neural-network

我必须将输入作为词嵌入+ POS标签提供给RNN。但是单词嵌入仅由代码生成。因此,我无法进行嵌入和POS一个热向量连接。完成这项任务的最佳方法是什么?

def BidLstm(maxlen, N_TAGS, EMBEDDING_DIM, embedding_matrix):
    inp = Input(shape=(maxlen,), dtype='int32')
    x = Embedding(len(word2int) + 1,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=maxlen,
                            trainable=False)(inp)


    x = Bidirectional(LSTM(300, return_sequences=True, dropout=0.25,
                           recurrent_dropout=0.25))(x)
    xa = Attention(maxlen)(x)
    xd = Dense(256, activation="relu")(xa)
    xdp = Dropout(0.25)(xd)
    xd1 = Dense(5, activation="softmax")(xdp)
    #x = TimeDistributed(Dense(N_TAGS + 1, activation='softmax'))(x)
    model = Model(inputs=inp, outputs=xd1)
    return model


model = BidLstm(max(sentence_length_list),5, EMBEDDING_DIM, embedding_matrix)
model.compile(loss='binary_crossentropy', optimizer='adam',
              metrics=['accuracy'])
file_path = ".model.hdf5"
ckpt = ModelCheckpoint(file_path, monitor='val_loss', verbose=1,
                       save_best_only=True, mode='min')
early = EarlyStopping(monitor="val_loss", mode="min", patience=1)
model.fit(X_train, Y_train_onehot, batch_size=32, epochs=15, validation_split=0.1, callbacks=[ckpt, early])

0 个答案:

没有答案