ExpandLayer.h 2.1 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 66 67
/* Copyright (c) 2016 Baidu, Inc. 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. */

#pragma once

#include "Layer.h"
#include "paddle/math/Matrix.h"

namespace paddle {

/**
 * A layer for "Expand Dense data or (sequence data where the length of each
 * sequence is one) to sequence data."
 *
 * It should have exactly 2 input, one for data, one for size:
 * - first one for data
 *   - If ExpandLevel = kNonSeq: dense data
 *   - If ExpandLevel = kSeq: sequence data where the length of each sequence is
 * one
 * - second one only for sequence info
 *   - should be sequence data with or without sub-sequence.
 *
 * And the output size is the batch size(not instances) of second input.
 *
 * The config file api is expand_layer.
 */

class ExpandLayer : public Layer {
protected:
  std::unique_ptr<Weight> biases_;
  /// if input[0] is dense data, ExpandLevel=kNonSeq;
  /// if input[0] is sequence data, ExpandLevel=kSeq
  enum ExpandLevel { kNonSeq = 0, kSeq = 1 };
  /// store the ExpandLevel
  int type_;
  // TODO(luotao) use ICpuGpuVectorPtr to merge cpuExpandStartsPos_
  // and expandStartsPos_
  /// expanded sequenceStartPositions or subSequenceStartPositions
  /// of input[1]
  IVectorPtr cpuExpandStartsPos_;
  /// point to cpuExpandStartsPos_ when useGpu_ is false,
  /// copy from cpuExpandStartsPos_ when useGpu_ is true
  IVectorPtr expandStartsPos_;

public:
  explicit ExpandLayer(const LayerConfig& config) : Layer(config) {}

  ~ExpandLayer() {}

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

  void forward(PassType passType);
  void backward(const UpdateCallback& callback = nullptr);
};

}  // namespace paddle