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, CRFLoss, CRFAccuracy
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=CRFLoss(crf),
metrics=[CRFAccuracy(crf)],
optimizer=tf.keras.optimizers.Adam(5e-5)
)
model.summary()
The model summary:
Model: "functional_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
sequence_input (InputLayer) [(None, None)] 0
_________________________________________________________________
embedding (Embedding) (None, None, 128) 12800
_________________________________________________________________
dense (Dense) (None, None, 256) 33024
_________________________________________________________________
crf (CRF) (None, None) 1862
=================================================================
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.0.4.tar.gz
(7.0 kB
view hashes)
Built Distribution
keras_crf-0.0.4-py3-none-any.whl
(12.7 kB
view hashes)
Close
Hashes for keras_crf-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a96d57d0b938a5724eea7c1aa2831919014e82f134d424b5779f14a2e36831f |
|
MD5 | 71285369e28e95a4f7f754c259102584 |
|
BLAKE2b-256 | 10a2bff4d6f705910d8ac0943913f6935b989c0de07264019bb215edf32bce86 |