Skip to main content

Transformer Embeddings

PyPI Status Python Version License

Tests

pre-commit Black

This library simplifies and streamlines the usage of encoder transformer models supported by HuggingFace's transformers library (model hub or local) to generate embeddings for string inputs, similar to the way sentence-transformers does.

Please note that starting with v4, we have dropped support for Python 3.7. If you need to use this library with Python 3.7, the latest compatible release is version 3.1.0.

Why use this over HuggingFace's transformers or sentence-transformers?

Under the hood, we take care of:

  1. Can be used with any model on the HF model hub, with sensible defaults for inference.
  2. Setting the PyTorch model to eval mode.
  3. Using no_grad() when doing the forward pass.
  4. Batching, and returning back output in the format produced by HF transformers.
  5. Padding / truncating to model defaults.
  6. Moving to and from GPUs if available.

Installation

You can install Transformer Embeddings via pip from PyPI:

$ pip install transformer-embeddings

Usage

from transformer_embeddings import TransformerEmbeddings

transformer = TransformerEmbeddings("model_name")

If you have a previously instantiated model and / or tokenizer, you can pass that in.

transformer = TransformerEmbeddings(model=model, tokenizer=tokenizer)
transformer = TransformerEmbeddings(model_name="model_name", model=model)

or

transformer = TransformerEmbeddings(model_name="model_name", tokenizer=tokenizer)

Note: The model_name should be included if only 1 of model or tokenizer are passed in.

Embeddings

To get output embeddings:

embeddings = transformer.encode(["Lorem ipsum dolor sit amet",
                                 "consectetur adipiscing elit",
                                 "sed do eiusmod tempor incididunt",
                                 "ut labore et dolore magna aliqua."])
embeddings.output

Pooled Output

To get pooled outputs:

from transformer_embeddings import TransformerEmbeddings, mean_pooling

transformer = TransformerEmbeddings("model_name", return_output=False, pooling_fn=mean_pooling)

embeddings = transformer.encode(["Lorem ipsum dolor sit amet",
                                "consectetur adipiscing elit",
                                "sed do eiusmod tempor incididunt",
                                "ut labore et dolore magna aliqua."])

embeddings.pooled

Exporting the Model

Once you are done testing and training the model, it can be exported into a single tarball:

from transformer_embeddings import TransformerEmbeddings

transformer = TransformerEmbeddings("model_name")
transformer.export(additional_files=["/path/to/other/files/to/include/in/tarball.pickle"])

This tarball can also be uploaded to S3, but requires installing the S3 extras (pip install transformer-embeddings[s3]). And then using:

from transformer_embeddings import TransformerEmbeddings

transformer = TransformerEmbeddings("model_name")
transformer.export(
    additional_files=["/path/to/other/files/to/include/in/tarball.pickle"],
    s3_path="s3://bucket/models/model-name/date-version/",
)

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the Apache 2.0 license, Transformer Embeddings is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was partly generated from @cjolowicz's Hypermodern Python Cookiecutter template.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transformer_embeddings-4.0.6.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transformer_embeddings-4.0.6-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file transformer_embeddings-4.0.6.tar.gz.

File metadata

  • Download URL: transformer_embeddings-4.0.6.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.31.0 requests-toolbelt/0.10.1 urllib3/1.26.17 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/24.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.8.18

File hashes

Hashes for transformer_embeddings-4.0.6.tar.gz
Algorithm Hash digest
SHA256 94151dcaabd3ff7ea6017c69aa0676b895e71bece8f7554c673ceb40b3caf540
MD5 36050f4b47af8581cccb36e8d670e1dc
BLAKE2b-256 0f8c0ad2554567236065d3ef73c84a12dafcffedf8384b000e5ec6558dd457e9

See more details on using hashes here.

File details

Details for the file transformer_embeddings-4.0.6-py3-none-any.whl.

File metadata

  • Download URL: transformer_embeddings-4.0.6-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.31.0 requests-toolbelt/0.10.1 urllib3/1.26.17 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/24.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.8.18

File hashes

Hashes for transformer_embeddings-4.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fe088beb1372c7c29852b1589a3f3c5d4801bf3a78a7cdca6d2f0ca7d12b4e7e
MD5 6b7e64c3a7fdbee1774f6fee9e411db8
BLAKE2b-256 c368644d33bd41750c134b9b83de946f00d47df3a72b27981abef78204a096b4

See more details on using hashes here.

Release history Release notifications | RSS feed

4.0.14

2 files

4.0.13

2 files

4.0.12

2 files

4.0.11

2 files

4.0.10

2 files

4.0.9

2 files

4.0.8

2 files

4.0.7

2 files

This release

4.0.6 This release

2 files

4.0.5

2 files

4.0.4

2 files

4.0.3

2 files

4.0.2

2 files

4.0.1

2 files

4.0.0

2 files

3.1.1

2 files

3.1.0

2 files

3.0.6

2 files

3.0.5

2 files

3.0.4

2 files

3.0.3

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

0.0.1

2 files

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