Skip to main content

fast-universal-sentence-encoder

Fast, dependency-light implementation of Google's universal-sentence-encoder-multilingual v3. Same SentencePiece (128k) vocabulary, same DAN + CNN n-gram encoder, same 512-dim L2-normalized output — reproducing the original quantized ONNX at cosine ≥ 0.99 — with no C++ dependencies: a pure-Python port of the normalizer + unigram Viterbi and a pure numpy forward pass.

Install

pip install fast-universal-sentence-encoder

Only needs numpy. (Optionally threadpoolctl via pip install "fast-universal-sentence-encoder[fast]" for faster small-batch matmuls.)

Quickstart

from usem3 import USE

use = USE()
vec = use.encode("o gato preto correu pelo jardim")   # (512,) L2-normalized
vecs = use.encode(["a menina lê um livro", "investir em renda fixa é seguro"])  # (2, 512)

use.similarity("gato preto correu", "o gato preto correu pelo jardim")
# 0.53

The package is distributed as fast-universal-sentence-encoder; the import name is usem3.

Number denoising

USE embeddings are sensitive to number tokens: two sentences that differ only in their quantities get cosine ~0.6-0.8 instead of ~1.0, which can hurt semantic search when the same content appears with different numbers. Normalizing numbers to a placeholder fixes this and is a no-op on clean text:

use = USE(denoise=True)
use.similarity("O projeto custou 1 milhão", "O projeto custou 5 milhões")
# 0.97 (vs 0.80 without denoise)

denoise=True replaces number tokens (digits, separators, %) with a fixed placeholder before embedding. It is OFF by default because it slightly changes the embeddings; enable it when your corpus mixes quantities. See examples/bench_numbers.py for the evaluation.

Model

  • Architecture: deep averaging network (DAN) with CNN n-gram features (orders 2/3/5) followed by a residual DNN, trained multi-task across 16 languages.
  • Output: 512-dim, L2-normalized sentence embeddings.
  • Languages: ar, de, en, es, fr, it, ja, ko, nl, pl, pt, ru, th, tr, zh, zh-TW.
  • Size: the package is ~33 MB (the embedding table is stored 6-bit-quantized per chunk, which halves it with no measurable quality loss).

Files

usem3/
├── resources/
│   ├── sp.model        # sentencepiece model (128k vocab)
│   └── weights.npz     # compact 6-bit-quantized weights
├── backends/numpy.py   # pure numpy forward pass
├── pure_tokenizer.py   # pure-Python sentencepiece port (normalizer + unigram)
├── preprocess.py       # optional number denoising
├── embedder.py         # USE front-end
└── tokenizer.py        # tokenizer front-end (wraps pure_tokenizer)
scripts/build_weights.py  # regenerate weights.npz from float32 weights

License

Apache-2.0. The underlying model is Google's universal-sentence-encoder-multilingual-3 (Apache-2.0).

Download files

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

Source Distribution

fast_universal_sentence_encoder-0.1.0.tar.gz (31.7 MB view details)

Uploaded Source

Built Distribution

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

fast_universal_sentence_encoder-0.1.0-py3-none-any.whl (31.7 MB view details)

Uploaded Python 3

File details

Details for the file fast_universal_sentence_encoder-0.1.0.tar.gz.

File metadata

File hashes

Hashes for fast_universal_sentence_encoder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b4cd8c046bf332662436ac5f8b07217d72187a9187d4b34370e08a18695eaa04
MD5 e94f373fdd206f575a3df3b9af97ae6b
BLAKE2b-256 4cdbfd72af77160d3c9341b89cd0f516c3c455aaee0e9810a8609f7720397f89

See more details on using hashes here.

File details

Details for the file fast_universal_sentence_encoder-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_universal_sentence_encoder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29318e7357053a17a5d82d5612201144fa57ae8e9cffc5001bf0e720c6cb4beb
MD5 ef5796f9ba9f7cc0ba1112777c62ea8d
BLAKE2b-256 65c5292253629f01cb931a7a24ba5211364a582a997d418aab5599bfba6c1823

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.1

2 files

0.2.0

2 files

This release

0.1.0 This release

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