Rasa官方教程翻译6 模型评估

评估NLU模型

比较NLU管道

意图分类

响应选择

实体提取

实体评分

评估Core模型

比较Core配置

端对端评估

 

注意

如果你想优化NLU模型的超参数,参阅教程 tutorial.

 

评估NLU模型

机器学习的一个标准技术是把一些数据分离出来作为测试集。你可以使用下面命令把NLU训练数据集拆分成训练数据集和测试数据集:

rasa data split nlu

如果您已经这样做了,您可以使用以下命令查看NLU模型对测试用例的预测情况:

rasa test nlu -u test_set.md --model models/nlu-20180323-145833.tar.gz

如果不想创建单独的测试集,仍然可以使用交叉验证来估计模型的泛化程度。为此,需要添加标志--cross-validation:

rasa test nlu -u data/nlu.md --config config.yml --cross-validation

下面是脚本的完整选项列表是:

usage: rasa test nlu [-h] [-v] [-vv] [--quiet] [-m MODEL] [-u NLU] [--out OUT]

                     [--successes] [--no-errors] [--histogram HISTOGRAM]

                     [--confmat CONFMAT] [-c CONFIG [CONFIG ...]]

                     [--cross-validation] [-f FOLDS] [-r RUNS]

                     [-p PERCENTAGES [PERCENTAGES ...]]

 

optional arguments:

  -h, --help            show this help message and exit

  -m MODEL, --model MODEL

                        Path to a trained Rasa model. If a directory is

                        specified, it will use the latest model in this

                        directory. (default: models)

  -u NLU, --nlu NLU     File or folder containing your NLU data. (default:

                        data)

  --out OUT             Output path for any files created during the

                        evaluation. (default: results)

  --successes           If set successful predictions (intent and entities)

                        will be written to a file. (default: False)

  --no-errors           If set incorrect predictions (intent and entities)

                        will NOT be written to a file. (default: False)

  --histogram HISTOGRAM

                        Output path for the confidence histogram. (default:

                        hist.png)

  --confmat CONFMAT     Output path for the confusion matrix plot. (default:

                        confmat.png)

  -c CONFIG [CONFIG ...], --config CONFIG [CONFIG ...]

                        Model configuration file. If a single file is passed

                        and cross validation mode is chosen, cross-validation

                        is performed, if multiple configs or a folder of

                        configs are passed, models will be trained and

                        compared directly. (default: None)

 

Python Logging Options:

  -v, --verbose         Be verbose. Sets logging level to INFO. (default:

                        None)

  -vv, --debug          Print lots of debugging statements. Sets logging level

                        to DEBUG. (default: None)

  --quiet               Be quiet! Sets logging level to WARNING. (default:

                        None)

 

Cross Validation:

  --cross-validation    Switch on cross validation mode. Any provided model

                        will be ignored. (default: False)

  -f FOLDS, --folds FOLDS

                        Number of cross validation folds (cross validation

                        only). (default: 10)

 

Comparison Mode:

  -r RUNS, --runs RUNS  Number of comparison runs to make. (default: 3)

  -p PERCENTAGES [PERCENTAGES ...], --percentages PERCENTAGES [PERCENTAGES ...]

                        Percentages of training data to exclude during

                        comparison. (default: [0, 25, 50, 75])

NLU管道比较

通过将多个管道配置(或包含这些配置的文件夹)传递给CLI,Rasa将运行管道之间的比较检查。

$ rasa test nlu --config pretrained_embeddings_spacy.yml supervised_embeddings.yml

  --nlu data/nlu.md --runs 3 --percentages 0 25 50 70 90

上述命令会把你的数据拆分成训练集和测试集,并在训练集中去掉0%、25%、50%、70%和90%的目标数据的情况下多次训练每个管道。然后在测试集上对模型进行评估,并记录每个去掉百分比的f1分数。这个过程运行三次(即总共有三个测试集),然后使用f1分数的平均值和标准差绘制一个图表。

所有训练/测试集的f1分数图形,训练和分类报告,错误报告会保存在一个名为nlu_compartison_results的文件夹中。

意图分类

评估脚本将为你的模型生成一个报告,混淆矩阵和置信直方图。

