A more elegant and convenient CRF built on tensorflow-addons.
Project description
keras-crf
A more elegant and convenient CRF built on tensorflow-addons.
Python Compatibility is limited to tensorflow/addons, you can check the compatibility from it's home page.
Installation
pip install keras-crf
Usage
Here is an example to show you how to build a CRF model easily:
import tensorflow as tf
from keras_crf import CRF
sequence_input = tf.keras.layers.Input(shape=(None,), dtype=tf.int32, name='sequence_input')
sequence_mask = tf.keras.layers.Lambda(lambda x: tf.greater(x, 0))(sequence_input)
outputs = tf.keras.layers.Embedding(100, 128)(sequence_input)
outputs = tf.keras.layers.Dense(256)(outputs)
crf = CRF(7)
# mask is important to compute sequence length in CRF
outputs = crf(outputs, mask=sequence_mask)
model = tf.keras.Model(inputs=sequence_input, outputs=outputs)
model.compile(
loss=crf.neg_log_likelihood,
metrics=[crf.accuracy],
optimizer=tf.keras.optimizers.Adam(5e-5)
)
model.summary()
The model summary:
Model: "functional_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
sequence_input (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
embedding (Embedding) (None, None, 128) 12800 sequence_input[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, None, 256) 33024 embedding[0][0]
__________________________________________________________________________________________________
lambda (Lambda) (None, None) 0 sequence_input[0][0]
__________________________________________________________________________________________________
crf (CRF) (None, None, 7) 1862 dense[0][0]
lambda[0][0]
==================================================================================================
Total params: 47,686
Trainable params: 47,686
Non-trainable params: 0
__________________________________________________________________________________________________
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
keras_crf-0.1.0.tar.gz
(6.9 kB
view hashes)
Built Distribution
keras_crf-0.1.0-py3-none-any.whl
(12.5 kB
view hashes)
Close
Hashes for keras_crf-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05c051550a7a1676eef5249ebdfb5273b357e38a2ae771372b8d3057f186324d |
|
MD5 | 13b52fce3a80ae789a256b9b351c9a95 |
|
BLAKE2b-256 | 13a3a29d3f37c8555f51f44b8567a7d9caf6badaaa4162c89400b1e3bd2669fd |