部署和管理 LTR 模型
Elastic Stack Serverless
此功能在 8.12.0 版本中引入,并且仅适用于某些订阅级别。有关更多信息,请参见 https://elastic.ac.cn/subscriptions。
通常,XGBoost 模型训练过程使用标准的 Python 数据科学工具,例如 Pandas 和 scikit-learn。
我们在 elasticsearch-labs
仓库中提供了一个示例笔记本。 这个交互式 Python 笔记本详细说明了一个端到端的模型训练和部署工作流程。
我们强烈建议您在工作流程中使用 eland,因为它为在 Elasticsearch 中使用 LTR 提供了重要的功能。使用 eland 可以
- 配置特征提取
- 提取用于训练的特征
- 在 Elasticsearch 中部署模型
特征提取器使用模板化查询定义。Eland 提供了 eland.ml.ltr.QueryFeatureExtractor
,可以直接在 Python 中定义这些特征提取器
from eland.ml.ltr import QueryFeatureExtractor
feature_extractors=[
# We want to use the BM25 score of the match query for the title field as a feature:
QueryFeatureExtractor(
feature_name="title_bm25",
query={"match": {"title": "{{query}}"}}
),
# We want to use the number of matched terms in the title field as a feature:
QueryFeatureExtractor(
feature_name="title_matched_term_count",
query={
"script_score": {
"query": {"match": {"title": "{{query}}"}},
"script": {"source": "return _termStats.matchedTermsCount();"},
}
},
),
# We can use a script_score query to get the value
# of the field rating directly as a feature:
QueryFeatureExtractor(
feature_name="popularity",
query={
"script_score": {
"query": {"exists": {"field": "popularity"}},
"script": {"source": "return doc['popularity'].value;"},
}
},
),
# We extract the number of terms in the query as feature.
QueryFeatureExtractor(
feature_name="query_term_count",
query={
"script_score": {
"query": {"match": {"title": "{{query}}"}},
"script": {"source": "return _termStats.uniqueTermsCount();"},
}
},
),
]
对于 LTR 模型而言,利用原始术语统计信息作为特征非常常见。 要提取此信息,您可以使用 术语统计信息功能,该功能作为 script_score
查询的一部分提供。
定义特征提取器后,将它们包装在 eland.ml.ltr.LTRModelConfig
对象中,以供以后在训练步骤中使用
from eland.ml.ltr import LTRModelConfig
ltr_config = LTRModelConfig(feature_extractors)
构建数据集是训练过程中的关键步骤。 这涉及提取相关特征并将它们添加到您的判断列表。 我们建议为此过程使用 Eland 的 eland.ml.ltr.FeatureLogger
辅助类。
from eland.ml.ltr import FeatureLogger
# Create a feature logger that will be used to query Elasticsearch to retrieve the features:
feature_logger = FeatureLogger(es_client, MOVIE_INDEX, ltr_config)
FeatureLogger 提供了一个 extract_features
方法,使您可以从判断列表中提取特定文档列表的特征。 同时,您可以将查询参数传递给先前定义的特征提取器
feature_logger.extract_features(
query_params={"query": "foo"},
doc_ids=["doc-1", "doc-2"]
)
我们的 示例笔记本 解释了如何使用 FeatureLogger
通过将特征添加到判断列表来构建训练数据集。
- 我们强烈建议不要自己实现特征提取。 至关重要的是,要保持训练环境和 Elasticsearch 中的推理之间特征提取的一致性。 通过使用与 Elasticsearch 协同开发和测试的 eland 工具,您可以确保它们始终如一地协同工作。
- 特征提取是通过在 Elasticsearch 服务器上执行查询来完成的。 这可能会给您的集群带来很大的压力,尤其是当您的判断列表包含大量示例或您有很多特征时。 我们的特征记录器实现旨在最大程度地减少发送到服务器的搜索请求数量并减少负载。 但是,最好使用与任何面向用户的生产流量隔离的 Elasticsearch 集群来构建训练数据集。
训练完模型后,您将能够在 Elasticsearch 集群中部署它。 您可以使用 Eland 的 MLModel.import_ltr_model method
from eland.ml import MLModel
LEARNING_TO_RANK_MODEL_ID="ltr-model-xgboost"
MLModel.import_ltr_model(
es_client=es_client,
model=ranker,
model_id=LEARNING_TO_RANK_MODEL_ID,
ltr_model_config=ltr_config,
es_if_exists="replace",
)
此方法将序列化训练后的模型和 Learning To Rank 配置(包括特征提取),格式为 Elasticsearch 可以理解的格式。 然后使用 创建训练模型 API 将模型部署到 Elasticsearch。
目前,Elasticsearch 的 LTR 支持以下类型的模型
将来将支持更多模型类型。
在 Elasticsearch 中部署模型后,您可以使用 训练模型 API 对其进行管理。 现在,您可以准备在 搜索时 将 LTR 模型用作重新评分器。