lucidrains / lucidrains/vector-quantize-pytorch

How to get the original code by saved codebook and index

Open
#102 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4k
Forks
338
PR merge metrics
No merged PRs in 30d

Description

Hi, I am trying to get quantized by saved codebook and index, but self.codebooks will always get 0. How can I do it?
```
import torch
from vector_quantize_pytorch import ResidualVQ

residual_vq = ResidualVQ(
dim = 256,
codebook_size = 256,
num_quantizers = 2,
kmeans_init = True,
kmeans_iters = 10
)

x = torch.randn(1, 1024, 256)

residual_vq.eval()

quantized, indices, commit_loss = residual_vq(x)

codebook = residual_vq.codebooks # get the codebook

torch.save(dict(codebook = codebook, indices = indices), 'codebook.pt')
codebook = torch.load('codebook.pt')['codebook']
indices = torch.load('codebook.pt')['indices']

residual_vq_reinit = ResidualVQ(
dim = 256,
codebook_size = 256,
num_quantizers = 2,
kmeans_init = True,
kmeans_iters = 10
)
residual_vq_reinit.codebook = codebook

quantized_out = residual_vq_reinit.get_codes_from_indices(indices)

assert torch.all(quantized == quantized_out.sum(dim = 0))
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproduction with ResidualVQ and inspect the codebooks attribute alongside get_codes_from_indices. Trace how the saved codebook is stored and how the reinitialized instance reads its codebook, then verify that reconstruction from the saved indices matches the original quantized output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.