scikit-embeddings
Utilites for training, storing and using word and document embeddings in scikit-learn pipelines.
Features
- Train Word and Paragraph embeddings in scikit-learn compatible pipelines.
- Fast and performant trainable tokenizer components from
tokenizers. - Easy to integrate components and pipelines in your scikit-learn workflows and machine learning pipelines.
- Easy serialization and integration with HugginFace Hub for quickly publishing your embedding pipelines.
What scikit-embeddings is not for:
- Training transformer models and deep neural language models (if you want to do this, do it with transformers)
- Using pretrained sentence transformers (use embetter)
Installation
You can easily install scikit-embeddings from PyPI:
pip install scikit-embeddings
If you want to use GloVe embedding models, install alogn with glovpy:
pip install scikit-embeddings[glove]
Example Pipelines
You can use scikit-embeddings with many many different pipeline architectures, I will list a few here:
Word Embeddings
You can train classic vanilla word embeddings by building a pipeline that contains a WordLevel tokenizer and an embedding model:
from skembedding.tokenizers import WordLevelTokenizer
from skembedding.models import Word2VecEmbedding
from skembeddings.pipeline import EmbeddingPipeline
embedding_pipe = EmbeddingPipeline(
WordLevelTokenizer(),
Word2VecEmbedding(n_components=100, algorithm="cbow")
)
embedding_pipe.fit(texts)
Fasttext-like
You can train an embedding pipeline that uses subword information by using a tokenizer that does that.
You may want to use Unigram, BPE or WordPiece for these purposes.
Fasttext also uses skip-gram by default so let's change to that.
from skembedding.tokenizers import UnigramTokenizer
from skembedding.models import Word2VecEmbedding
from skembeddings.pipeline import EmbeddingPipeline
embedding_pipe = EmbeddingPipeline(
UnigramTokenizer(),
Word2VecEmbedding(n_components=250, algorithm="sg")
)
embedding_pipe.fit(texts)
Paragraph Embeddings
You can train Doc2Vec paragpraph embeddings with the chosen choice of tokenization.
from skembedding.tokenizers import WordPieceTokenizer
from skembedding.models import ParagraphEmbedding
from skembeddings.pipeline import EmbeddingPipeline, PretrainedPipeline
embedding_pipe = EmbeddingPipeline(
WordPieceTokenizer(),
ParagraphEmbedding(n_components=250, algorithm="dm")
)
embedding_pipe.fit(texts)
Serialization
Pipelines can be safely serialized to disk:
embedding_pipe.to_disk("output_folder/")
pretrained = PretrainedPipeline("output_folder/")
Or published to HugginFace Hub:
from huggingface_hub import login
login()
embedding_pipe.to_hub("username/name_of_pipeline")
pretrained = PretrainedPipeline("username/name_of_pipeline")
Text Classification
You can include an embedding model in your classification pipelines by adding some classification head.
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
X_train, X_test, y_train, y_test = train_test_split(X, y)
cls_pipe = make_pipeline(pretrained, LogisticRegression())
cls_pipe.fit(X_train, y_train)
y_pred = cls_pipe.predict(X_test)
print(classification_report(y_test, y_pred))
Metadata
Release files for scikit-embeddings 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| scikit_embeddings-0.3.1.tar.gz | 13.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| scikit_embeddings-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.9 kB
Release files / scikit_embeddings-0.3.1.tar.gz
| Download URL | scikit_embeddings-0.3.1.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ea068a598e062cc7f189e86daec943d4d7de8e72b880d651d2ec9952efc5c44
|
|
BLAKE2b-256 checksum How to use checksums |
5b7dac7b69a1ca32debd3f98fc2f67edad66aab265fc386e26bd0f62b05fb1f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.8 Linux/5.15.0-84-generic
|
Release files / scikit_embeddings-0.3.1-py3-none-any.whl
| Download URL | scikit_embeddings-0.3.1-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dea3ba22ee448bdaf9df2019f1f927d83ae75f9c64e24c2258b21f7dbd4517ce
|
|
BLAKE2b-256 checksum How to use checksums |
c846e25c0757f4fcb59f755b06bdbb2298ad5f7e94f444035296bcd1468da454
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.8 Linux/5.15.0-84-generic
|
Security for scikit-embeddings 0.3.1
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.