Skip to main content

Neural network-based string similarity using squared Euclidean approximation to Levenshtein distance

Project description

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

  • Base Architecture: Convolutional Neural Network with embedding layer
  • Input: ASCII sequences up to 80 characters (padded with null characters)
  • Output: 80-dimensional dense embeddings
  • Vocab Size: 128 (ASCII character set)
  • Embedding Dimension: 140

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}")

Training Details

The model is trained using:

  • Loss Function: Poisson Negative Log-Likelihood (PNLL)
  • Optimizer: AdamW with learning rate 0.000817
  • Batch Size: 32
  • Sequence Length: 80 characters (fixed)
  • Synthetic Data: Pairs of ASCII strings with known Levenshtein distances

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

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

megashtein-0.1.1.tar.gz (42.7 kB view details)

Uploaded Source

Built Distribution

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

megashtein-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: megashtein-0.1.1.tar.gz
  • Upload date:
  • Size: 42.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for megashtein-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5bc729b87d0bea1c73b55b3d9cb37e7150c819de99677ef1e843ef23ba071370
MD5 367d6c9760c8d560ec506e6e823f6e5f
BLAKE2b-256 687df4d45925468feb7a06eee295275db40879d0bea52e90056acee485cdc770

See more details on using hashes here.

Provenance

The following attestation bundles were made for megashtein-0.1.1.tar.gz:

Publisher: publish.yml on Sawyer-Powell/megashtein

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

File details

Details for the file megashtein-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: megashtein-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for megashtein-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8744a2c66e6945838dbc604054affcde05f15d5d1b98cd79fc0f5f5fe5de724
MD5 859449e226d836bcc79646849049b4e8
BLAKE2b-256 22ec46f48d06cf0c1499a8ccd2c981954d0c95616e56dcd56a7a5b826b85cf1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for megashtein-0.1.1-py3-none-any.whl:

Publisher: publish.yml on Sawyer-Powell/megashtein

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