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
嗨,我想这可能是你要找的 https://github.com/DeMoriarty/TorchPQ/blob/main/torchpq/codec/PQCodec.py