IdentityProjection.cpp 3.1 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

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 "Projection.h"
Y
Yu Yang 已提交
16
#include "paddle/utils/Stat.h"
Z
zhangjinchao01 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30

namespace paddle {

/**
 * IdentityProjection performs addition:
 * \f[
 *   out.row[i] += in.row[i]
 * \f]
 *
 * The config file api is identity_projection.
 */
class IdentityProjection : public Projection {
public:
  IdentityProjection(const ProjectionConfig& config,
31 32
                     const ParameterPtr& parameter,
                     bool useGpu);
Z
zhangjinchao01 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  virtual void forward();
  virtual void backward(const UpdateCallback& callback);
};

REGISTER_PROJECTION(identity, IdentityProjection);

/**
 * Constructed function.
 * @note IdentityProjection should not have any parameter.
 */
IdentityProjection::IdentityProjection(const ProjectionConfig& config,
                                       const ParameterPtr& parameter,
                                       bool useGpu)
    : Projection(config, parameter, useGpu) {
  CHECK(!parameter) << "'identity' projection should not have any parameter";
}

void IdentityProjection::forward() { out_->value->add(*in_->value); }

void IdentityProjection::backward(const UpdateCallback& callback) {
  if (in_->grad) {
    in_->grad->add(*out_->grad);
  }
}

/**
 * IdentityOffsetProjection likes IdentityProjection, but layer size may be
 * smaller
 * than input size. It selects dimensions [offset, offset+layer_size) from input
 * to
 * perform addition:
 * \f[
 *   out.row[i] += in.row[i + \textrm{offset}]
 * \f]
 *
 * The config file api is identity_projection.
 */
class IdentityOffsetProjection : public Projection {
public:
  IdentityOffsetProjection(const ProjectionConfig& config,
73 74
                           const ParameterPtr& parameter,
                           bool useGpu);
Z
zhangjinchao01 已提交
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 100 101 102 103
  virtual void forward();
  virtual void backward(const UpdateCallback& callback);
};

REGISTER_PROJECTION(identity_offset, IdentityOffsetProjection);

/**
 * Constructed function.
 * @note IdentityOffsetProjection should not have any parameter.
 */
IdentityOffsetProjection::IdentityOffsetProjection(
    const ProjectionConfig& config, const ParameterPtr& parameter, bool useGpu)
    : Projection(config, parameter, useGpu) {
  CHECK(!parameter) << "'identity_offset' projection "
                       "should not have any parameter";
  CHECK_LE(config.output_size() + config.offset(), config.input_size());
}

void IdentityOffsetProjection::forward() {
  out_->value->addAtOffset(*in_->value, config_.offset());
}

void IdentityOffsetProjection::backward(const UpdateCallback& callback) {
  if (in_->grad) {
    in_->grad->addAtOffset(*out_->grad, config_.offset());
  }
}

}  // namespace paddle