chunk_eval_op.cc 5.9 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/chunk_eval_op.h"

namespace paddle {
namespace operators {

class ChunkEvalOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Inference"),
                   "Input(Inference) of ChunkEvalOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"),
                   "Input(Label) of ChunkEvalOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Precision"),
                   "Output(Precision) of ChunkEvalOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Recall"),
                   "Output(Recall) of ChunkEvalOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("F1-Score"),
                   "Output(F1-Score) of ChunkEvalOp should not be null.");

    auto inference_dim = ctx->GetInputDim("Inference");
    auto label_dim = ctx->GetInputDim("Label");

    PADDLE_ENFORCE(inference_dim == label_dim,
                   "Inference's shape must be the same as Label's shape.");

    ctx->SetOutputDim("Precision", {1});
    ctx->SetOutputDim("Recall", {1});
    ctx->SetOutputDim("F1-Score", {1});
  }

47
 protected:
Y
yangyaming 已提交
48
  framework::OpKernelType GetKernelType(
G
guosheng 已提交
49
      const framework::ExecutionContext &ctx) const override {
Y
yangyaming 已提交
50 51
    return framework::OpKernelType(framework::DataType::FP32,
                                   ctx.device_context());
G
guosheng 已提交
52 53 54 55 56 57 58 59 60
  }
};

class ChunkEvalOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  ChunkEvalOpMaker(framework::OpProto *proto,
                   framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Inference",
61 62 63 64 65 66
             "(Tensor, default: Tensor<int>). Predictions from the network.");
    AddInput("Label",
             "(Tensor, default: Tensor<int>). The true tag sequences.");
    AddOutput("Precision",
              "(float). The evaluated precision (called positive predictive "
              "value) of chunks on the given mini-batch.");
G
guosheng 已提交
67
    AddOutput("Recall",
68 69
              "(float). The evaluated recall (true positive rate or "
              "sensitivity) of chunks on the given mini-batch.");
G
guosheng 已提交
70
    AddOutput("F1-Score",
71 72 73 74 75 76 77 78
              "(float). The evaluated F1-Score on the given mini-batch.");
    AddAttr<int>("num_chunk_types",
                 "(int). The number of chunk type. See below for details.");
    AddAttr<std::string>(
        "chunk_scheme",
        "(string, default IOB). The labeling scheme indicating "
        "how to encode the chunks. Must be IOB, IOE, IOBES or plain. See below "
        "for details.")
G
guosheng 已提交
79
        .SetDefault("IOB");
80 81 82 83
    AddAttr<std::vector<int>>("excluded_chunk_types",
                              "(list<int>) A list including chunk type ids "
                              "indicating chunk types that are not counted. "
                              "See below for details.")
G
guosheng 已提交
84 85
        .SetDefault(std::vector<int>{});
    AddComment(R"DOC(
Y
yangyaming 已提交
86
For some basics of chunking, please refer to
87 88 89
‘Chunking with Support Vector Mechines <https://aclanthology.info/pdf/N/N01/N01-1025.pdf>’.


Y
yangyaming 已提交
90 91
CheckEvalOp computes the precision, recall, and F1-score of chunk detection,
and supports IOB, IOE, IOBES and IO (also known as plain) tagging schemes.
92 93 94 95 96 97 98 99
Here is a NER example of labeling for these tagging schemes:

 	     Li     Ming    works  at  Agricultural   Bank   of    China  in  Beijing.
  IO:    I-PER  I-PER   O      O   I-ORG          I-ORG  I-ORG I-ORG  O   I-LOC
  IOB:   B-PER  I-PER   O      O   B-ORG          I-ORG  I-ORG I-ORG  O   B-LOC
  IOE:   I-PER  E-PER   O      O   I-ORG          I-ORG  I-ORG E-ORG  O   E-LOC
  IOBES: B-PER  E-PER   O      O   I-ORG          I-ORG  I-ORG E-ORG  O   S-LOC

Y
yangyaming 已提交
100
There are three chunk types(named entity types) including PER(person), ORG(orgnazation)
101 102
and LOC(LOCATION), and we can see that the labels have the form <tag type>-<chunk type>.

Y
yangyaming 已提交
103 104 105
Since the calculations actually use label ids rather than labels, extra attention
should be paid when mapping labels to ids to make CheckEvalOp work. The key point
is that the listed equations are satisfied by ids.
106 107 108 109

    tag_type = label % num_tag_type
    chunk_type = label / num_tag_type

Y
yangyaming 已提交
110
where `num_tag_type` is the num of tag types in the tagging scheme, `num_chunk_type`
111
is the num of chunk types, and `tag_type` get its value from the following table.
G
guosheng 已提交
112 113

    Scheme Begin Inside End   Single
114 115 116 117
     plain   0     -      -     -
     IOB     0     1      -     -
     IOE     -     0      1     -
     IOBES   0     1      2     3
G
guosheng 已提交
118

Y
yangyaming 已提交
119
Still use NER as example, assuming the tagging scheme is IOB while chunk types are ORG,
120
PER and LOC. To satisfy the above equations, the label map can be like this:
G
guosheng 已提交
121 122 123 124 125 126 127 128 129

    B-ORG  0
    I-ORG  1
    B-PER  2
    I-PER  3
    B-LOC  4
    I-LOC  5
    O      6

Y
yangyaming 已提交
130 131 132
It’s not hard to verify the equations noting that the num of chunk types
is 3 and the num of tag types in IOB scheme is 2. For example, the label
id of I-LOC is 5, the tag type id of I-LOC is 1, and the chunk type id of
133
I-LOC is 2, which consistent with the results from the equations.
G
guosheng 已提交
134 135 136 137 138 139 140 141 142 143 144 145
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(chunk_eval, ops::ChunkEvalOp,
                             ops::ChunkEvalOpMaker);
REGISTER_OP_CPU_KERNEL(chunk_eval,
                       ops::ChunkEvalKernel<paddle::platform::CPUPlace, float>);