Caffe
recurrent_layer.hpp
1 #ifndef CAFFE_RECURRENT_LAYER_HPP_
2 #define CAFFE_RECURRENT_LAYER_HPP_
3 
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 #include "caffe/blob.hpp"
9 #include "caffe/common.hpp"
10 #include "caffe/layer.hpp"
11 #include "caffe/net.hpp"
12 #include "caffe/proto/caffe.pb.h"
13 #include "caffe/util/format.hpp"
14 
15 namespace caffe {
16 
17 template <typename Dtype> class RecurrentLayer;
18 
25 template <typename Dtype>
26 class RecurrentLayer : public Layer<Dtype> {
27  public:
28  explicit RecurrentLayer(const LayerParameter& param)
29  : Layer<Dtype>(param) {}
30  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
31  const vector<Blob<Dtype>*>& top);
32  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
33  const vector<Blob<Dtype>*>& top);
34  virtual void Reset();
35 
36  virtual inline const char* type() const { return "Recurrent"; }
37  virtual inline int MinBottomBlobs() const {
38  int min_bottoms = 2;
39  if (this->layer_param_.recurrent_param().expose_hidden()) {
40  vector<string> inputs;
41  this->RecurrentInputBlobNames(&inputs);
42  min_bottoms += inputs.size();
43  }
44  return min_bottoms;
45  }
46  virtual inline int MaxBottomBlobs() const { return MinBottomBlobs() + 1; }
47  virtual inline int ExactNumTopBlobs() const {
48  int num_tops = 1;
49  if (this->layer_param_.recurrent_param().expose_hidden()) {
50  vector<string> outputs;
51  this->RecurrentOutputBlobNames(&outputs);
52  num_tops += outputs.size();
53  }
54  return num_tops;
55  }
56 
57  virtual inline bool AllowForceBackward(const int bottom_index) const {
58  // Can't propagate to sequence continuation indicators.
59  return bottom_index != 1;
60  }
61 
62  protected:
67  virtual void FillUnrolledNet(NetParameter* net_param) const = 0;
68 
74  virtual void RecurrentInputBlobNames(vector<string>* names) const = 0;
75 
81  virtual void RecurrentInputShapes(vector<BlobShape>* shapes) const = 0;
82 
88  virtual void RecurrentOutputBlobNames(vector<string>* names) const = 0;
89 
96  virtual void OutputBlobNames(vector<string>* names) const = 0;
97 
143  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
144  const vector<Blob<Dtype>*>& top);
145  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
146  const vector<Blob<Dtype>*>& top);
147  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
148  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
149 
151  shared_ptr<Net<Dtype> > unrolled_net_;
152 
154  int N_;
155 
160  int T_;
161 
164 
170 
176 
177  vector<Blob<Dtype>* > recur_input_blobs_;
178  vector<Blob<Dtype>* > recur_output_blobs_;
179  vector<Blob<Dtype>* > output_blobs_;
180  Blob<Dtype>* x_input_blob_;
181  Blob<Dtype>* x_static_input_blob_;
182  Blob<Dtype>* cont_input_blob_;
183 };
184 
185 } // namespace caffe
186 
187 #endif // CAFFE_RECURRENT_LAYER_HPP_
virtual void LayerSetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: recurrent_layer.cpp:14
bool static_input_
Whether the layer has a "static" input copied across all timesteps.
Definition: recurrent_layer.hpp:163
virtual int MinBottomBlobs() const
Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is requi...
Definition: recurrent_layer.hpp:37
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
virtual bool AllowForceBackward(const int bottom_index) const
Return whether to allow force_backward for a given bottom blob index.
Definition: recurrent_layer.hpp:57
int last_layer_index_
The last layer to run in the network. (Any later layers are losses added to force the recurrent net t...
Definition: recurrent_layer.hpp:169
virtual void RecurrentInputBlobNames(vector< string > *names) const =0
Fills names with the names of the 0th timestep recurrent input Blob&s. Subclasses should define this ...
virtual void Forward_cpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Definition: recurrent_layer.cpp:245
virtual void Forward_gpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Using the GPU device, compute the layer output. Fall back to Forward_cpu() if unavailable.
int T_
The number of timesteps in the layer&#39;s input, and the number of timesteps over which to backpropagate...
Definition: recurrent_layer.hpp:160
virtual void RecurrentOutputBlobNames(vector< string > *names) const =0
Fills names with the names of the Tth timestep recurrent output Blob&s. Subclasses should define this...
virtual void RecurrentInputShapes(vector< BlobShape > *shapes) const =0
Fills shapes with the shapes of the recurrent input Blob&s. Subclasses should define this – see RNNL...
shared_ptr< Net< Dtype > > unrolled_net_
A Net to implement the Recurrent functionality.
Definition: recurrent_layer.hpp:151
virtual void Reshape(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs...
Definition: recurrent_layer.cpp:183
virtual void Backward_cpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Using the CPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
Definition: recurrent_layer.cpp:277
virtual void OutputBlobNames(vector< string > *names) const =0
Fills names with the names of the output blobs, concatenated across all timesteps. Should return a name for each top Blob. Subclasses should define this – see RNNLayer and LSTMLayer for examples.
virtual const char * type() const
Returns the layer type.
Definition: recurrent_layer.hpp:36
Layer(const LayerParameter &param)
Definition: layer.hpp:40
virtual int ExactNumTopBlobs() const
Returns the exact number of top blobs required by the layer, or -1 if no exact number is required...
Definition: recurrent_layer.hpp:47
bool expose_hidden_
Whether the layer&#39;s hidden state at the first and last timesteps are layer inputs and outputs...
Definition: recurrent_layer.hpp:175
LayerParameter layer_param_
Definition: layer.hpp:297
virtual int MaxBottomBlobs() const
Returns the maximum number of bottom blobs required by the layer, or -1 if no maximum number is requi...
Definition: recurrent_layer.hpp:46
int N_
The number of independent streams to process simultaneously.
Definition: recurrent_layer.hpp:154
virtual void FillUnrolledNet(NetParameter *net_param) const =0
Fills net_param with the recurrent network architecture. Subclasses should define this – see RNNLaye...
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24