CrossChannelNormLayer.cpp 5.1 KB
Newer Older
G
gaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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"
16
#include "NormLayer.h"
G
gaoyuan 已提交
17 18 19 20 21
#include "paddle/math/BaseMatrix.h"
#include "paddle/math/Matrix.h"

namespace paddle {

G
gaoyuan 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
MatrixPtr CrossChannelNormLayer::createSampleMatrix(MatrixPtr data,
                                                    size_t iter,
                                                    size_t spatialDim) {
  return Matrix::create(data->getData() + iter * channels_ * spatialDim,
                        channels_,
                        spatialDim,
                        false,
                        useGpu_);
}

MatrixPtr CrossChannelNormLayer::createSpatialMatrix(MatrixPtr data,
                                                     size_t iter,
                                                     size_t spatialDim) {
  return Matrix::create(
      data->getData() + iter * spatialDim, 1, spatialDim, false, useGpu_);
}

Y
yangyaming 已提交
39 40 41 42 43 44 45 46 47 48
bool CrossChannelNormLayer::init(const LayerMap& layerMap,
                                 const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);
  CHECK(parameters_[0]);
  const NormConfig& conf = config_.inputs(0).norm_conf();
  channels_ = conf.channels();
  scale_.reset(new Weight(channels_, 1, parameters_[0]));
  return true;
}

49
void CrossChannelNormLayer::forward(PassType passType) {
G
gaoyuan 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  Layer::forward(passType);
  MatrixPtr inV = getInputValue(0);

  size_t batchSize = inV->getHeight();
  size_t dataDim = inV->getWidth();
  CHECK_EQ(getSize(), dataDim);

  reserveOutput(batchSize, dataDim);
  MatrixPtr outV = getOutputValue();
  size_t spatialDim = dataDim / channels_;

  Matrix::resizeOrCreate(dataBuffer_, batchSize, dataDim, false, useGpu_);
  Matrix::resizeOrCreate(spatialBuffer_, 1, spatialDim, false, useGpu_);
  Matrix::resizeOrCreate(normBuffer_, batchSize, spatialDim, false, useGpu_);
  normBuffer_->zeroMem();
  // add eps to avoid overflow
  normBuffer_->addScalar(*normBuffer_, 1e-6);
  inV->square2(*dataBuffer_);
  for (size_t i = 0; i < batchSize; i++) {
G
gaoyuan 已提交
69 70 71 72 73
    const MatrixPtr inVTmp = createSampleMatrix(inV, i, spatialDim);
    const MatrixPtr dataTmp = createSampleMatrix(dataBuffer_, i, spatialDim);
    MatrixPtr outVTmp = createSampleMatrix(outV, i, spatialDim);
    MatrixPtr normTmp = createSpatialMatrix(normBuffer_, i, spatialDim);

G
gaoyuan 已提交
74
    // compute norm.
G
gaoyuan 已提交
75
    spatialBuffer_->sumCols(*dataTmp, 1, 0);
Y
yangyaming 已提交
76
    spatialBuffer_->add(*normTmp);
G
gaoyuan 已提交
77 78
    spatialBuffer_->sqrt2(*spatialBuffer_);
    normTmp->copyFrom(*spatialBuffer_);
G
gaoyuan 已提交
79 80
    outVTmp->copyFrom(*inVTmp);
    outVTmp->divRowVector(*spatialBuffer_);
G
gaoyuan 已提交
81
    // scale the layer.
G
gaoyuan 已提交
82
    outVTmp->mulColVector(*scale_->getW());
G
gaoyuan 已提交
83 84 85
  }
}

86
void CrossChannelNormLayer::backward(const UpdateCallback& callback) {
G
gaoyuan 已提交
87 88 89 90 91 92 93 94 95
  MatrixPtr inG = getInputGrad(0);
  MatrixPtr inV = getInputValue(0);
  MatrixPtr outG = getOutputGrad();
  MatrixPtr outV = getOutputValue();

  size_t batchSize = inG->getHeight();
  size_t dataDim = inG->getWidth();
  size_t spatialDim = dataDim / channels_;

Y
yangyaming 已提交
96 97 98
  MatrixPtr inGBuffer;
  Matrix::resizeOrCreate(inGBuffer, channels_, spatialDim, false, useGpu_);

G
gaoyuan 已提交
99 100
  dataBuffer_->dotMul(*outG, *outV);
  Matrix::resizeOrCreate(scaleDiff_, channels_, 1, false, useGpu_);
101 102
  Matrix::resizeOrCreate(channelBuffer_, channels_, 1, false, useGpu_);
  Matrix::resizeOrCreate(sampleBuffer_, channels_, spatialDim, false, useGpu_);
G
gaoyuan 已提交
103 104
  scaleDiff_->zeroMem();
  for (size_t i = 0; i < batchSize; i++) {
G
gaoyuan 已提交
105 106 107 108 109 110 111
    MatrixPtr outGTmp = createSampleMatrix(outG, i, spatialDim);
    const MatrixPtr dataTmp = createSampleMatrix(dataBuffer_, i, spatialDim);
    const MatrixPtr inVTmp = createSampleMatrix(inV, i, spatialDim);
    const MatrixPtr inGTmp = createSampleMatrix(inG, i, spatialDim);
    const MatrixPtr normTmp = createSpatialMatrix(normBuffer_, i, spatialDim);

    channelBuffer_->sumRows(*dataTmp, 1, 0);
G
gaoyuan 已提交
112 113 114 115
    channelBuffer_->dotDiv(*channelBuffer_, *(scale_->getW()));
    // store a / scale[i] in scaleDiff_ temporary
    scaleDiff_->add(*channelBuffer_, 1.);

G
gaoyuan 已提交
116
    sampleBuffer_->dotMul(*inVTmp, *outGTmp);
Y
yangyaming 已提交
117
    spatialBuffer_->sumCols(*sampleBuffer_, 1., 0.);
G
gaoyuan 已提交
118
    // scale the grad
Y
yangyaming 已提交
119 120
    inGBuffer->copyFrom(*inVTmp);
    inGBuffer->mulRowVector(*spatialBuffer_);
G
gaoyuan 已提交
121 122
    // divide by square of norm
    spatialBuffer_->dotMul(*normTmp, *normTmp);
Y
yangyaming 已提交
123
    inGBuffer->divRowVector(*spatialBuffer_);
G
gaoyuan 已提交
124
    // subtract
Y
yangyaming 已提交
125
    inGBuffer->add(*outGTmp, -1, 1);
G
gaoyuan 已提交
126
    // divide by norm
Y
yangyaming 已提交
127
    inGBuffer->divRowVector(*normTmp);
G
gaoyuan 已提交
128
    // scale the diff
Y
yangyaming 已提交
129 130 131
    inGBuffer->mulColVector(*scale_->getW());

    inGTmp->add(*inGBuffer);
G
gaoyuan 已提交
132 133
  }
  // updata scale
Y
yangyaming 已提交
134
  if (scale_->getWGrad()) scale_->getWGrad()->add(*scaleDiff_);
G
gaoyuan 已提交
135 136 137 138
  scale_->getParameterPtr()->incUpdate(callback);
}

}  // namespace paddle