Caffe
All Classes Namespaces Functions Variables Typedefs
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_
caffe::RecurrentLayer::last_layer_index_
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
caffe::RecurrentLayer::unrolled_net_
shared_ptr< Net< Dtype > > unrolled_net_
A Net to implement the Recurrent functionality.
Definition: recurrent_layer.hpp:151
caffe::RecurrentLayer::RecurrentInputBlobNames
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 ...
caffe::RecurrentLayer
An abstract class for implementing recurrent behavior inside of an unrolled network....
Definition: recurrent_layer.hpp:26
caffe::RecurrentLayer::static_input_
bool static_input_
Whether the layer has a "static" input copied across all timesteps.
Definition: recurrent_layer.hpp:163
caffe::RecurrentLayer::FillUnrolledNet
virtual void FillUnrolledNet(NetParameter *net_param) const =0
Fills net_param with the recurrent network architecture. Subclasses should define this – see RNNLayer...
caffe::RecurrentLayer::OutputBlobNames
virtual void OutputBlobNames(vector< string > *names) const =0
Fills names with the names of the output blobs, concatenated across all timesteps....
caffe::Blob
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers,...
Definition: blob.hpp:24
caffe::Layer
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
caffe::RecurrentLayer::RecurrentOutputBlobNames
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...
caffe::RecurrentLayer::RecurrentInputShapes
virtual void RecurrentInputShapes(vector< BlobShape > *shapes) const =0
Fills shapes with the shapes of the recurrent input Blob&s. Subclasses should define this – see RNNLa...
caffe::RecurrentLayer::N_
int N_
The number of independent streams to process simultaneously.
Definition: recurrent_layer.hpp:154
caffe::RecurrentLayer::expose_hidden_
bool expose_hidden_
Whether the layer's hidden state at the first and last timesteps are layer inputs and outputs,...
Definition: recurrent_layer.hpp:175
caffe::RecurrentLayer::T_
int T_
The number of timesteps in the layer's input, and the number of timesteps over which to backpropagate...
Definition: recurrent_layer.hpp:160
caffe::RecurrentLayer::Forward_cpu
virtual void Forward_cpu(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Definition: recurrent_layer.cpp:246
caffe
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14