Skip to main content

megashtein

In their paper "Deep Squared Euclidean Approximation to the Levenshtein Distance for DNA Storage", Guo et al. explore techniques for using a neural network to embed sequences in such a way that the squared Euclidean distance between embeddings approximates the Levenshtein distance between the original sequences. This implementation also takes techniques from "Levenshtein Distance Embeddings with Poisson Regression for DNA Storage" by Wei et al. (2023).

This is valuable because there are excellent libraries for doing fast GPU accelerated searches for the K nearest neighbors of vectors, like faiss. Algorithms like HNSW allow us to do these searches in logarithmic time, where a brute force levenshtein distance based fuzzy search would need to run in exponential time.

This repo contains a PyTorch implementation of the core ideas from Guo's paper, adapted for ASCII sequences rather than DNA sequences. The implementation includes:

  • A convolutional neural network architecture for sequence embedding
  • Training using Poisson regression loss (PNLL) as described in the paper
  • Synthetic data generation with controlled edit distance relationships
  • Model saving and loading functionality

The trained model learns to embed ASCII strings such that the squared Euclidean distance between embeddings approximates the true Levenshtein distance between the strings.

Model Architecture

The model uses a 5-layer CNN with average pooling followed by fully connected layers to produce fixed-size embeddings from variable-length ASCII sequences.

Installation

pip install megashtein

Usage

import torch
from megashtein import load_model, embed_string

# Load the pre-trained model
model = load_model()

# Example usage
text1 = "hello world"
text2 = "hello word"

# Embed strings
emb1 = embed_string(model, text1)
emb2 = embed_string(model, text2)

# Compute approximate edit distance
approx_distance = torch.sum((emb1 - emb2) ** 2).item()
print(f"Approximate edit distance: {approx_distance}")

Use Cases

  • Fuzzy String Search: Find similar strings in large text collections
  • Text Clustering: Group similar texts based on edit distance
  • Data Deduplication: Identify near-duplicate text entries
  • Approximate String Matching: Fast similarity search with controllable accuracy

Limitations

  • Fixed Length: Input sequences must be exactly 80 characters (padded/truncated)
  • ASCII Only: Limited to ASCII character set (0-127)
  • Approximation: Provides approximate rather than exact edit distances

Metadata

Release files for megashtein 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for megashtein 0.1.5
File Size Uploaded Provenance Actions
megashtein-0.1.5.tar.gz 543.5 kB With provenance Details for megashtein-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for megashtein 0.1.5
File Interpreter ABI Platform Actions
megashtein-0.1.5-py3-none-any.whl Python 3 none any Details for megashtein-0.1.5-py3-none-any.whl

Total release size: 1.1 MB

Release files / megashtein-0.1.5.tar.gz

Download URL megashtein-0.1.5.tar.gz
Size 543.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3348eb4d685d4900a86cde59b154cb8e837746ce1beea906b4687e6a46b43873
BLAKE2b-256 checksum
How to use checksums
ef6b09b56213cf144371bc7c759e426dfa8192e6eb89710493c110d1f2fd4134
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2025.

Transparency log

Release files / megashtein-0.1.5-py3-none-any.whl

Download URL megashtein-0.1.5-py3-none-any.whl
Size 514.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c11a160c5545293998d94cc49a2ec3f6df95e888dd7822ff26477cbba9a8792
BLAKE2b-256 checksum
How to use checksums
c64897093104457223f0073e8bb3560af9f2bb7b5c8688e8e22a9617a33a861a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2025.

Transparency log

Security for megashtein 0.1.5

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for megashtein-0.1.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2025.

Transparency log

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page