OuterProdLayer.cpp 4.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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"
X
Xin Pan 已提交
16
#include "paddle/legacy/math/Matrix.h"
X
Xin Pan 已提交
17 18
#include "paddle/legacy/utils/Logging.h"
#include "paddle/legacy/utils/Stat.h"
Z
zhangjinchao01 已提交
19 20 21 22

namespace paddle {

/**
Q
qijun 已提交
23 24 25 26 27
 * @brief A layer for computing the outer product of two vectors
 * @note used in NEURAL TURING MACHINE
 * Input1: vector (batchSize * dim1)
 * Input2: vector (batchSize * dim2)
 * Output: a matrix: (batchSize * (dim1*dim2))
Z
zhangjinchao01 已提交
28 29 30
 */

class OuterProdLayer : public Layer {
W
Wu Yi 已提交
31
 protected:
Z
zhangjinchao01 已提交
32 33 34 35
  MatrixPtr tmpMtx0;
  MatrixPtr tmpRow0;
  MatrixPtr tmpRow1;

W
Wu Yi 已提交
36
 public:
Z
zhangjinchao01 已提交
37 38 39 40
  explicit OuterProdLayer(const LayerConfig& config) : Layer(config) {}

  ~OuterProdLayer() {}

Y
Yu Yang 已提交
41 42
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
43

Y
Yu Yang 已提交
44 45
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
};

REGISTER_LAYER(out_prod, OuterProdLayer);

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

  CHECK_EQ(inputLayers_.size(), 2U);

  size_t dim0 = inputLayers_[0]->getSize();
  size_t dim1 = inputLayers_[1]->getSize();

  CHECK_EQ(dim0 * dim1, getSize()) << "Dimension mismatch";

61 62 63 64 65 66 67 68
  tmpRow0 = Matrix::create(
      nullptr, /* height= */ 1, dim0, /* trans= */ false, useGpu_);
  tmpRow1 = Matrix::create(
      nullptr, /* height= */ 1, dim1, /* trans= */ false, useGpu_);
  tmpMtx0 = Matrix::create(nullptr,
                           /* height= */ dim0,
                           dim1,
                           /* trans= */ false,
Z
zhangjinchao01 已提交
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 98 99
                           useGpu_);
  return true;
}

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

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

  size_t batchSize = inV0->getHeight();
  size_t dim0 = inV0->getWidth();
  size_t dim1 = inV1->getWidth();

  CHECK_EQ(dim0 * dim1, getSize());
  CHECK_EQ(inV1->getHeight(), batchSize);

  {
    REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
    reserveOutput(batchSize, dim0 * dim1);
  }

  MatrixPtr outV = getOutputValue();

  {
    REGISTER_TIMER_INFO("FwOutProdTimer", getName().c_str());
    for (size_t i = 0; i < batchSize; i++) {
      tmpMtx0->setData(outV->getData() + i * dim0 * dim1);
      tmpRow0->setData(inV0->getData() + i * dim0);
      tmpRow1->setData(inV1->getData() + i * dim1);

100
      tmpMtx0->mul(*tmpRow0->getTranspose(), *tmpRow1);
Z
zhangjinchao01 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    }
  }
}

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

  size_t batchSize = inV0->getHeight();
  size_t dim0 = inV0->getWidth();
  size_t dim1 = inV1->getWidth();

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

    if (inG0) {
      for (size_t i = 0; i < batchSize; i++) {
        tmpMtx0->setData(outG->getData() + i * dim0 * dim1);
        tmpRow0->setData(inG0->getData() + i * dim0);
        tmpRow1->setData(inV1->getData() + i * dim1);

125
        tmpRow0->mul(*tmpRow1, *tmpMtx0->getTranspose(), 1, 1);
Z
zhangjinchao01 已提交
126 127 128 129 130 131 132 133 134
      }
    }

    if (inG1) {
      for (size_t i = 0; i < batchSize; i++) {
        tmpMtx0->setData(outG->getData() + i * dim0 * dim1);
        tmpRow0->setData(inV0->getData() + i * dim0);
        tmpRow1->setData(inG1->getData() + i * dim1);

135
        tmpRow1->mul(*tmpRow0, *tmpMtx0, 1, 1);
Z
zhangjinchao01 已提交
136 137 138 139 140 141
      }
    }
  }
}

}  // namespace paddle