|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/crf_decoding_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | +class CRFDecodingOpMaker : public framework::OpProtoAndCheckerMaker { |
| 20 | + public: |
| 21 | + CRFDecodingOpMaker(framework::OpProto* proto, |
| 22 | + framework::OpAttrChecker* op_checker) |
| 23 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 24 | + AddInput("Emission", |
| 25 | + "(LoDTensor, default: LoDTensor<float>). A LoDTensor with shape " |
| 26 | + "[N x D] where N is the size of the mini-batch and D is the total " |
| 27 | + "tag number. This input is the unscaled emission weight matrix of " |
| 28 | + "the linear_chain_crf operator."); |
| 29 | + AddInput( |
| 30 | + "Transition", |
| 31 | + "(Tensor, default: Tensor<float>). A Tensor with shape [(D + 2) x D]. " |
| 32 | + "This input is the transition weights learned by the linear_chain_crf " |
| 33 | + "operator, denoted as w. The 1st row of w are transition weights for " |
| 34 | + "the start mask. The 2nd row of w are transition weights for the end " |
| 35 | + "mask. Transition weights between other tags begin from the 3rd row of " |
| 36 | + "w. See more details in comments of the linear_chain_crf operator."); |
| 37 | + AddInput( |
| 38 | + "Label", |
| 39 | + "(LoDTensor, LoDTensor<int>). The ground truth with shape " |
| 40 | + "[N x 1]. This input is optional. See more details in the operator's " |
| 41 | + "comments.") |
| 42 | + .AsDispensable(); |
| 43 | + AddOutput("ViterbiPath", |
| 44 | + "(LoDTensor, LoDTensor<int>). The decoding results. What to " |
| 45 | + "return changes depending on whether the Input(Label) (the groud " |
| 46 | + "truth) is given. See more details in the operator's comment."); |
| 47 | + AddComment(R"DOC( |
| 48 | +The crf_decoding operator reads the emission feature weights and the transition |
| 49 | +freature weights learned by the linear_chain_crf operator. It implements the |
| 50 | +Viterbi algorithm which is a dynamic programming algorithm for finding the most |
| 51 | +likely sequence of hidden states, called the Viterbi path, that results in a |
| 52 | +sequence of observed tags. |
| 53 | +
|
| 54 | +The output of this operator changes according to whether Input(Label) is given: |
| 55 | +
|
| 56 | +1. Input(Label) is given: |
| 57 | +
|
| 58 | +This happens in training. This operator is used to co-work with the chunk_eval |
| 59 | +operator. |
| 60 | +
|
| 61 | +When Input(Label) is given, the crf_decoding operator returns a row vector |
| 62 | +with shape [N x 1] whose values are fixed to be 0, indicating an incorrect |
| 63 | +prediction, or 1 indicating a tag is correctly predicted. Such an ouput is the |
| 64 | +input to chunk_eval operator. |
| 65 | +
|
| 66 | +2. Input(Label) is not given: |
| 67 | +
|
| 68 | +This is the standard decoding process. |
| 69 | +
|
| 70 | +The crf_decoding operator returns a row vecotr with shape [N x 1] whose values |
| 71 | +range from 0 to maximum tag number - 1. Each element indicates an index of a |
| 72 | +predicted tag. |
| 73 | +)DOC"); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +class CRFDecodingOp : public framework::OperatorWithKernel { |
| 78 | + public: |
| 79 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 80 | + |
| 81 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 82 | + PADDLE_ENFORCE(ctx->HasInput("Emission"), |
| 83 | + "Input(Emission) should be not null."); |
| 84 | + PADDLE_ENFORCE(ctx->HasInput("Transition"), |
| 85 | + "Input(Transition) should be not null."); |
| 86 | + |
| 87 | + PADDLE_ENFORCE(ctx->HasOutput("ViterbiPath"), |
| 88 | + "Output(ViterbiPath) should be not null."); |
| 89 | + |
| 90 | + auto emission_dims = ctx->GetInputDim("Emission"); |
| 91 | + PADDLE_ENFORCE_EQ(emission_dims.size(), 2UL, |
| 92 | + "The Input(Emission) should be a 2-D tensor."); |
| 93 | + PADDLE_ENFORCE(emission_dims[0], "An empty mini-batch is not allowed."); |
| 94 | + |
| 95 | + auto transition_dims = ctx->GetInputDim("Transition"); |
| 96 | + PADDLE_ENFORCE_EQ(transition_dims.size(), 2UL, |
| 97 | + "The Input(Transition) should be a 2-D tensor."); |
| 98 | + PADDLE_ENFORCE_EQ( |
| 99 | + transition_dims[0] - 2, transition_dims[1], |
| 100 | + "An invalid dimension for the Input(Transition), which should " |
| 101 | + "be a 2-D tensor with shape [(D + 2) x D]."); |
| 102 | + PADDLE_ENFORCE_EQ( |
| 103 | + emission_dims[1], transition_dims[1], |
| 104 | + "The 2nd dimension of the Input(Emission) and the Input(Transition) " |
| 105 | + "should be equal to the tag number."); |
| 106 | + |
| 107 | + if (ctx->HasInput("Label")) { |
| 108 | + auto label_dims = ctx->GetInputDim("Label"); |
| 109 | + PADDLE_ENFORCE(label_dims.size() == 2UL && label_dims[1] == 1UL, |
| 110 | + "The Input(Label) should be a 2-D tensor with the 2nd " |
| 111 | + "dimensions fixed to 1."); |
| 112 | + PADDLE_ENFORCE_EQ( |
| 113 | + emission_dims[0], label_dims[0], |
| 114 | + "The height of Input(Emission) and the height of Input(Label) " |
| 115 | + "should be the same."); |
| 116 | + } |
| 117 | + |
| 118 | + ctx->ShareLoD("Emission", /*->*/ "ViterbiPath"); |
| 119 | + ctx->SetOutputDim("ViterbiPath", {emission_dims[0], 1}); |
| 120 | + } |
| 121 | + |
| 122 | + protected: |
| 123 | + framework::DataType IndicateDataType( |
| 124 | + const framework::ExecutionContext& ctx) const override { |
| 125 | + return framework::ToDataType(ctx.Input<LoDTensor>("Emission")->type()); |
| 126 | + } |
| 127 | +}; |
| 128 | +} // namespace operators |
| 129 | +} // namespace paddle |
| 130 | + |
| 131 | +namespace ops = paddle::operators; |
| 132 | +REGISTER_OP_WITHOUT_GRADIENT(crf_decoding, ops::CRFDecodingOp, |
| 133 | + ops::CRFDecodingOpMaker); |
| 134 | +REGISTER_OP_CPU_KERNEL( |
| 135 | + crf_decoding, ops::CRFDecodingOpKernel<paddle::platform::CPUPlace, float>, |
| 136 | + ops::CRFDecodingOpKernel<paddle::platform::CPUPlace, double>); |
0 commit comments