All Projects → Hironsan → keras-crf-layer

Hironsan / keras-crf-layer

Licence: MIT license
Implementation of CRF layer in Keras.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to keras-crf-layer

Fancy Nlp
NLP for human. A fast and easy-to-use natural language processing (NLP) toolkit, satisfying your imagination about NLP.
Stars: ✭ 233 (+206.58%)
Mutual labels:  crf
xinlp
把李航老师《统计学习方法》的后几章的算法都用java实现了一遍,实现盒子与球的EM算法,扩展到去GMM训练,后来实现了HMM分词(实现了HMM分词的参数训练)和CRF分词(借用CRF++训练的参数模型),最后利用tensorFlow把BiLSTM+CRF实现了,然后为lucene包装了一个XinAnalyzer
Stars: ✭ 21 (-72.37%)
Mutual labels:  crf
deepseg
Chinese word segmentation in tensorflow 2.x
Stars: ✭ 23 (-69.74%)
Mutual labels:  crf
Pytorch Bert Crf Ner
KoBERT와 CRF로 만든 한국어 개체명인식기 (BERT+CRF based Named Entity Recognition model for Korean)
Stars: ✭ 236 (+210.53%)
Mutual labels:  crf
ChineseNER
中文NER的那些事儿
Stars: ✭ 241 (+217.11%)
Mutual labels:  crf
mahjong
开源中文分词工具包,中文分词Web API,Lucene中文分词,中英文混合分词
Stars: ✭ 40 (-47.37%)
Mutual labels:  crf
Deep Crf
An implementation of Conditional Random Fields (CRFs) with Deep Learning Method
Stars: ✭ 161 (+111.84%)
Mutual labels:  crf
Hierarchical-Word-Sense-Disambiguation-using-WordNet-Senses
Word Sense Disambiguation using Word Specific models, All word models and Hierarchical models in Tensorflow
Stars: ✭ 33 (-56.58%)
Mutual labels:  crf
keras-crf-ner
keras+bi-lstm+crf,中文命名实体识别
Stars: ✭ 16 (-78.95%)
Mutual labels:  crf
BiLSTM-CRF-NER-PyTorch
This repo contains a PyTorch implementation of a BiLSTM-CRF model for named entity recognition task.
Stars: ✭ 109 (+43.42%)
Mutual labels:  crf
Pytorch ner bilstm cnn crf
End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF implement in pyotrch
Stars: ✭ 249 (+227.63%)
Mutual labels:  crf
Machine Learning Code
《统计学习方法》与常见机器学习模型(GBDT/XGBoost/lightGBM/FM/FFM)的原理讲解与python和类库实现
Stars: ✭ 169 (+122.37%)
Mutual labels:  crf
fastai sequence tagging
sequence tagging for NER for ULMFiT
Stars: ✭ 21 (-72.37%)
Mutual labels:  crf
Torchnlp
Easy to use NLP library built on PyTorch and TorchText
Stars: ✭ 233 (+206.58%)
Mutual labels:  crf
NLP-paper
🎨 🎨NLP 自然语言处理教程 🎨🎨 https://dataxujing.github.io/NLP-paper/
Stars: ✭ 23 (-69.74%)
Mutual labels:  crf
Open Sesame
A frame-semantic parsing system based on a softmax-margin SegRNN.
Stars: ✭ 170 (+123.68%)
Mutual labels:  crf
crfsuite-rs
Rust binding to crfsuite
Stars: ✭ 19 (-75%)
Mutual labels:  crf
CRFasRNNLayer
Conditional Random Fields as Recurrent Neural Networks (Tensorflow)
Stars: ✭ 76 (+0%)
Mutual labels:  crf
korean ner tagging challenge
KU_NERDY 이동엽, 임희석 (2017 국어 정보 처리 시스템경진대회 금상) - 한글 및 한국어 정보처리 학술대회
Stars: ✭ 30 (-60.53%)
Mutual labels:  crf
Gumbel-CRF
Implementation of NeurIPS 20 paper: Latent Template Induction with Gumbel-CRFs
Stars: ✭ 51 (-32.89%)
Mutual labels:  crf

Keras-CRF-Layer

The Keras-CRF-Layer module implements a linear-chain CRF layer for learning to predict tag sequences. This variant of the CRF is factored into unary potentials for every element in the sequence and binary potentials for every transition between output tags.

Usage

Below is an example of the API, which learns a CRF for some random data. The linear layer in the example can be replaced by any neural network.

import numpy as np
from keras.layers import Embedding, Input
from keras.models import Model

from crf import CRFLayer

# Hyperparameter settings.
vocab_size = 20
n_classes = 11
batch_size = 2
maxlen = 2

# Random features.
x = np.random.randint(1, vocab_size, size=(batch_size, maxlen))

# Random tag indices representing the gold sequence.
y = np.random.randint(n_classes, size=(batch_size, maxlen))
y = np.eye(n_classes)[y]

# All sequences in this example have the same length, but they can be variable in a real model.
s = np.asarray([maxlen] * batch_size, dtype='int32')

# Build an example model.
word_ids = Input(batch_shape=(batch_size, maxlen), dtype='int32')
sequence_lengths = Input(batch_shape=[batch_size, 1], dtype='int32')

word_embeddings = Embedding(vocab_size, n_classes)(word_ids)
crf = CRFLayer()
pred = crf(inputs=[word_embeddings, sequence_lengths])
model = Model(inputs=[word_ids, sequence_lengths], outputs=[pred])
model.compile(loss=crf.loss, optimizer='sgd')

# Train first 1 batch.
model.train_on_batch([x, s], y)

# Save the model
model.save('model.h5')

Model loading

When you want to load a saved model that has a crf output, then loading the model with 'keras.models.load_model' won't work properly because the reference of the loss function to the transition parameters is lost. To fix this, you need to use the parameter 'custom_objects' as follows:

from keras.models import load_model

from crf import create_custom_objects

model = load_model('model.h5', custom_objects=create_custom_objects())
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].