Skip to main content

Discrete Distribution Network

Project description

Discrete Distribution Network

Exploration into Discrete Distribution Network, by Lei Yang out of Beijing

Besides the split-and-prune, may also throw in an option for crossover (mixing of top 2 nodes to replace the pruned)

Install

$ discrete-distribution-network

Usage

import torch
from discrete_distribution_network import DDN

ddn = DDN(
    dim = 32,
    image_size = 256
)

images = torch.randn(2, 3, 256, 256)

loss = ddn(images)
loss.backward()

# after much training

sampled = ddn.sample(batch_size = 1)

assert sampled.shape == (1, 3, 256, 256)

The proposed GuidedSampler in the paper

import torch
from discrete_distribution_network import GuidedSampler

sampler = GuidedSampler(
    dim = 16,              # feature dimension
    dim_query = 3,         # the query image dimension
    codebook_size = 10,    # the codebook size K in the paper, which is K separate projections of the features, which is then measured distance wise to the query image guide
)

features = torch.randn(20, 16, 32, 32)
query_image = torch.randn(20, 3, 32, 32)

out, codes, commit_loss = sampler(features, query_image)

# (20, 3, 32, 32), (20,), ()

assert torch.allclose(sampler.forward_for_codes(features, codes), out, atol = 1e-5)

# after optimizer step, this needs to be called
# there is also a helper function by the same name that can take in your module and will invoke all of the guided samplers

sampler.split_and_prune_()

Oxford flowers

Install uv, which will probably become the default in the near future

$ pip install uv

Then

$ uv run train_oxford_flowers.py

Citations

@misc{yang2025discretedistributionnetworks,
    title   = {Discrete Distribution Networks}, 
    author  = {Lei Yang},
    year    = {2025},
    eprint  = {2401.00036},
    archivePrefix = {arXiv},
    primaryClass = {cs.CV},
    url     = {https://arxiv.org/abs/2401.00036}, 
}

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

discrete_distribution_network-0.1.15.tar.gz (676.4 kB view details)

Uploaded Source

Built Distribution

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

discrete_distribution_network-0.1.15-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file discrete_distribution_network-0.1.15.tar.gz.

File metadata

File hashes

Hashes for discrete_distribution_network-0.1.15.tar.gz
Algorithm Hash digest
SHA256 519efea447828a3eaa08f4fbe35b9e2e83ab43f169234fa7b04167ca84915ea9
MD5 045e3370c3b9eae6939fe6d78d35d0da
BLAKE2b-256 e79667adabddf16277c759c0a69d3b8e01e9dd7115fcdf2debf34c1b8a902177

See more details on using hashes here.

File details

Details for the file discrete_distribution_network-0.1.15-py3-none-any.whl.

File metadata

File hashes

Hashes for discrete_distribution_network-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 8da83dcbbec143d8e9437a09d8e18df97aa70b393b9850a2a85c4c3c1f4b5a7e
MD5 f8cb406b8d93bd0834d6fe63f7fb2826
BLAKE2b-256 cbf17bfb321f55f0a7d85a3eb4d6fe4d6bad11e6c2069ae3a3ef82e69a3dfda5

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