GatedRecurrentLayer.h 3.0 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

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. */

#pragma once

#include "GruCompute.h"
#include "Layer.h"
Y
Yu Yang 已提交
19 20
#include "SequenceToBatch.h"
#include "paddle/math/Matrix.h"
Z
zhangjinchao01 已提交
21 22 23 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

namespace paddle {

/**
 * @brief Please refer to "Junyoung Chung, Empirical Evaluation
 * of Gated Recurrent Neural Networks on Sequence Modeling".
 *
 * GatedRecurrentLayer takes 1 input layer with size * 3.
 * Input layer is diveded into 3 equal parts: (xz_t, xr_t, xi_t).
 * parameter and biasParameter is also diveded into 3 equal parts:
 *   - parameter consists of (U_z, U_r, U)
 *   - baisParameter consists of (bias_z, bias_r, bias_o)
 *
 * \f[
 * update \ gate: z_t = actGate(xz_t + U_z * h_{t-1} + bias_z) \\
 * reset \ gate: r_t = actGate(xr_t + U_r * h_{t-1} + bias_r) \\
 * output \ candidate: {h}_t = actNode(xi_t + U * dot(r_t, h_{t-1}) + bias_o) \\
 * hidden \ activation: h_t = dot((1-z_t), h_{t-1}) + dot(z_t, {h}_t) \\
 * \f]
 *
 * @note
 * - dot denotes "element-wise multiplication".
 * - actNode is defined by config active_type
 * - actGate is defined by config actvie_gate_type
 *
 * The config file is grumemory.
 */

class GatedRecurrentLayer : public Layer, public GruCompute {
public:
  explicit GatedRecurrentLayer(const LayerConfig& config) : Layer(config) {}

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

  void forward(PassType passType);

  void backward(const UpdateCallback& callback);

  void resetState();

  void setState(LayerStatePtr state);

  LayerStatePtr getState();

protected:
66 67 68 69 70 71 72 73 74 75 76 77 78
  void forwardSequence(int batchSize,
                       size_t numSequences,
                       const int* starts,
                       MatrixPtr inputValue);
  void backwardSequence(int batchSize,
                        size_t numSequences,
                        const int* starts,
                        MatrixPtr inputGrad);

  void forwardBatch(int batchSize,
                    size_t numSequences,
                    const int* starts,
                    MatrixPtr inputValue);
Z
zhangjinchao01 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  void backwardBatch(int batchSize, MatrixPtr inputGrad);

protected:
  std::unique_ptr<Weight> weight_;
  std::unique_ptr<Weight> gateWeight_;
  std::unique_ptr<Weight> stateWeight_;
  std::unique_ptr<Weight> bias_;

  Argument gate_;
  Argument resetOutput_;

  bool reversed_;
  bool useBatch_;
  std::unique_ptr<SequenceToBatch> batchValue_;
  std::unique_ptr<SequenceToBatch> batchGrad_;
  std::unique_ptr<ActivationFunction> activationGate_;

  MatrixPtr prevOutput_;
};

}  // namespace paddle