tf2crf
a crf layer for tensorflow 2 keras
What it is and what it does
tf2crf wraps a CRF (Conditional Random Field) layer into TensorFlow 2 Keras, allowing you to add sequence-level constraints to neural sequence labeling models. It handles masking automatically and supports mixed precision training. The package includes a ModelWithCRFLoss wrapper that decouples the loss function from the layer itself, and offers an optional DSC (Dice) loss variant for imbalanced datasets.
The layer is designed to sit at the end of a sequence model (typically after a BiLSTM or BiGRU) and learns transition probabilities between output labels. You build a standard Keras model, wrap it with ModelWithCRFLoss, and train normally. However, the package has been abandoned since mid-2021, so you should verify compatibility with your TensorFlow and tensorflow-addons versions before relying on it in production.
Use it for:
- Named entity recognition (NER) tasks where label transitions matter and you want CRF constraints
- Part-of-speech tagging or other sequence labeling where forbidden transitions should be penalized
- Imbalanced NLP classification using the DSC loss variant to improve F1 scores
- Prototyping sequence models with Keras when you need CRF without building it from scratch
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides a Conditional Random Field (CRF) layer for TensorFlow 2 Keras models, enabling sequence labeling tasks with built-in support for masking and mixed precision training.
Yes-with-conditions. The package solves a real problem (CRF in Keras) and has low install friction, but it is abandoned and last tested with TensorFlow versions from 2021. Install only if you can verify compatibility with your TensorFlow and tensorflow-addons versions, and accept the risk that bugs or incompatibilities will not be fixed upstream. For active projects, consider whether a maintained alternative or a custom CRF implementation is safer.
Install
tf2crf on PyPI
pip
pip install tf2crfuv
uv add tf2crfpoetry
poetry add tf2crfInstalling tf2crf
Before you install
Low install friction; pure Python wheel. However, the package is abandoned—last release was 2021-09-10 and last commit 2023-03-25. Depends on tensorflow and tensorflow-addons, which are large dependencies; you'll need to manage compatibility between them yourself.
License in practice
MIT License permits commercial and private use with minimal restrictions, making it safe to adopt from a licensing standpoint.
Quickstart
pip install tf2crf
import tensorflow as tf
from tf2crf import CRF, ModelWithCRFLoss
from tensorflow.keras.layers import Input, Embedding, Bidirectional, GRU
from tensorflow.keras.models import Model
inputs = Input(shape=(None,), dtype='int32')
output = Embedding(100, 40, mask_zero=True)(inputs)
output = Bidirectional(GRU(64, return_sequences=True))(output)
crf = CRF(units=9, type='float32')
output = crf(output)
base_model = Model(inputs, output)
model = ModelWithCRFLoss(base_model, sparse_target=True)
model.compile(optimizer='adam')
model.fit(x=x, y=y, epochs=2, batch_size=2)
Requires TensorFlow >= 2.1.0 and compatible tensorflow-addons version; you must manage version alignment yourself.
Verify before relying
- Whether the package works with current TensorFlow versions (last tested 2021-09-10)
- Compatibility status with recent tensorflow-addons releases
- Performance or correctness issues discovered since abandonment
Package facts
| License | MIT License (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — tensorflow, tensorflow-addons |
| Maintenance | abandoned — 1,799 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 201,989/month — #9,654 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: tf2crf-0.1.33-py2.py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
sklearn-crfsuitesklearn-crfsuite wraps CRFsuite (Conditional…
permissive · top 15,000 on PyPI
python-crfsuitePython binding to CRFsuite for conditional…
permissive · top 5,000 on PyPI
keras-nlpKeras-NLP provides pretrained models and…
permissive · top 15,000 on PyPI
tf-slimTensorFlow-Slim provides high-level layers,…
permissive · top 15,000 on PyPI
tf-keras-nightlyTF-Keras is a pure-TensorFlow implementation of…
permissive · top 5,000 on PyPI
seqevalseqeval evaluates the performance of sequence…
permissive · top 15,000 on PyPI
kerasKeras 3 is a multi-backend deep learning…
permissive · top 5,000 on PyPI
tf-kerasTF-Keras is the pure-TensorFlow implementation…
permissive · top 5,000 on PyPI
keras-nightlyA multi-backend deep learning framework that…
permissive · top 15,000 on PyPI
seqio-nightlySeqIO provides task-based data pipelines for…
permissive · top 15,000 on PyPI