keras實現Bi-LSTM+CRF
阿新 • • 發佈:2019-02-18
keras官方版目前還沒有實現CRF層,但是網上有大牛實現的擴充套件包戳這裡,取用之。
安裝方法1
git clone https://www.github.com/farizrahman4u/keras-contrib.git
cd keras-contrib
python setup.py install
安裝方法2
pip install git+https://www.github.com/farizrahman4u/keras-contrib.git
我自己在使用方法1的時候一直出錯,google了好久也沒解決,故改用方法2。
安裝完之後就可以直接使用了,上面給出的連結裡有example,很容易上手。
程式碼
下面這部分程式碼是之前做實驗搭baseline寫的。
input_layer = Input(shape = (max_len, ))
embedding_layer = Embedding(len(vocabulary) + 1, output_dim = embedding_dim, mask_zero = True)
bi_lstm_layer = Bidirectional(LSTM(64, return_sequences = True))
bi_lstm_drop_layer = Dropout(0.5)
dense_layer = TimeDistributed(Dense(len(tag_list)))
crf_layer = CRF(len(tag_list), sparse_target = True)
input = input_layer
embedding = embedding_layer(input)
bi_lstm = bi_lstm_layer(embedding)
bi_lstm_drop = bi_lstm_drop_layer(bi_lstm)
dense = dense_layer(bi_lstm_drop)
crf = crf_layer(dense)
model = Model(input = [input], output = [crf])
model.summary()
optmr = optimizers.Adam(lr = 0.001, beta_1 = 0.5)
model.compile(optimizer = optmr, loss = crf_layer.loss_function, metrics = [crf_layer.accuracy])
check_pointer = ModelCheckpoint(filepath = 'best_model.hdf5', verbose = 1, save_best_only = True)
hist = model.fit(train_x, train_y, batch_size = 32, epochs = 20, verbose = 2, validation_data = [val_x, val_y], callbacks = [check_pointer])
model.load_weights('best_model.hdf5')
test_y_pred = model.predict(test_x).argmax(-1)
test_y_true = test_y[:, :, 0]
注意
keras-contrib在linux python2.7 theano後端的情況下不work!
雖然不知道原因,總之後端改成tensorflow就沒有任何問題。