CRFDecodingLayer.cpp 2.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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 "CRFDecodingLayer.h"

namespace paddle {

REGISTER_LAYER(crf_decoding, CRFDecodingLayer);

bool CRFDecodingLayer::init(const LayerMap& layerMap,
                            const ParameterMap& parameterMap) {
  if (!CRFLayer::init(layerMap, parameterMap)) {
    return false;
  }
  crf_.reset(new LinearChainCRF(
27
      numClasses_, parameter_->getBuf(PARAMETER_VALUE)->getData()));
Z
zhangjinchao01 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  return true;
}

void CRFDecodingLayer::forward(PassType passType) {
  Layer::forward(passType);

  CHECK(!useGpu_) << "GPU is not supported";

  const Argument& output = getInput(0);
  CHECK(output.sequenceStartPositions);

  size_t batchSize = output.getBatchSize();
  size_t numSequences = output.sequenceStartPositions->getSize() - 1;

  IVector::resizeOrCreate(output_.ids, batchSize, useGpu_);
  const int* starts = output.sequenceStartPositions->getData(false);
  CHECK_EQ(starts[numSequences], (int)batchSize);

  for (size_t i = 0; i < numSequences; ++i) {
    crf_->decode(output.value->getData() + numClasses_ * starts[i],
48 49
                 output_.ids->getData() + starts[i],
                 starts[i + 1] - starts[i]);
Z
zhangjinchao01 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  }

  if (inputLayers_.size() == 2) {
    const Argument& label = getInput(1);
    resizeOutput(batchSize, 1);
    CHECK(label.ids);
    real* error = output_.value->getData();
    int* ids = label.ids->getData();
    int* result = output_.ids->getData();
    for (size_t i = 0; i < batchSize; ++i) {
      error[i] = ids[i] == result[i] ? 0 : 1;
    }
  }
}

void CRFDecodingLayer::backward(const UpdateCallback& callback) {
  parameter_->incUpdate(callback);
}

}  // namespace paddle