密集向量
密集向量是廣泛用於機器學習和資料分析的數值資料表示。它們由實數陣列組成,其中大多數或所有元素都是非零的。相較於稀疏向量,密集向量在相同的維度層級包含更多的資訊,因為每個維度都持有有意義的數值。這種表示方式可以有效捕捉複雜的模式和關係,讓資料更容易在高維空間中分析和處理。密集向量通常有固定的維數,從幾十到幾百甚至上千不等,這取決於特定的應用程式和需求。
密集向量主要用於需要瞭解資料語意的情境,例如語意搜尋和推薦系統。在語意搜尋中,密集向量有助於捕捉查詢與文件之間的潛在關聯,從而改善搜尋結果的相關性。在推薦系統中,密集向量有助於識別使用者與項目之間的相似性,提供更個人化的建議。
概述
密集向量通常以固定長度的浮點數陣列表示,例如[0.2, 0.7, 0.1, 0.8, 0.3, ..., 0.5] 。這些向量的維度通常從數百到數千不等,例如 128、256、768 或 1024。每個維度都能捕捉物件的特定語意特徵,使其透過相似度計算適用於各種情境。
密集向量
上圖說明密集向量在 2D 空間中的表示。雖然實際應用中的密集向量通常有更高的維度,但這個 2D 圖解有效地傳達了幾個關鍵概念:
多維表示:每個點代表一個概念物件 (如Milvus、向量資料庫、檢索系統等),其位置由其維度值來決定。
語意關係:點與點之間的距離反映了概念之間的語義相似性。較近的點則表示語義關係較密切的概念。
聚類效應:相關的概念(例如Milvus、向量資料庫和檢索系統)在空間中的位置彼此接近,形成一個語意叢集。
以下是表示文字"Milvus is an efficient vector database" 的真實密集向量範例:
[
-0.013052909,
0.020387933,
-0.007869,
-0.11111383,
-0.030188112,
-0.0053388323,
0.0010654867,
0.072027855,
// ... more dimensions
]
稠密向量可以使用各種嵌入模型產生,例如針對影像的 CNN 模型 (如ResNet、VGG),以及針對文字的語言模型 (如BERT、Word2Vec)。這些模型可將原始資料轉換為高維空間中的點數,捕捉資料的語意特徵。此外,Milvus 還提供方便的方法,協助使用者產生和處理密集向量,詳情請參閱 Embeddings。
一旦資料被向量化,就可以儲存在 Milvus 中進行管理和向量檢索。下圖顯示基本流程。
使用密集向量
除了密集向量,Milvus 也支援稀疏向量和二進位向量。稀疏向量適用於基於特定詞彙的精確匹配,例如關鍵字搜尋和詞彙匹配;而二進制向量則常用於有效處理二進制資料,例如圖像模式匹配和某些雜湊應用。如需詳細資訊,請參閱二進位向量和稀疏向量。
使用密集向量
新增向量領域
要在 Milvus 中使用密集向量,首先要在建立集合時定義一個向量欄位來儲存密集向量。這個過程包括
將
datatype設定為支援的密集向量資料類型。關於支援的密集向量資料類型,請參閱資料類型。使用
dim參數指定密集向量的尺寸。
在下面的範例中,我們新增一個名為dense_vector 的向量欄位來儲存密集向量。欄位的資料類型是FLOAT_VECTOR ,維度是4 。
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="dense_vector", datatype=DataType.FLOAT_VECTOR, dim=4)
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("dense_vector")
.dataType(DataType.FloatVector)
.dimension(4)
.build());
import { DataType } from "@zilliz/milvus2-sdk-node";
schema.push({
name: "dense_vector",
data_type: DataType.FloatVector,
dim: 4,
});
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/column"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "localhost:19530"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeVarChar).
WithIsPrimaryKey(true).
WithIsAutoID(true).
WithMaxLength(100),
).WithField(entity.NewField().
WithName("dense_vector").
WithDataType(entity.FieldTypeFloatVector).
WithDim(4),
)
export primaryField='{
"fieldName": "pk",
"dataType": "VarChar",
"isPrimary": true,
"elementTypeParams": {
"max_length": 100
}
}'
export vectorField='{
"fieldName": "dense_vector",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": 4
}
}'
export schema="{
\"autoID\": true,
\"fields\": [
$primaryField,
$vectorField
]
}"
密集向量欄位支援的資料類型:
資料類型 |
說明 |
|---|---|
|
儲存 32 位元浮點數,常用於表示科學計算和機器學習中的實數。適用於需要高精度的場景,例如區分相似向量。 |
|
儲存 16 位元半精確浮點數,用於深度學習和 GPU 運算。在精確度不太重要的情況下,例如推薦系統的低精確度召回階段,可節省儲存空間。 |
|
儲存 16 位元腦浮點 (bfloat16) 數字,提供與 Float32 相同的指數範圍,但精確度較低。適用於需要快速處理大量向量的情況,例如大規模的影像檢索。 |
|
儲存向量,其每個維度的個別元素為 8 位元整數 (int8),每個元素的範圍為 -128 到 127。INT8_VECTOR 專為量化深度學習模型 (例如 ResNet、EfficientNet) 而設計,可減少模型大小,並加快推論速度,同時將精確度損失降至最低。 |
為向量欄位設定索引參數
為了加速語意搜尋,必須為向量欄位建立索引。索引可以顯著改善大規模向量資料的檢索效率。
index_params = client.prepare_index_params()
index_params.add_index(
field_name="dense_vector",
index_name="dense_vector_index",
index_type="AUTOINDEX",
metric_type="IP"
)
import io.milvus.v2.common.IndexParam;
import java.util.*;
List<IndexParam> indexes = new ArrayList<>();
indexes.add(IndexParam.builder()
.fieldName("dense_vector")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.IP)
.build());
import { MetricType, IndexType } from "@zilliz/milvus2-sdk-node";
const indexParams = {
index_name: 'dense_vector_index',
field_name: 'dense_vector',
metric_type: MetricType.IP,
index_type: IndexType.AUTOINDEX
};
idx := index.NewAutoIndex(index.MetricType(entity.IP))
indexOption := milvusclient.NewCreateIndexOption("my_collection", "dense_vector", idx)
export indexParams='[
{
"fieldName": "dense_vector",
"metricType": "IP",
"indexName": "dense_vector_index",
"indexType": "AUTOINDEX"
}
]'
在上面的範例中,使用AUTOINDEX 索引類型為dense_vector 欄位建立了一個名為dense_vector_index 的索引。metric_type 設定為IP ,表示將使用內積作為距離指標。
Milvus 提供多種索引類型,以提供更好的向量搜尋體驗。AUTOINDEX 是一種特殊的索引類型,專為平滑向量搜尋的學習曲線而設計。有很多索引類型可供您選擇。詳情請參考 xxx。
Milvus 支援其他公制類型。如需詳細資訊,請參考公制類型。
建立集合
一旦密集向量和索引參數設定完成,你就可以建立一個包含密集向量的集合。以下範例使用create_collection 方法建立一個名為my_collection 的集合。
client.create_collection(
collection_name="my_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_collection")
.collectionSchema(schema)
.indexParams(indexes)
.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_collection',
schema: schema,
index_params: indexParams
});
err = client.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption("my_collection", schema).
WithIndexOptions(indexOption))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_collection\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
插入資料
建立集合後,使用insert 方法加入包含密集向量的資料。確保插入的密集向量的維度與新增密集向量欄位時定義的dim 值相符。
data = [
{"dense_vector": [0.1, 0.2, 0.3, 0.7]},
{"dense_vector": [0.2, 0.3, 0.4, 0.8]},
]
client.insert(
collection_name="my_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;
List<JsonObject> rows = new ArrayList<>();
Gson gson = new Gson();
rows.add(gson.fromJson("{\"dense_vector\": [0.1, 0.2, 0.3, 0.4]}", JsonObject.class));
rows.add(gson.fromJson("{\"dense_vector\": [0.2, 0.3, 0.4, 0.5]}", JsonObject.class));
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName("my_collection")
.data(rows)
.build());
const data = [
{ dense_vector: [0.1, 0.2, 0.3, 0.7] },
{ dense_vector: [0.2, 0.3, 0.4, 0.8] },
];
client.insert({
collection_name: "my_collection",
data: data,
});
_, err = client.Insert(ctx, milvusclient.NewColumnBasedInsertOption("my_collection").
WithFloatVectorColumn("dense_vector", 4, [][]float32{
{0.1, 0.2, 0.3, 0.7},
{0.2, 0.3, 0.4, 0.8},
}),
)
if err != nil {
fmt.Println(err.Error())
// handle err
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/insert" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"data": [
{"dense_vector": [0.1, 0.2, 0.3, 0.4]},
{"dense_vector": [0.2, 0.3, 0.4, 0.5]}
],
"collectionName": "my_collection"
}'
## {"code":0,"cost":0,"data":{"insertCount":2,"insertIds":["453577185629572531","453577185629572532"]}}
執行相似性搜尋
基於密集向量的語意搜尋是 Milvus 的核心功能之一,可讓您根據向量之間的距離,快速找到與查詢向量最相似的資料。若要執行相似性搜尋,請準備查詢向量和搜尋參數,然後調用search 方法。
search_params = {
"params": {"nprobe": 10}
}
query_vector = [0.1, 0.2, 0.3, 0.7]
res = client.search(
collection_name="my_collection",
data=[query_vector],
anns_field="dense_vector",
search_params=search_params,
limit=5,
output_fields=["pk"]
)
print(res)
# Output
# data: ["[{'id': '453718927992172271', 'distance': 0.7599999904632568, 'entity': {'pk': '453718927992172271'}}, {'id': '453718927992172270', 'distance': 0.6299999952316284, 'entity': {'pk': '453718927992172270'}}]"]
import io.milvus.v2.service.vector.request.data.FloatVec;
Map<String,Object> searchParams = new HashMap<>();
searchParams.put("nprobe",10);
FloatVec queryVector = new FloatVec(new float[]{0.1f, 0.3f, 0.3f, 0.4f});
SearchResp searchR = client.search(SearchReq.builder()
.collectionName("my_collection")
.data(Collections.singletonList(queryVector))
.annsField("dense_vector")
.searchParams(searchParams)
.topK(5)
.outputFields(Collections.singletonList("pk"))
.build());
System.out.println(searchR.getSearchResults());
// Output
//
// [[SearchResp.SearchResult(entity={pk=453444327741536779}, score=0.65, id=453444327741536779), SearchResp.SearchResult(entity={pk=453444327741536778}, score=0.65, id=453444327741536778)]]
query_vector = [0.1, 0.2, 0.3, 0.7];
client.search({
collection_name: 'my_collection',
data: query_vector,
limit: 5,
output_fields: ['pk'],
params: {
nprobe: 10
}
});
queryVector := []float32{0.1, 0.2, 0.3, 0.7}
annParam := index.NewCustomAnnParam()
annParam.WithExtraParam("nprobe", 10)
resultSets, err := client.Search(ctx, milvusclient.NewSearchOption(
"my_collection", // collectionName
5, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithANNSField("dense_vector").
WithOutputFields("pk").
WithAnnParam(annParam))
if err != nil {
fmt.Println(err.Error())
// handle error
}
for _, resultSet := range resultSets {
fmt.Println("IDs: ", resultSet.IDs.FieldData().GetScalars())
fmt.Println("Scores: ", resultSet.Scores)
fmt.Println("Pks: ", resultSet.GetColumn("pk").FieldData().GetScalars())
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "my_collection",
"data": [
[0.1, 0.2, 0.3, 0.7]
],
"annsField": "dense_vector",
"limit": 5,
"searchParams":{
"params":{"nprobe":10}
},
"outputFields": ["pk"]
}'
## {"code":0,"cost":0,"data":[{"distance":0.55,"id":"453577185629572532","pk":"453577185629572532"},{"distance":0.42,"id":"453577185629572531","pk":"453577185629572531"}]}
有關相似性搜尋參數的詳細資訊,請參閱基本 ANN 搜尋。