Skip to main content

Tranformers-CRF

For training BERT-CRF models with the huggingface Transformers libary

Instalation

    git clone https://bitbucket.org/avisourgente/transformers_crf.git
    cd transformers_crf
    pip install -e .

Train example

Train script is in examples/run_ner.py It follows the api of https://github.com/huggingface/transformers/blob/main/examples/pytorch/token-classification/run_ner.py New args:

    --learning_rate_ner LEARNING_RATE_NER
                Custom initial learning rate for the CRF and Linear layers on AdamW. (default: None)
    --weight_decay_ner WEIGHT_DECAY_NER
                Custom weight decay for the CRF and Linear layers on AdamW. (default: None)
    --use_crf  
                Will enable to use CRF layer. (default: False)
    --no_constrain_crf
                Set to not to constrain crf outputs to labeling scheme (default: False)
    --break_docs_to_max_length [BREAK_DOCS_TO_MAX_LENGTH]
                Whether to chunck docs into sentences with the max seq length of tokenizer. (default: False)
    --convert_to_iobes [CONVERT_TO_IOBES]
                Convert a IOB2 input to IOBES. (default: False)

Example:

python run_ner.py \
  --model_name_or_path neuralmind/bert-base-portuguese-cased \
  --dataset_name eduagarcia/portuguese_benchmark \
  --dataset_config_name harem-default \
  --per_device_train_batch_size 16 \
  --per_device_eval_batch_size 16 \
  --num_train_epochs 15 \
  --learning_rate 5e-5 \
  --do_train \
  --do_eval \
  --do_predict \
  --evaluation_strategy steps \
  --eval_steps 500 \
  --output_dir /workspace/models/test-transformers-crf \
  --max_seq_length 128 \
  --break_docs_to_max_length \
  --overwrite_output_dir \
  --learning_rate_ner 5e-3 \
  --convert_to_iobes \
  --use_crf

Usage example

    import torch
    from transformers_crf import CRFTokenizer, AutoModelForEmbedderCRFTokenClassification

    model_path = "./model"

    device = torch.device('cpu') 
    tokenizer = CRFTokenizer.from_pretrained(model_path)
    model = AutoModelForEmbedderCRFTokenClassification.from_pretrained(model_path).to(device)

    tokens = [["Esse", "é", "um", "exemplo"], ["Esse", "é", "um", "segundo", "exemplo"]]
    batch = tokenizer(tokens, max_length=512).to(device)
    output = model(**batch, reorder=True)
    predicts_id = output.predicts.detach().cpu().numpy()
    preds = [[model.config.id2label[p] for p in pred_seq][:len(token_seq)] for (pred_seq, token_seq) in zip(predicts_id, tokens)]

Pip upload package

python setup.py bdist_wheel

python -m twine upload dist/*

Metadata

Release files for transformers-crf 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for transformers-crf 0.3.3
File Size Uploaded Provenance Actions
transformers_crf-0.3.3.tar.gz 59.1 kB Without provenance Details for transformers_crf-0.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for transformers-crf 0.3.3
File Interpreter ABI Platform Actions
transformers_crf-0.3.3-py3-none-any.whl Python 3 none any Details for transformers_crf-0.3.3-py3-none-any.whl

Total release size: 104.1 kB

Release files / transformers_crf-0.3.3.tar.gz

Download URL transformers_crf-0.3.3.tar.gz
Size 59.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6dfe6600c18c0725a92ace4490fb2fdc927e998b3b008670844109fd79588f4e
BLAKE2b-256 checksum
How to use checksums
830e2993d1c9d6b9328fed7889aad9f92763c4026eb072ba9d443e1931e6492f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / transformers_crf-0.3.3-py3-none-any.whl

Download URL transformers_crf-0.3.3-py3-none-any.whl
Size 45.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5489e9da1582ec21d46d3d90c5d9dd3c1371732cfbfc330e92537d88fc59779
BLAKE2b-256 checksum
How to use checksums
3e70d76a45c8519d74d504695f153bfae5230b8fb3fec5f1337a00dbdbb185d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for transformers-crf 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page