二進位向量
二進位向量是一種特殊的資料表示形式,可將傳統的高維浮點向量轉換成只包含 0 和 1 的二進位向量。這種轉換不僅壓縮了向量的大小,還降低了儲存和計算成本,同時保留了語義資訊。當非關鍵特徵的精確度不重要時,二進位向量可以有效地維持原始浮點向量的大部分完整性和效用。
二進位向量的應用範圍非常廣泛,尤其是在計算效率和儲存最佳化非常重要的情況下。在大型人工智能系統中,例如搜尋引擎或推薦系統,即時處理大量資料是關鍵。透過減少向量的大小,二進位向量有助於降低延遲和計算成本,而不會大幅犧牲精確度。此外,二進位向量適用於資源有限的環境,例如記憶體和處理能力有限的行動裝置和嵌入式系統。透過二進位向量的使用,複雜的人工智慧功能可以在這些受限的環境中實作,同時維持高效能。
概述
二進位向量是一種將複雜物件(如影像、文字或音訊)編碼成固定長度二進位數值的方法。在 Milvus 中,二進位向量通常表示為位元陣列或位元組陣列。例如,一個 8 維的二進位向量可以表示為[1, 0, 1, 1, 0, 0, 1, 0]
。
下圖顯示二進位向量如何表示文字內容中關鍵字的存在。在這個例子中,一個 10 維的二進位向量用來表示兩個不同的文字(文字 1和文字 2),其中每個維度對應詞彙中的一個字:1 表示文字中存在該字,0 表示不存在該字。
文字內容的二進位向量表示法
二進位向量具有以下特點。
高效儲存:每個維度只需要 1 位元的儲存空間,大幅減少儲存空間。
快速計算:向量之間的相似性可以使用 XOR 等位元運算快速計算。
固定長度:不論原始文字的長度如何,向量的長度都保持不變,讓索引和檢索變得更容易。
簡單直覺:直接反映關鍵字的存在,適合某些專門的檢索任務。
二進位向量可以透過各種方法產生。在文字處理中,可以使用預先定義的詞彙,根據字詞的存在設定相對應的位元。在影像處理中,感知散列演算法 (如pHash) 可以產生影像的二進位特徵。在機器學習應用程式中,模型輸出可進行二進位,以獲得二進位向量表示。
在二進位向量化之後,資料可以儲存在 Milvus 中進行管理和向量檢索。下圖顯示基本流程。
在 Milvus 中使用二進位向量
雖然二進位向量在特定情境中表現優異,但其表達能力有其限制,難以捕捉複雜的語意關係。因此,在現實世界的情境中,二進位向量通常會與其他向量類型一起使用,以平衡效率與表達能力。如需詳細資訊,請參閱密集向量 (Dense Vector) 與稀疏向量 (Sparse Vector)。
在 Milvus 中使用二進位向量
新增向量領域
要在 Milvus 中使用二進位向量,首先在建立集合時定義一個向量欄位來儲存二進位向量。這個過程包括
將
datatype
設定為支援的二進位向量資料類型,即BINARY_VECTOR
。使用
dim
參數指定向量的尺寸。請注意,dim
必須是 8 的倍數,因為二進位向量在插入時必須轉換成位元組。每 8 個布林值 (0 或 1) 會打包成 1 個位元組。例如,如果dim=128
,插入時需要一個 16 位元組的陣列。
from pymilvus import MilvusClient, DataType
client = MilvusClient(uri="http://localhost:19530")
schema = client.create_schema(
auto_id=True,
enable_dynamic_fields=True,
)
schema.add_field(field_name="pk", datatype=DataType.VARCHAR, is_primary=True, max_length=100)
schema.add_field(field_name="binary_vector", datatype=DataType.BINARY_VECTOR, dim=128)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
CreateCollectionReq.CollectionSchema schema = client.createSchema();
schema.setEnableDynamicField(true);
schema.addField(AddFieldReq.builder()
.fieldName("pk")
.dataType(DataType.VarChar)
.isPrimaryKey(true)
.autoID(true)
.maxLength(100)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("binary_vector")
.dataType(DataType.BinaryVector)
.dimension(128)
.build());
import { DataType } from "@zilliz/milvus2-sdk-node";
schema.push({
name: "binary vector",
data_type: DataType.BinaryVector,
dim: 128,
});
export primaryField='{
"fieldName": "pk",
"dataType": "VarChar",
"isPrimary": true,
"elementTypeParams": {
"max_length": 100
}
}'
export vectorField='{
"fieldName": "binary_vector",
"dataType": "BinaryVector",
"elementTypeParams": {
"dim": 128
}
}'
export schema="{
\"autoID\": true,
\"fields\": [
$primaryField,
$vectorField
],
\"enableDynamicField\": true
}"
在這個範例中,新增了一個向量欄位,名為binary_vector
,用來儲存二進位向量。這個欄位的資料類型是BINARY_VECTOR
,維數是 128。
為向量欄位設定索引參數
為了加快搜尋速度,必須為二進位向量欄位建立索引。索引可大幅提升大型向量資料的檢索效率。
index_params = client.prepare_index_params()
index_params.add_index(
field_name="binary_vector",
index_name="binary_vector_index",
index_type="BIN_IVF_FLAT",
metric_type="HAMMING",
params={"nlist": 128}
)
import io.milvus.v2.common.IndexParam;
import java.util.*;
List<IndexParam> indexParams = new ArrayList<>();
Map<String,Object> extraParams = new HashMap<>();
extraParams.put("nlist",128);
indexParams.add(IndexParam.builder()
.fieldName("binary_vector")
.indexType(IndexParam.IndexType.BIN_IVF_FLAT)
.metricType(IndexParam.MetricType.HAMMING)
.extraParams(extraParams)
.build());
import { MetricType, IndexType } from "@zilliz/milvus2-sdk-node";
const indexParams = {
indexName: "binary_vector_index",
field_name: "binary_vector",
metric_type: MetricType.HAMMING,
index_type: IndexType.BIN_IVF_FLAT,
params: {
nlist: 128,
},
};
export indexParams='[
{
"fieldName": "binary_vector",
"metricType": "HAMMING",
"indexName": "binary_vector_index",
"indexType": "BIN_IVF_FLAT",
"params":{"nlist": 128}
}
]'
在上面的範例中,使用BIN_IVF_FLAT
索引類型,為binary_vector
欄位建立一個名為binary_vector_index
的索引。metric_type
設定為HAMMING
,表示使用漢明距離進行相似性測量。
除了BIN_IVF_FLAT
之外,Milvus 還支援二進位向量的其他索引類型。如需詳細資訊,請參閱二進位向量索引。此外,Milvus 也支援二進位向量的其他相似度指標。如需詳細資訊,請參閱度量類型。
建立集合
二進位向量和索引設定完成後,建立一個包含二進位向量的集合。以下範例使用create_collection
方法建立一個名為my_binary_collection
的集合。
client.create_collection(
collection_name="my_binary_collection",
schema=schema,
index_params=index_params
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName("my_binary_collection")
.collectionSchema(schema)
.indexParams(indexParams)
.build();
client.createCollection(requestCreate);
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({
address: 'http://localhost:19530'
});
await client.createCollection({
collection_name: 'my_dense_collection',
schema: schema,
index_params: indexParams
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_binary_collection\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
插入資料
建立集合後,使用insert
方法加入包含二進位向量的資料。請注意,二進位向量應以位元組陣列的形式提供,其中每個位元組代表 8 個布林值。
例如,對於 128 位元的二進位向量,需要一個 16 位元組的陣列 (因為 128 位元 ÷ 8 位元/位元組 = 16 位元組)。以下是插入資料的範例程式碼。
def convert_bool_list_to_bytes(bool_list):
if len(bool_list) % 8 != 0:
raise ValueError("The length of a boolean list must be a multiple of 8")
byte_array = bytearray(len(bool_list) // 8)
for i, bit in enumerate(bool_list):
if bit == 1:
index = i // 8
shift = i % 8
byte_array[index] |= (1 << shift)
return bytes(byte_array)
bool_vectors = [
[1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112,
[0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1] + [0] * 112,
]
data = [{"binary_vector": convert_bool_list_to_bytes(bool_vector) for bool_vector in bool_vectors}]
client.insert(
collection_name="my_binary_collection",
data=data
)
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.response.InsertResp;
private static byte[] convertBoolArrayToBytes(boolean[] booleanArray) {
byte[] byteArray = new byte[booleanArray.length / Byte.SIZE];
for (int i = 0; i < booleanArray.length; i++) {
if (booleanArray[i]) {
int index = i / Byte.SIZE;
int shift = i % Byte.SIZE;
byteArray[index] |= (byte) (1 << shift);
}
}
return byteArray;
}
List<JsonObject> rows = new ArrayList<>();
Gson gson = new Gson();
{
boolean[] boolArray = {true, false, false, true, true, false, true, true, false, true, false, false, true, true, false, true};
JsonObject row = new JsonObject();
row.add("binary_vector", gson.toJsonTree(convertBoolArrayToBytes(boolArray)));
rows.add(row);
}
{
boolean[] boolArray = {false, true, false, true, false, true, false, false, true, true, false, false, true, true, false, true};
JsonObject row = new JsonObject();
row.add("binary_vector", gson.toJsonTree(convertBoolArrayToBytes(boolArray)));
rows.add(row);
}
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName("my_binary_collection")
.data(rows)
.build());
const data = [
{ binary_vector: [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1] },
{ binary_vector: [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1] },
];
client.insert({
collection_name: "my_binary_collection",
data: data,
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/insert" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"data\": $data,
\"collectionName\": \"my_binary_collection\"
}"
執行相似性搜尋
相似性搜尋是 Milvus 的核心功能之一,可讓您根據向量之間的距離,快速找到與查詢向量最相似的資料。若要使用二進位向量執行相似性搜尋,請準備查詢向量和搜尋參數,然後調用search
方法。
在搜尋作業期間,二進位向量也必須以位元組陣列的形式提供。確保查詢向量的維度與定義dim
時指定的維度相符,且每 8 個布林值會轉換成 1 個位元組。
search_params = {
"params": {"nprobe": 10}
}
query_bool_list = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112
query_vector = convert_bool_list_to_bytes(query_bool_list)
res = client.search(
collection_name="my_binary_collection",
data=[query_vector],
anns_field="binary_vector",
search_params=search_params,
limit=5,
output_fields=["pk"]
)
print(res)
# Output
# data: ["[{'id': '453718927992172268', 'distance': 10.0, 'entity': {'pk': '453718927992172268'}}]"]
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.BinaryVec;
import io.milvus.v2.service.vector.response.SearchResp;
Map<String,Object> searchParams = new HashMap<>();
searchParams.put("nprobe",10);
boolean[] boolArray = {true, false, false, true, true, false, true, true, false, true, false, false, true, true, false, true};
BinaryVec queryVector = new BinaryVec(convertBoolArrayToBytes(boolArray));
SearchResp searchR = client.search(SearchReq.builder()
.collectionName("my_binary_collection")
.data(Collections.singletonList(queryVector))
.annsField("binary_vector")
.searchParams(searchParams)
.topK(5)
.outputFields(Collections.singletonList("pk"))
.build());
System.out.println(searchR.getSearchResults());
// Output
//
// [[SearchResp.SearchResult(entity={pk=453444327741536775}, score=0.0, id=453444327741536775), SearchResp.SearchResult(entity={pk=453444327741536776}, score=7.0, id=453444327741536776)]]
query_vector = [1,0,1,0,1,1,1,1,1,1,1,1];
client.search({
collection_name: 'my_binary_collection',
data: query_vector,
limit: 5,
output_fields: ['pk'],
params: {
nprobe: 10
}
});
export searchParams='{
"params":{"nprobe":10}
}'
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_binary_collection\",
\"data\": $data,
\"annsField\": \"binary_vector\",
\"limit\": 5,
\"searchParams\":$searchParams,
\"outputFields\": [\"pk\"]
}"
有關相似性搜尋參數的詳細資訊,請參閱基本 ANN 搜尋。