Linear chain conditional random fields are implemented using Numpy and Mxnet/Gluon, and batch training is supported, not limited to training the data one by one. The latest version of the code uses the foreach operation to perform the expansion of the sequence. The calculation uses NDArray for calculations as much as possible, so that the calculation can be accelerated on the GPU. The final test is more than 40 times faster than the previous calculation using the for loop, so the performance is effectively improved.
The forward calculation, backward calculation, Viterbi decoding, log maximum likelihood and other methods in the conditional random field are realized in crf.py. Finally, the CRF is designed as the layer of the neural network that can be reused in Mxet/Gluon. In order to directly interface with other network layers, such as LSTM + CRF.
- crf.py (Source code for CRF implementation)
- test.py (Directly test whether CRF is available)
- test_crf_layer.py (Place the CRF as a custom layer in other models and test for availability)