本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
NLP 模型的可解释性报告
亚马逊 SageMaker Explain 支持对自然语言处理 (NLP) 模型的说明。NLP 模型可帮助您了解哪些文本部分对于模型所做的预测最为重要。此功能可用于解释单个局部预测或模型对整体全局解释的预期预测。您可以定义文本段(标记、短语、句子、段落)的长度,以了解和可视化模型在多个粒度级别的行为。
SageMaker 澄清 NLP 可解释性与分类和回归模型兼容。还可以使用澄清来解释模型在包含文本、分类或数字要素的多模态数据集上的行为。多模态数据集的 NLP 可解释性可以帮助您了解每个要素对模型输出的重要性。 SageMaker 澄清支持 62 种语言,可以处理包含多种语言的文本。
要获取输入文本部分的功能重要性,请创建TextConfig指定granularity文本的各部分和language. 然后澄清,然后根据你的选择将文本分解为令牌、句子或段落granularity. 它用来自baseline. 这些区域有:baseline默认为空字符串,意思是 SageMaker 澄清删除输入文本的一部分。 SageMaker 然后澄清替换标记(分别是句子和段落),以确定这些替代品如何影响模型的预测,以评估它们的重要性。
from sagemaker import clarify text_config = clarify.TextConfig( # Specify the language of your text or use "xx" for multi-language. language="en", # Choose the granularity of your explanations as tokens, sentences or paragraphs. granularity="token" ) shap_config = clarify.SHAPConfig( # For now, we don't support global aggregation, # so make sure you retrieve local explanations. save_local_shap_values=True, text_config=text_config, # Determine with what to replace tokens/sentences/paragraphs (based on your choice of granularity) with. baseline=[["<UNK>"]], # You can further specify num_samples and agg_method. )
可解释性工作的其余部分像往常一样编码。
explainability_output_path = 's3://{}/{}/clarify-explainability'.format(bucket, prefix) explainability_data_config = clarify.DataConfig( s3_data_input_path=train_uri, s3_output_path=explainability_output_path, # With text data sometimes detection of headers fails, # so we recommend explicitly setting it in the config. headers=["text"], dataset_type="text/csv") model_config = clarify.ModelConfig( model_name=model_name, # Specialized hardware is well-suited for NLP models. instance_type="ml.p2.xlarge", instance_count=1 ) clarify_processor = clarify.SageMakerClarifyProcessor( role=role, instance_count=1, instance_type="ml.m5.xlarge", sagemaker_session=session ) clarify_processor.run_explainability( data_config=explainability_data_config, model_config=model_config, explainability_config=shap_config)
查看本地说明explainability_output_pathS3 存储桶。 SageMaker 澄清并没有总结个别令牌/句子/段落的重要性。
:扩展名
-
多模态数据-如果您的数据包含的不止是文本,例如分类或数字要素,则可以在标题和基线中添加更多列以容纳这些要素。
-
JSON-如果模型输入或输出数据采用 JSON 线格式,请指定
dataset_type中的DataConfig或者accept_type和content_type中的ModelConfig. -
多类别模型输出-如果你的模型进行了多级或多标签预测, SageMaker Clplain 计算每个课程的重要性。
-
模型输出-某些模型输出包含超过一个分数。对三个示例的预测返回一个形状矩阵
[3, 1]例如[[0.1], [-1.1], [10.2], [0.0], [3.2]]. 您可以指定model_scores在run_explainability. 例如,如果你的模型预测了五个类别的概率并另外输出获胜类别,["c1", [0.1, 0.4, 0.3, 0.2]],然后你可以设置model_scores=1.
快速结果的提示:
-
在开发阶段,使用一小部分示例进行快速迭代。
-
要向上扩展以进行部署,请增加
instance_count用于处理作业和预测变量。这指示 SageMaker 澄清如何对数据进行分区 parallel 处理多台计算机。对于 parallel 处理,我们建议使用镶木地板数据格式,因为 Spark 可能会遇到 CSV 文件中的特殊字符。 -
选择合适的
instance_type为您的模型。GPU 实例通常适用于 NLP 模型。 -
要保存不必要的计算,请考虑截断文本。许多模型都使用最大文本长度。通过在预处理步骤中应用截断, SageMaker 澄清不会浪费资源来确定模型忽视的文本部分的功能重要性。