MixedLayer.h 1.9 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 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 "Layer.h"
#include "Operator.h"
Y
Yu Yang 已提交
19
#include "Projection.h"
Z
zhangjinchao01 已提交
20 21 22 23

namespace paddle {

/**
24 25
 * A mixed layer has multiple input layers.
 * Each input layer was processed by a Projection or Operator.
Z
zhangjinchao01 已提交
26 27 28 29 30 31 32
 * The results of all projections or Operators are summed together with bias
 * (if configured), and then go through an activation function and dropout
 * (if configured).
 *
 * The config file api is mixed_layer.
 */
class MixedLayer : public Layer {
W
Wu Yi 已提交
33
 public:
Z
zhangjinchao01 已提交
34 35 36 37
  explicit MixedLayer(const LayerConfig& config) : Layer(config) {}

  ~MixedLayer() {}

Y
Yu Yang 已提交
38 39
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
40

Y
Yu Yang 已提交
41 42 43 44
  void prefetch() override;
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
  void resetState() override;
Z
zhangjinchao01 已提交
45
  /**
46
   * setState() should be called after getState().
Z
zhangjinchao01 已提交
47 48
   * Argument state consists of all projections states.
   */
Y
Yu Yang 已提交
49
  void setState(LayerStatePtr state) override;
Z
zhangjinchao01 已提交
50 51 52
  /**
   * Return state which consists of all projections states.
   */
Y
Yu Yang 已提交
53
  LayerStatePtr getState() override;
Z
zhangjinchao01 已提交
54

W
Wu Yi 已提交
55
 protected:
Z
zhangjinchao01 已提交
56 57 58 59 60
  std::vector<std::unique_ptr<Projection>> projections_;
  std::vector<std::unique_ptr<Operator>> operators_;
  /// the matrix size of projection state
  std::vector<int> projectionStateMatrixSize_;
  std::unique_ptr<Weight> biases_;
61
  bool sharedBias_;
Z
zhangjinchao01 已提交
62 63
};
}  // namespace paddle