skillfed

tf2crf

a crf layer for tensorflow 2 keras

tf2crf v0.1.33 202.0K downloads/30d#9,654 on PyPI69
Permissive license MIT License Abandoned released

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 tf2crf

uv

uv add tf2crf

poetry

poetry add tf2crf

Installing 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

CRF layer tensorflow kerassequence labeling tensorflowconditional random field kerasNLP sequence tagging tensorflowtensorflow CRF implementationkeras masking CRFnamed entity recognition tensorflow
sequence-labelingnlpabandoned

More Artificial Intelligence packages

Further reading