recurrent_layer.hpp
#ifndef CAFFE_RECURRENT_LAYER_HPP_
#define CAFFE_RECURRENT_LAYER_HPP_
#include <string>
#include <utility>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/format.hpp"
namespace caffe {
template <typename Dtype> class RecurrentLayer;
template <typename Dtype>
class RecurrentLayer : public Layer<Dtype> {
public:
explicit RecurrentLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reset();
virtual inline const char* type() const { return "Recurrent"; }
virtual inline int MinBottomBlobs() const {
int min_bottoms = 2;
if (this->layer_param_.recurrent_param().expose_hidden()) {
vector<string> inputs;
this->RecurrentInputBlobNames(&inputs);
min_bottoms += inputs.size();
}
return min_bottoms;
}
virtual inline int MaxBottomBlobs() const { return MinBottomBlobs() + 1; }
virtual inline int ExactNumTopBlobs() const {
int num_tops = 1;
if (this->layer_param_.recurrent_param().expose_hidden()) {
vector<string> outputs;
this->RecurrentOutputBlobNames(&outputs);
num_tops += outputs.size();
}
return num_tops;
}
virtual inline bool AllowForceBackward(const int bottom_index) const {
// Can't propagate to sequence continuation indicators.
return bottom_index != 1;
}
protected:
virtual void FillUnrolledNet(NetParameter* net_param) const = 0;
virtual void RecurrentInputBlobNames(vector<string>* names) const = 0;
virtual void RecurrentInputShapes(vector<BlobShape>* shapes) const = 0;
virtual void RecurrentOutputBlobNames(vector<string>* names) const = 0;
virtual void OutputBlobNames(vector<string>* names) const = 0;
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
shared_ptr<Net<Dtype> > unrolled_net_;
int N_;
int T_;
bool static_input_;
int last_layer_index_;
bool expose_hidden_;
vector<Blob<Dtype>* > recur_input_blobs_;
vector<Blob<Dtype>* > recur_output_blobs_;
vector<Blob<Dtype>* > output_blobs_;
Blob<Dtype>* x_input_blob_;
Blob<Dtype>* x_static_input_blob_;
Blob<Dtype>* cont_input_blob_;
};
} // namespace caffe
#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