该报告中记录每个意图和实体记录了精确度、召回率和f1指标,并提供总体平均值。你可以使用参数--report把报告保存为JSON格式文件。

混淆矩阵显示了哪些意图被误认为是其他意图;任何被错误预测的样本都会被记录并保存到名为errors.json的文件中,以便于调试。

脚本生成的直方图允许您可视化所有预测的置信度分布,正确和错误预测的数量分别用蓝色和红色条显示。提高训练数据的质量将使蓝色直方图条向右移动,红色直方图条向左移动。

 

注意

只有在测试集上评估模型时,才会生成混淆矩阵。 在交叉验证模式下,不会生成混淆矩阵。

 

警告

任何一个实体被错误地标注可能导致评估的失败。 常见的问题是一个实体没有被从开头或末尾被正确提取出来。例如,如果你有一个像[Brian](name)'s house这样的name实体示例, 只有你的分词器把 Brian's 分成Brians时才是有效的. 这里不考虑空格分割标记。

响应选择

评估脚本会给管道中所有的响应选择器模型生成一个组合报告。报告记录了每一个响应的精确度,召回率和f1指标,并提供总体平均值。你可以使用参数--report把报告保存为JSON格式文件。

实体提取

CRFEntityExtractor是唯一的可以用你自己的数据进行训练的实体提取器,因此也是唯一一个可以被评估的实体提取器。如果你使用spaCy或duckling pre-trained实体提取器,Rasa NLU不会去评估它们。

Rasa NLU会报告被CRFEntityExtractor训练过的每一类实体的召回率,精确度和f1指标。

实体评分

为了评估实体提取,我们采用了一种简单的基于标签的方法。我们不考虑 BILOU(Begin, Inside, Last, Outside, Unit)标记方法,只考虑每个标记的实体类型标记。对于像 “near Alexanderplatz”这样的位置实体,我们希望标记是LOC LOC而不是基于BILOU标记的B-LOC L-LOC。在评估方面,我们的方法更为宽松,因为它奖励部分提取,而不惩罚实体分割。例如,考虑到上述实体 “near Alexanderplatz”和提取 “Alexanderplatz”的系统我们的方法奖励提取“Alexanderplatz”,并处罚漏掉“near”单词。然而,基于BILOU的方法对此标将是完全失败的,因为“Alexanderplatz”希望被标记为实体的最后一个词 (L-LOC),而不是标记为独立实体(U-LOC)。还要注意的是对于 “near”和 “Alexanderplatz”的分割提取在我们的方法上得满分,而在基于BILOU方法上得0分。

以下是“near Alexanderplatz tonight”这句话的两种评分机制的比较:

extracted

Simple tags(score)

BILOU tags(score)

[near Alexanderplatz](loc) [tonight](time)

loc loc time (3)

B-loc L-loc U-time (3)

[near](loc) [Alexanderplatz](loc) [tonight](time)

loc loc time(3)

U-loc U-loc U-time (1)

near [Alexanderplatz](loc) [tonight](time)

O loc time (2)

O U-loc U-time (1)

[near](loc) Alexanderplatz [tonight](time)

loc O time (2)

U-loc O U-time (1)

[near Alexanderplatz tonight](loc)

loc loc loc (2)

B-loc I-loc L-loc (1)

评估Core模型

通过使用评价脚本,你可以在一组故事测试集上评估你的训练模型:

rasa test core --stories test_stories.md --out results

 

该命令将会把失败的故事打印到results/failed_stories.md文件。如果至少有一个动作被错误预测,我们就认为任何故事都失败了。

另外,该命令将保存一个混淆矩阵到results/story_confmat.pdf文件中。对于你领域内的每个动作,混淆矩阵将展示一个action被预测正确的频率和一个错误的action被预测正确的频率。

下面是脚本的完整选项列表:

usage: rasa test core [-h] [-v] [-vv] [--quiet] [-m MODEL [MODEL ...]]

                      [-s STORIES] [--max-stories MAX_STORIES] [--out OUT]

                      [--e2e] [--endpoints ENDPOINTS]

                      [--fail-on-prediction-errors] [--url URL]

                      [--evaluate-model-directory]

 

