Skip to main content

Word2Vec with Matryoshka Representation Learning (Rust+PyO3), gensim-style API, numpy interop.

Project description

word2vec‑matryoshka

Lightweight Word2Vec for Python, backed by Rust (PyO3). It implements a small, practical subset of Word2Vec (Skip‑gram/CBOW with Negative Sampling or Hierarchical Softmax) and adds Matryoshka multi‑level representations: you can train on multiple prefix dimensions and query a prefix level without recomputing vectors.

This project is a learning/practice (“vibe coding”) effort inspired by gensim. It intentionally covers only a tiny part of gensim’s functionality and adds Matryoshka prefix representations. If you need a full‑featured Word2Vec, please try gensim first.

Install

pip install word2vec-matryoshka

Quick start

from word2vec_matryoshka import Word2Vec, KeyedVectors, set_seed

set_seed(42)
corpus = [["hello", "world"], ["computer", "science"]]

model = Word2Vec(
    sentences=corpus,
    vector_size=64,
    window=5,
    min_count=1,
    workers=2,
    negative=5,
    sg=1,    # 1=Skip-gram, 0=CBOW
    hs=0,    # 1=Hierarchical Softmax, 0=Negative Sampling
    levels=[16, 32, 64],  # Matryoshka prefix dimensions
)

# Full vector
vec = model.wv["hello"]

# Top-5 similar at a prefix level (e.g., 32 dims)
sims = model.wv.most_similar("hello", topn=5, level=32)

# Save/load vectors (base path creates .vocab.json + .npy)
model.wv.save("vectors")
wv = KeyedVectors.load("vectors", mmap="r")  # zero-copy memmap
vec16 = wv.get_vector("hello", level=16)

# Inspect vocabulary and the vector matrix
print(wv.index_to_key[:5])    # e.g., ['hello', 'computer', ...]
M = wv.vectors                # numpy.ndarray with shape (n_words, vector_size)
print(M.shape)

What’s inside

  • Word2Vec training: Skip‑gram or CBOW with Negative Sampling or Hierarchical Softmax
  • Matryoshka levels: train multiple prefix dimensions and query by level
  • Vector I/O: KeyedVectors.save(base)base.vocab.json + base.npy; KeyedVectors.load(base, mmap='r') for fast, memory‑mapped reads
  • Determinism: set_seed(seed) for reproducible runs

Scope and disclaimer

  • Not a drop‑in replacement for gensim. Only a minimal API is provided (Word2Vec, KeyedVectors, wv[...], most_similar, get_vector).
  • Built as a small practice project to explore PyO3/Rust performance and Matryoshka prefix training.
  • For most production needs, start with gensim first, then evaluate if this project’s Matryoshka feature helps your use case.

License

MIT — see LICENSE.

Project details


Download files

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

Source Distribution

word2vec_matryoshka-0.1.2.tar.gz (66.7 kB view details)

Uploaded Source

Built Distributions

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

word2vec_matryoshka-0.1.2-cp310-abi3-win_amd64.whl (376.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

word2vec_matryoshka-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl (562.2 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

word2vec_matryoshka-0.1.2-cp310-abi3-manylinux_2_28_x86_64.whl (484.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_universal2.whl (858.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ universal2 (ARM64, x86-64)

word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_arm64.whl (422.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file word2vec_matryoshka-0.1.2.tar.gz.

File metadata

  • Download URL: word2vec_matryoshka-0.1.2.tar.gz
  • Upload date:
  • Size: 66.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for word2vec_matryoshka-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4c478ca8166711f9f2ccefab6bc9d51d021b21627d817dd4324421e6a0aee956
MD5 7fa0aa61aeacd200a5fc61e88ad880a2
BLAKE2b-256 01d89d33e54caffeff38a3282cfb7d343557d582a887a08c23d34fa53be242cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2.tar.gz:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file word2vec_matryoshka-0.1.2-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 edcaf7edbc3372a618977562ca488811f11b556853e435de966e8307902146d2
MD5 64625fb9dbd9766d76dd9cdd71e2c80a
BLAKE2b-256 b3480d6965551664585e51c82307c651635f5664ccdf45979b7c33513b1ea491

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2-cp310-abi3-win_amd64.whl:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file word2vec_matryoshka-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cf2e21dce1fe7bea5b2b94281b101e4b42c4dadff08913ef1c055aa1602cf0a9
MD5 b795f8a2dce26a495305f9f48c32894c
BLAKE2b-256 9a7ba266cbc6d80684266e512214510230a9933c4684d0f31683892d5bd296cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file word2vec_matryoshka-0.1.2-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.2-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d10b3fdd7e9a23f208075656edbc3e45223360ec76bfd074c3fd5933ebd9d091
MD5 8d0fe0377f84f393d0a961ae94d23825
BLAKE2b-256 444906b6e6573e3e17674f929eac2f19afda468122c4cab306fc01eafd59553f

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 64a0925ec7bb453043a33121a7b267c75364b5869a5276991bb1bf9c97d22eb2
MD5 bab21dfebf34c1cb94bfd0275f7aa2a6
BLAKE2b-256 6dc25189aa4aacb3906605d1dbde4740f5758cd62fb4d22ed2f77575376b03b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_universal2.whl:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 afc637f70d70ebf864a38f96577e3030a34eb847588cb8a958487c2979abe0f0
MD5 ff817bd152ce6e1da7d5e8dc4c5e32b7
BLAKE2b-256 9a5479154784dfc7d68af7d1a4a931026009912f733375d90bfe75be7063ae96

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.2-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on feisan/word2vec_matryoshka

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page