DotProdLayer.cpp 2.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
R
ranqiu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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 "Layer.h"
#include "paddle/math/Matrix.h"
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"

namespace paddle {

/**
R
ranqiu 已提交
23
 * @brief A layer for computing the dot product of two vectors.
R
ranqiu 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * Input1: vector (batchSize * dim)
 * Input2: vector (batchSize * dim)
 * Output: a matrix: (batchSize * 1)
 */

class DotProdLayer : public Layer {
public:
  explicit DotProdLayer(const LayerConfig& config) : Layer(config) {}

  ~DotProdLayer() {}

  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;

  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
};

REGISTER_LAYER(dot_prod, DotProdLayer);

bool DotProdLayer::init(const LayerMap& layerMap,
                        const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);

  CHECK_EQ(inputLayers_.size(), 2U);
R
ranqiu 已提交
49 50
  CHECK_EQ(1UL, getSize())
      << "The output dimensionality of this layer should be fixed to 1.";
R
ranqiu 已提交
51 52 53 54 55 56 57 58 59 60 61 62

  return true;
}

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

  MatrixPtr inV0 = getInputValue(0);
  MatrixPtr inV1 = getInputValue(1);

  size_t batchSize = inV0->getHeight();
  CHECK_EQ(inV1->getHeight(), batchSize);
R
ranqiu 已提交
63
  CHECK_EQ(inV0->getWidth(), inV1->getWidth());
R
ranqiu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

  {
    REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
    reserveOutput(batchSize, 1);
  }

  MatrixPtr outV = getOutputValue();
  {
    REGISTER_TIMER_INFO("FwDotProdTimer", getName().c_str());
    outV->sumOfProducts(*inV0, *inV1, 1, 0);
  }
}

void DotProdLayer::backward(const UpdateCallback& callback) {
  MatrixPtr inV0 = getInputValue(0);
  MatrixPtr inV1 = getInputValue(1);
  MatrixPtr outG = getOutputGrad();
  MatrixPtr inG0 = getInputGrad(0);
  MatrixPtr inG1 = getInputGrad(1);

  {
    REGISTER_TIMER_INFO("BwDotProdTimer", getName().c_str());

    if (inG0) {
      inG0->addRowScale(0, *inV1, *outG);
    }

    if (inG1) {
      inG1->addRowScale(0, *inV0, *outG);
    }
  }
}

}  // namespace paddle