CRFLayer.cpp 3.7 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 27 28 29 30 31 32

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

namespace paddle {

REGISTER_LAYER(crf, CRFLayer);

bool CRFLayer::init(const LayerMap& layerMap,
                    const ParameterMap& parameterMap) {
  /* Initialize the basic parent class */
  Layer::init(layerMap, parameterMap);

  if (config_.type() == "crf") {
    CHECK_GE(inputLayers_.size(), 2UL);
    // the third output is sequence weight. one weight for each sequence
    CHECK_LE(inputLayers_.size(), 3UL);
  }

  // coeff only affect bp, keep consistent with CostLayer
33
  coeff_ = config_.coeff();
Z
zhangjinchao01 已提交
34 35 36 37 38 39 40 41 42 43 44
  if (inputLayers_.size() == 3) {
    weightLayer_ = inputLayers_[2];
  }

  numClasses_ = inputLayers_[0]->getSize();

  CHECK_GE(numClasses_, 2UL);

  CHECK_EQ(parameters_[0]->getSize(), numClasses_ * (numClasses_ + 2));

  parameter_ = parameters_[0];
P
Peng Li 已提交
45
  weight_.reset(new Weight(numClasses_ + 2, numClasses_, parameter_));
Z
zhangjinchao01 已提交
46 47 48 49

  // We don't need sequenceStartPositions because each sample of output_ is
  // for the cost of one sequence.
  setNeedSequenceInfo(false);
50

Z
zhangjinchao01 已提交
51 52 53 54 55 56
  return true;
}

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

57 58 59 60
  CHECK(!useGpu_) << "GPU is not supported";

  const Argument& output = getInput(0);
  const Argument& label = getInput(1);
Z
zhangjinchao01 已提交
61 62 63 64 65 66 67 68 69 70 71 72
  CHECK(label.sequenceStartPositions);
  CHECK(label.ids);

  int batchSize = output.getBatchSize();
  size_t numSequences = label.sequenceStartPositions->getSize() - 1;
  resizeOutput(numSequences, 1);

  const int* starts = label.sequenceStartPositions->getData(false);
  CHECK_EQ(starts[numSequences], batchSize);

  for (size_t i = 0; i < numSequences; ++i) {
    if (i >= crfs_.size()) {
73
      crfs_.emplace_back(numClasses_, weight_->getW()->getData());
Z
zhangjinchao01 已提交
74
    }
75 76 77 78
    output_.value->getData()[i] =
        crfs_[i].forward(output.value->getData() + numClasses_ * starts[i],
                         label.ids->getData() + starts[i],
                         starts[i + 1] - starts[i]);
Z
zhangjinchao01 已提交
79
  }
80

Z
zhangjinchao01 已提交
81 82 83 84 85 86
  if (weightLayer_) {
    const MatrixPtr& weight = getInputValue(*weightLayer_);
    getOutputValue()->dotMul(*getOutputValue(), *weight);
  }
}

87
void CRFLayer::backward(const UpdateCallback& callback) {
88 89
  const Argument& output = getInput(0);
  const Argument& label = getInput(1);
Z
zhangjinchao01 已提交
90 91 92
  const int* starts = label.sequenceStartPositions->getData(false);
  int numSequences = label.sequenceStartPositions->getSize() - 1;

93
  bool needWGrad = weight_->getWGrad() ? true : false;
Z
zhangjinchao01 已提交
94 95 96
  for (int i = 0; i < numSequences; ++i) {
    crfs_[i].backward(output.value->getData() + numClasses_ * starts[i],
                      label.ids->getData() + starts[i],
P
Peng Li 已提交
97 98 99 100 101
                      starts[i + 1] - starts[i],
                      needWGrad);
    real instanceWeight = weightLayer_
                              ? getInputValue(*weightLayer_)->getElement(i, 0)
                              : real(1.0f);
102 103 104 105 106
    instanceWeight *= coeff_;

    MatrixPtr grad = output.grad->subRowMatrix(starts[i], starts[i + 1]);
    grad->add(*crfs_[i].getXGrad(), real(1.0f), instanceWeight);
    if (needWGrad) {
P
Peng Li 已提交
107 108
      weight_->getWGrad()->add(
          *crfs_[i].getWGrad(), real(1.0f), instanceWeight);
Z
zhangjinchao01 已提交
109 110
    }
  }
111

Z
zhangjinchao01 已提交
112 113 114 115
  parameter_->incUpdate(callback);
}

}  // namespace paddle