optional arguments:

  -h, --help            show this help message and exit

  -m MODEL [MODEL ...], --model MODEL [MODEL ...]

                        Path to a pre-trained model. If it is a 'tar.gz' file

                        that model file will be used. If it is a directory,

                        the latest model in that directory will be used

                        (exception: '--evaluate-model-directory' flag is set).

                        If multiple 'tar.gz' files are provided, all those

                        models will be compared. (default: [None])

  -s STORIES, --stories STORIES

                        File or folder containing your test stories. (default:

                        data)

  --max-stories MAX_STORIES

                        Maximum number of stories to test on. (default: None)

  --out OUT             Output path for any files created during the

                        evaluation. (default: results)

  --e2e, --end-to-end   Run an end-to-end evaluation for combined action and

                        intent prediction. Requires a story file in end-to-end

                        format. (default: False)

  --endpoints ENDPOINTS

                        Configuration file for the connectors as a yml file.

                        (default: None)

  --fail-on-prediction-errors

                        If a prediction error is encountered, an exception is

                        thrown. This can be used to validate stories during

                        tests, e.g. on travis. (default: False)

  --url URL             If supplied, downloads a story file from a URL and

                        trains on it. Fetches the data by sending a GET

                        request to the supplied URL. (default: None)

  --evaluate-model-directory

                        Should be set to evaluate models trained via 'rasa

                        train core --config <config-1> <config-2>'. All models

                        in the provided directory are evaluated and compared

                        against each other. (default: False)

 

Python Logging Options:

  -v, --verbose         Be verbose. Sets logging level to INFO. (default:

                        None)

  -vv, --debug          Print lots of debugging statements. Sets logging level

                        to DEBUG. (default: None)

  --quiet               Be quiet! Sets logging level to WARNING. (default:

                        None)

比较Core配置

为Core模型选择配置,或为特定策略选择超参数,你需要知道Rasa Core如何归纳之前从未出现过的会话。尤其是在项目刚开始时,没有足够真实的会话来训练机器人,更不想再分出一部分数据作为测试集。

Rasa Core提供一些脚本帮你选择和微调策略配置。一旦达到满意的结果,你就可以在你的完整数据集上训练你的最终配置。要做到这一点,你首先必须要为你的不同配置训练模型。创建两个(或更多)包含你想比较的策略的配置文件,然后使用训练脚本的compare模式来训练你的模型:

$ rasa train core -c config_1.yml config_2.yml \

  -d domain.yml -s stories_folder --out comparison_models --runs 3 \

  --percentages 0 5 25 50 70 95

 

对于提供的每个策略配置,在训练数据中去掉你训练故事的0%、5%、25%、50%、70%和95%的的情况下,Rasa Core会进行多次训练。这样多次运行是为了保持结果一致。

此脚本完成后,你可以在compare模式下使用评估脚本去评估你刚刚训练的模型:

$ rasa test core -m comparison_models --stories stories_folder

--out comparison_results --evaluate-model-directory

 

这将评估所提供故事中的每个模型(也可以是训练或测试集),并绘制一些图表去显示哪个策略执行得最好。通过对全部故事进行评估,你可以衡量Rasa Core在故事集外的故事的预测情况。

要比较单个策略,要创建仅包含一个策略的配置文件。如果你不确定要比较哪些策略,我们建议你尝试EmbeddingPolicy 和KerasPolicy,看看那种策略更适合你。

 

注意

由于训练过程可能需要很长时间,我们建议以不被打断的方式在后台运行。

端对端评估

Rasa允许你进行端对端评估对话,运行测试对话并确保NLU和Core都能做出正确预测。

为此,你需要一些端到端格式的故事,其中包括NLU输出和原始文本。下面是一个例子:

## end-to-end story 1* greet: hello

   - utter_ask_howcanhelp* inform: show me [chinese](cuisine) restaurants

   - utter_ask_location* inform: in [Paris](location)

   - utter_ask_price

 

如果您已将端到端故事保存为名为e2e_stories.md的文件,你可以通过运行下面命令评估你的模型:

$ rasa test --stories e2e_stories.md --e2e

 

注意

确保models文件下的模型文件组合了core和nlu模型。如果没有包含NLU模型,Core将使用默认值 RegexInterpreter.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值