Skip to main content

Implementation of the graph2vec algorithm

Project description

graph2emb

graph2emb is a lightweight Python package for learning graph embeddings. It combines Node2Vec-style random-walk node embeddings, edge embeddings, and Graph2Vec-style whole-graph embeddings in one project.

The implementation is inspired by:

Features

  • Node2Vec: random-walk based node embeddings.
  • Edge embeddings: Hadamard, average, weighted L1, and weighted L2 edge representations.
  • Graph2Vec: Weisfeiler-Lehman hashing + Doc2Vec-based graph embeddings.
  • Gensim-like APIs: small in-repo Word2Vec, Doc2Vec, and KeyedVectors implementations.
  • uv-friendly workflow for development, testing, and running examples.

Installation

From the project root:

uv add graph2emb

Or install the package in editable mode with pip:

pip install graph2emb

Quick start

Node embeddings with Node2Vec

import networkx as nx
from graph2emb import Node2Vec

# Build a small graph.
graph = nx.fast_gnp_random_graph(n=100, p=0.3, seed=42)

# Precompute transition probabilities and generate random walks.
node2vec = Node2Vec(
    graph,
    dimensions=64,
    walk_length=30,
    num_walks=20,
    workers=1,
    seed=42,
    quiet=True,
)

# Train embeddings from the walks.
model = node2vec.fit(window=10, min_count=1, epochs=5)

# Node ids are stored as strings.
print(model.wv.most_similar("2", topn=5))

Edge embeddings

from graph2emb.edges import HadamardEmbedder, AverageEmbedder

# Reuse a trained Node2Vec model.
hadamard = HadamardEmbedder(model.wv, quiet=True)
average = AverageEmbedder(model.wv, quiet=True)

print(hadamard[("1", "2")])
print(average[("1", "2")])

Graph embeddings with Graph2Vec

import networkx as nx
from graph2emb import Graph2Vec

graphs = [
    nx.fast_gnp_random_graph(n=12, p=0.3, seed=1),
    nx.fast_gnp_random_graph(n=14, p=0.2, seed=2),
]

graph2vec = Graph2Vec(
    dimensions=32,
    wl_iterations=2,
    workers=1,
    min_count=1,
    epochs=3,
    seed=42,
)
graph2vec.fit(graphs)

embeddings = graph2vec.get_embedding()
print(embeddings.shape)  # (2, 32)

Running the sample scripts

The sample/ directory contains small runnable examples with intentionally small parameters.

# Node2Vec from an edge-list file
uv run python sample/node2vec_from_edgelist.py

# Node2Vec + edge embeddings
uv run python sample/node2vec_edge_embeddings.py

# Graph2Vec
uv run python sample/graph2vec_basic.py

Outputs are written to sample/out/.

Notes

  • Node labels are converted to strings in generated walks and learned KeyedVectors.
  • Weighted Node2Vec graphs use the weight edge attribute by default. Override this with weight_key="...".
  • Edge weights, p, and q must be finite positive numbers.
  • For MultiGraph inputs, parallel edge weights are validated and summed.

Development

# Run tests
uv run pytest

# Run tests in parallel
uv run pytest -n auto

# Run coverage
uv run pytest --cov=graph2emb --cov-report=term-missing

# Build package artifacts
uv build

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

graph2emb-1.0.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

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

graph2emb-1.0.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file graph2emb-1.0.0.tar.gz.

File metadata

  • Download URL: graph2emb-1.0.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for graph2emb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f3b12c3091d914e70f42c7f50bc0b643fa5d13f469bd3f21bdaf9b6d11afad9f
MD5 aa60bb3ecc67c6e307e2ae51d854d973
BLAKE2b-256 74373571bcd2421e00d800ffe32208b867da5ffb11166d87a7c9fc0a42b075dd

See more details on using hashes here.

File details

Details for the file graph2emb-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: graph2emb-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for graph2emb-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 247118c9622016fe5fb7e72d4608ac4d63f9a62e322d2fc9006ffd335fe919c3
MD5 5e8fdde4b31b9d8216f0a84b0e92169c
BLAKE2b-256 8708a82182abda2eb99ae19bc68b2a6eb752d46ef810fee34a209afca93d79da

See more details on using hashes here.

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