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)

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.1.tar.gz (65.9 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.1-cp310-abi3-win_amd64.whl (373.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

word2vec_matryoshka-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl (559.6 kB view details)

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

word2vec_matryoshka-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl (481.7 kB view details)

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

word2vec_matryoshka-0.1.1-cp310-abi3-macosx_11_0_universal2.whl (855.7 kB view details)

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

word2vec_matryoshka-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (422.1 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: word2vec_matryoshka-0.1.1.tar.gz
  • Upload date:
  • Size: 65.9 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.1.tar.gz
Algorithm Hash digest
SHA256 b565026ec3f69c3dd398cb34fc3cb0dfb6e8d05e258898597a9555d8b157b0a2
MD5 7ff48aade1ce6fb4fe3d107ae7c90e1a
BLAKE2b-256 dd723972083e73eb3c9c34010180189677337e91fc7aa32792a5c98d661c36db

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1.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.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0d563302051c0281e47dd6d85023756eb1a868ce5672c74db660df29b4ce4868
MD5 0a0b502e0e9dcc589ec42ccb46234ce5
BLAKE2b-256 d53f3df172736e6b7fd37311cedf53224bcd9d189a282461ddc520deeb0d1e77

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1-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.1-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3c407fab7980602ad722f6a938fe647d57a4dabd97c066f93e9fdaf17296431b
MD5 3577e0053014542d65096e5ddf308d39
BLAKE2b-256 f35e259bc84f6e684a1aebd5a4af38cca92958ceaf7797e69821cb7fa82feb17

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1-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.1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 af356e10f140c6b0fb662cf6100462b93980f6d1ae15856112cf3054acf423a8
MD5 4850aab330e2b30524b837215f77857c
BLAKE2b-256 57d89417bcfa97e94322f9c577f4dd14f0bce8f390b357cf6e4710382e2c6517

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1-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.1-cp310-abi3-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.1-cp310-abi3-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 bf83fb0e99a35be1292547789fa6c4f854f25645ccac2f5fc2e226784c812a7d
MD5 d5b005eb8a45ed50d0d3ae26daf356e6
BLAKE2b-256 28e874da5efb375eb278038e1bfe30ebfa4d232fb842c4abad1ceec0c91766fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1-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.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 afbe60bf90611151d45ac107ded2e70ee95a1904414925a4a811866e864e0416
MD5 a0cafd690bdc6bcf2e2e4c4f2711d254
BLAKE2b-256 49d9678eeb4d7ca74846d7287919371e4c289b8201534126715eafd26b97cb04

See more details on using hashes here.

Provenance

The following attestation bundles were made for word2vec_matryoshka-0.1.1-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