评估模型

在训练模型后,AutoML Natural Language 使用 TEST 集中的文档来评估新模型的质量和准确率。

AutoML Natural Language 提供了一组总体评估指标以及每个类别标签的评估指标。前者指示模型的整体表现情况,后者指示模型对该标签的执行情况。

精确率召回率用于衡量模型捕获信息的情况以及丢失的信息量。精确率表示在标识为特定实体或标签的所有文档中,实际有多少文档应分配给该实体或标签。召回率表示在应被识别为特定实体或标签的所有文档中,实际有多少文档分配给了该实体或标签。

混淆矩阵(仅适用于每个文档只有一个标签的模型)表示评估期间每个标签被预测为训练集内相应标签的次数百分比。理想情况下,标签 one 只会分配给归类为 one 标签的文档,以此类推;因此,完美的矩阵应如下所示:

100  0   0   0
 0  100  0   0
 0   0  100  0
 0   0   0  100

在上面的示例中,如果文档归类为 one,但模型预测为 two,则第一行将变为如下所示:

99  1  0  0

AutoML Natural Language 最多为 10 个标签创建混淆矩阵。 如果标签数量超过 10 个,则该矩阵会包含最容易混淆(预测错误)的 10 个标签。

对于情感模型:

  • 平均绝对误差 (MAE)均方误差 (MSE) 衡量预测情感值与实际情感值之间的偏差。数值越低,表示模型越准确。

  • 线性加权 kappa二次加权 kappa 衡量模型分配的情感值与人工标注者分配的情感值的接近程度。数值越高,表示模型越准确。

您可利用这些指标来评估您的模型的准备情况。如果精确率和召回率得分低,这可能表明您的模型需要额外的训练数据或者模型注释不一致。完美的精确率和召回率可能表明数据过于简单,模型可能无法有效泛化。如需查看有关评估模型的更多提示,请参阅新手指南

如果您对质量水平不满意,则可以重新执行前述步骤以提高质量:

  • 考虑为任何低质量的标签添加更多文档。
  • 您可能需要添加不同类型的文档。例如,篇幅更长或更短的文档,不同作者使用不同措辞或风格撰写的文档。
  • 您可以清理标签。
  • 如果训练图片不够,请考虑完全移除相应标签。

一旦进行了更改,请训练并评估新模型,直到达到足够高的质量水平为止。

网页界面

如需查看模型的评估指标,请执行以下操作:

  1. 点击左侧导航栏中的灯泡图标以显示可用的模型。

    如需查看其他项目的模型,请从标题栏右上角的下拉列表中选择该项目。

  2. 点击待评估模型所在的行。

  3. 如果需要,请点击标题栏正下方的评估标签。

    如果该模型已完成训练,则 AutoML Natural Language 会显示其评估指标。

    评估页面

  4. 如要查看特定标签的指标,请从页面下部的标签列表中选择标签名称。

代码示例

这些示例为整个模型提供了评估。您还可以使用评估 ID 获取特定标签 (displayName) 的指标。

REST

在使用任何请求数据之前,请先进行以下替换:

  • project-id:您的项目 ID
  • location-id:资源的位置,全球位置为 us-central1,欧盟位置为 eu
  • model-id:您的模型 ID

HTTP 方法和网址:

