| 1 | [**@lancedb/lancedb**](../README.md) • **Docs** |
| 2 | |
| 3 | *** |
| 4 | |
| 5 | [@lancedb/lancedb](../globals.md) / IvfPqOptions |
| 6 | |
| 7 | # Interface: IvfPqOptions |
| 8 | |
| 9 | Options to create an `IVF_PQ` index |
| 10 | |
| 11 | ## Properties |
| 12 | |
| 13 | ### distanceType? |
| 14 | |
| 15 | > `optional` **distanceType**: `"l2"` \| `"cosine"` \| `"dot"` |
| 16 | |
| 17 | Distance type to use to build the index. |
| 18 | |
| 19 | Default value is "l2". |
| 20 | |
| 21 | This is used when training the index to calculate the IVF partitions |
| 22 | (vectors are grouped in partitions with similar vectors according to this |
| 23 | distance type) and to calculate a subvector's code during quantization. |
| 24 | |
| 25 | The distance type used to train an index MUST match the distance type used |
| 26 | to search the index. Failure to do so will yield inaccurate results. |
| 27 | |
| 28 | The following distance types are available: |
| 29 | |
| 30 | "l2" - Euclidean distance. This is a very common distance metric that |
| 31 | accounts for both magnitude and direction when determining the distance |
| 32 | between vectors. L2 distance has a range of [0, ∞). |
| 33 | |
| 34 | "cosine" - Cosine distance. Cosine distance is a distance metric |
| 35 | calculated from the cosine similarity between two vectors. Cosine |
| 36 | similarity is a measure of similarity between two non-zero vectors of an |
| 37 | inner product space. It is defined to equal the cosine of the angle |
| 38 | between them. Unlike L2, the cosine distance is not affected by the |
| 39 | magnitude of the vectors. Cosine distance has a range of [0, 2]. |
| 40 | |
| 41 | Note: the cosine distance is undefined when one (or both) of the vectors |
| 42 | are all zeros (there is no direction). These vectors are invalid and may |
| 43 | never be returned from a vector search. |
| 44 | |
| 45 | "dot" - Dot product. Dot distance is the dot product of two vectors. Dot |
| 46 | distance has a range of (-∞, ∞). If the vectors are normalized (i.e. their |
| 47 | L2 norm is 1), then dot distance is equivalent to the cosine distance. |
| 48 | |
| 49 | *** |
| 50 | |
| 51 | ### maxIterations? |
| 52 | |
| 53 | > `optional` **maxIterations**: `number` |
| 54 | |
| 55 | Max iteration to train IVF kmeans. |
| 56 | |
| 57 | When training an IVF PQ index we use kmeans to calculate the partitions. This parameter |
| 58 | controls how many iterations of kmeans to run. |
| 59 | |
| 60 | Increasing this might improve the quality of the index but in most cases these extra |
| 61 | iterations have diminishing returns. |
| 62 | |
| 63 | The default value is 50. |
| 64 | |
| 65 | *** |
| 66 | |
| 67 | ### numPartitions? |
| 68 | |
| 69 | > `optional` **numPartitions**: `number` |
| 70 | |
| 71 | The number of IVF partitions to create. |
| 72 | |
| 73 | This value should generally scale with the number of rows in the dataset. |
| 74 | By default the number of partitions is the square root of the number of |
| 75 | rows. |
| 76 | |
| 77 | If this value is too large then the first part of the search (picking the |
| 78 | right partition) will be slow. If this value is too small then the second |
| 79 | part of the search (searching within a partition) will be slow. |
| 80 | |
| 81 | *** |
| 82 | |
| 83 | ### numSubVectors? |
| 84 | |
| 85 | > `optional` **numSubVectors**: `number` |
| 86 | |
| 87 | Number of sub-vectors of PQ. |
| 88 | |
| 89 | This value controls how much the vector is compressed during the quantization step. |
| 90 | The more sub vectors there are the less the vector is compressed. The default is |
| 91 | the dimension of the vector divided by 16. If the dimension is not evenly divisible |
| 92 | by 16 we use the dimension divded by 8. |
| 93 | |
| 94 | The above two cases are highly preferred. Having 8 or 16 values per subvector allows |
| 95 | us to use efficient SIMD instructions. |
| 96 | |
| 97 | If the dimension is not visible by 8 then we use 1 subvector. This is not ideal and |
| 98 | will likely result in poor performance. |
| 99 | |
| 100 | *** |
| 101 | |
| 102 | ### sampleRate? |
| 103 | |
| 104 | > `optional` **sampleRate**: `number` |
| 105 | |
| 106 | The number of vectors, per partition, to sample when training IVF kmeans. |
| 107 | |
| 108 | When an IVF PQ index is trained, we need to calculate partitions. These are groups |
| 109 | of vectors that are similar to each other. To do this we use an algorithm called kmeans. |
| 110 | |
| 111 | Running kmeans on a large dataset can be slow. To speed this up we run kmeans on a |
| 112 | random sample of the data. This parameter controls the size of the sample. The total |
| 113 | number of vectors used to train the index is `sample_rate * num_partitions`. |
| 114 | |
| 115 | Increasing this value might improve the quality of the index but in most cases the |
| 116 | default should be sufficient. |
| 117 | |
| 118 | The default value is 256. |