DeMoriarty / TorchPQ

Approximate nearest neighbor search with product quantization on GPU in pytorch and cuda
MIT License
208 stars 20 forks source link

您好 请问下我只想使用PQ方法,即论文中公式11之前的方法。用于将tensor聚类存储,计算的时候并能索引到。请问我应该怎么调用API #21

Open LiZhangMing opened 1 year ago

DeMoriarty commented 1 year ago

嗨,我想这可能是你要找的 https://github.com/DeMoriarty/TorchPQ/blob/main/torchpq/codec/PQCodec.py

from torchpq.codec import PQCodec
codec = PQCodec(d_vector, n_subvectors, n_clusters)

d_vector = 128 # vector dimension
n_vectors = 10000 # number of vectors
n_subvectors = 8 # number of subvectors used in PQ; variable $m$ in the original paper
n_clusters = 256 # number of clusters used in kmeans; variable $k^*$ in the original paper

x = torch.randn(d_vector, n_vectors, device="cuda:0")
codec.train(x) # train the codec, you can use a seperate trainset
code = codec.encode(x) # quantize x
reconstruction = codec.decode(code) # dequantize x