Skip to main content

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

Project description

word2vec‑matryoshka

Ask DeepWiki

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.4.tar.gz (68.2 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.4-cp310-abi3-win_amd64.whl (381.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

word2vec_matryoshka-0.1.4-cp310-abi3-musllinux_1_2_x86_64.whl (568.5 kB view details)

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

word2vec_matryoshka-0.1.4-cp310-abi3-manylinux_2_28_x86_64.whl (489.7 kB view details)

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

word2vec_matryoshka-0.1.4-cp310-abi3-macosx_11_0_universal2.whl (866.8 kB view details)

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

word2vec_matryoshka-0.1.4-cp310-abi3-macosx_11_0_arm64.whl (425.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: word2vec_matryoshka-0.1.4.tar.gz
  • Upload date:
  • Size: 68.2 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.4.tar.gz
Algorithm Hash digest
SHA256 fb73318ce328d3f2b0d674702e75b96f6d2659672f612ad64c8f901deb5b69d9
MD5 73cafdbc3ad52d67ec039f60f924ab1d
BLAKE2b-256 b9ae5c9822f61e7452f3b13d34469c60d6408f0f4eedbf985e2ca8f35230cd5d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 275c5d7f03579965243035f9404510b79ccab122e7fb4f8a899326b49fc8cd06
MD5 f3d37a94e09344dbb5fcf54603390268
BLAKE2b-256 64dd2a000c386c76550f1fb0542f2cafe7b25d22c737eab96b4dd3adf9449d22

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.4-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c515f801e151b5d9ebb5fb3c04ab0af6b65e4512a4a4579a6ca101af192794fb
MD5 be942f58af30188f2ccd7a98e3fc7a20
BLAKE2b-256 5e0c1bf4d25d8f77e1684615825a78271d1ec6ff1afc686445a6f1f82f635640

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.4-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 03020b90f23a8391020fc358f758968a123c4c1a2f6b27204bb19aa13a4ae24f
MD5 5acacc881d21190444b026b87cf66af9
BLAKE2b-256 f8b2b326438dc3915015123b38c31d25547f2f49cd2062d7a3bab3a54863fb8c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.4-cp310-abi3-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 305ff567899b817934685a4403750376126d5dd3fc6f5d425076ffca3240cf4f
MD5 6a24234dfc93794b9ffcb01095764496
BLAKE2b-256 68cde398d15d8a62c85b2639bf1a4e02ca3d2c8c91846cff91f10b5f6b834e09

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for word2vec_matryoshka-0.1.4-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 23283a7a1038231b9160f8256b049be496f0cdb9c67154eb8433926ff555b5af
MD5 2b94c1aa63b3be3610659a51b0029aef
BLAKE2b-256 702a94b06ff02fac32529be0ed1724bb395efdad2f38f130f61cf79ace989dee

See more details on using hashes here.

Provenance

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