GET https://automl.googleapis.com/v1/projects/project-id/locations/location-id/models/model-id/modelEvaluations

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "modelEvaluation": [
    {
      "name": "projects/434039606874/locations/us-central1/models/7537307368641647584/modelEvaluations/9009741181387603448",
      "annotationSpecId": "17040929661974749",
      "classificationMetrics": {
        "auPrc": 0.99772006,
        "baseAuPrc": 0.21706384,
        "evaluatedExamplesCount": 377,
        "confidenceMetricsEntry": [
          {
            "recall": 1,
            "precision": -1.3877788e-17,
            "f1Score": -2.7755576e-17,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
          {
            "confidenceThreshold": 0.05,
            "recall": 0.997,
            "precision": 0.867,
            "f1Score": 0.92746675,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
          {
            "confidenceThreshold": 0.1,
            "recall": 0.995,
            "precision": 0.905,
            "f1Score": 0.9478684,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
          {
            "confidenceThreshold": 0.15,
            "recall": 0.992,
            "precision": 0.932,
            "f1Score": 0.96106446,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
          {
            "confidenceThreshold": 0.2,
            "recall": 0.989,
            "precision": 0.951,
            "f1Score": 0.96962786,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
          {
            "confidenceThreshold": 0.25,
            "recall": 0.987,
            "precision": 0.957,
            "f1Score": 0.9717685,
            "recallAt1": 0.9761273,
            "precisionAt1": 0.9761273,
            "f1ScoreAt1": 0.9761273
          },
        ...
        ],
      },
      "createTime": "2018-04-30T23:06:14.746840Z"
    },
    {
      "name": "projects/434039606874/locations/us-central1/models/7537307368641647584/modelEvaluations/9009741181387603671",
      "annotationSpecId": "1258823357545045636",
      "classificationMetrics": {
        "auPrc": 0.9972302,
        "baseAuPrc": 0.1883289,
      ...
      },
      "createTime": "2018-04-30T23:06:14.649260Z"
    }
  ]
}

Python

如需了解如何安装和使用 AutoML Natural Language 的客户端库,请参阅 AutoML Natural Language 客户端库。 如需了解详情,请参阅 AutoML Natural Language Python API 参考文档

如需向 AutoML Natural Language 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# model_id = "YOUR_MODEL_ID"

client = automl.AutoMlClient()
# Get the full path of the model.
model_full_id = client.model_path(project_id, "us-central1", model_id)

print("List of model evaluations:")
for evaluation in client.list_model_evaluations(parent=model_full_id, filter=""):
    print(f"Model evaluation name: {evaluation.name}")
    print(f"Model annotation spec id: {evaluation.annotation_spec_id}")
    print(f"Create Time: {evaluation.create_time}")
    print(f"Evaluation example count: {evaluation.evaluated_example_count}")
    print(
        "Classification model evaluation metrics: {}".format(
            evaluation.classification_evaluation_metrics
        )
    )

Java

如需了解如何安装和使用 AutoML Natural Language 的客户端库,请参阅 AutoML Natural Language 客户端库。 如需了解详情,请参阅 AutoML Natural Language Java API 参考文档

如需向 AutoML Natural Language 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.ListModelEvaluationsRequest;
import com.google.cloud.automl.v1.ModelEvaluation;
import com.google.cloud.automl.v1.ModelName;
import java.io.IOException;

class ListModelEvaluations {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    listModelEvaluations(projectId, modelId);
  }

  // List model evaluations
  static void listModelEvaluations(String projectId, String modelId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
      // Get the full path of the model.
      ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
      ListModelEvaluationsRequest modelEvaluationsrequest =
          ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build();

      // List all the model evaluations in the model by applying filter.
      System.out.println("List of model evaluations:");
      for (ModelEvaluation modelEvaluation :
          client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) {

        System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName());
        System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId());
        System.out.println("Create Time:");
        System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds());
        System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9);
        System.out.format(
            "Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
        System.out.format(
            "Classification Model Evaluation Metrics: %s\n",
            modelEvaluation.getClassificationEvaluationMetrics());
      }
    }
  }
}

Node.js

如需了解如何安装和使用 AutoML Natural Language 的客户端库,请参阅 AutoML Natural Language 客户端库。 如需了解详情,请参阅 AutoML Natural Language Node.js API 参考文档

如需向 AutoML Natural Language 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function listModelEvaluations() {
  // Construct request
  const request = {
    parent: client.modelPath(projectId, location, modelId),
    filter: '',
  };

  const [response] = await client.listModelEvaluations(request);

  console.log('List of model evaluations:');
  for (const evaluation of response) {
    console.log(`Model evaluation name: ${evaluation.name}`);
    console.log(`Model annotation spec id: ${evaluation.annotationSpecId}`);
    console.log(`Model display name: ${evaluation.displayName}`);
    console.log('Model create time');
    console.log(`\tseconds ${evaluation.createTime.seconds}`);
    console.log(`\tnanos ${evaluation.createTime.nanos / 1e9}`);
    console.log(
      `Evaluation example count: ${evaluation.evaluatedExampleCount}`
    );
    console.log(
      `Classification model evaluation metrics: ${evaluation.classificationEvaluationMetrics}`
    );
  }
}

listModelEvaluations();

Go

如需了解如何安装和使用 AutoML Natural Language 的客户端库,请参阅 AutoML Natural Language 客户端库。 如需了解详情,请参阅 AutoML Natural Language Go API 参考文档

如需向 AutoML Natural Language 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	"cloud.google.com/go/automl/apiv1/automlpb"
	"google.golang.org/api/iterator"
)

// listModelEvaluation lists existing model evaluations.
func listModelEvaluations(w io.Writer, projectID string, location string, modelID string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "TRL123456789..."

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &automlpb.ListModelEvaluationsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
	}

	it := client.ListModelEvaluations(ctx, req)

	// Iterate over all results
	for {
		evaluation, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListModelEvaluations.Next: %w", err)
		}

		fmt.Fprintf(w, "Model evaluation name: %v\n", evaluation.GetName())
		fmt.Fprintf(w, "Model annotation spec id: %v\n", evaluation.GetAnnotationSpecId())
		fmt.Fprintf(w, "Create Time:\n")
		fmt.Fprintf(w, "\tseconds: %v\n", evaluation.GetCreateTime().GetSeconds())
		fmt.Fprintf(w, "\tnanos: %v\n", evaluation.GetCreateTime().GetNanos())
		fmt.Fprintf(w, "Evaluation example count: %v\n", evaluation.GetEvaluatedExampleCount())
		fmt.Fprintf(w, "Classification model evaluation metrics: %v\n", evaluation.GetClassificationEvaluationMetrics())
	}

	return nil
}

其他语言

C#: 请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 AutoML Natural Language 参考文档。

PHP: 请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 AutoML Natural Language 参考文档。

Ruby: 请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 AutoML Natural Language 参考文档。