lstm_layer.hpp
#ifndef CAFFE_LSTM_LAYER_HPP_
#define CAFFE_LSTM_LAYER_HPP_
#include <string>
#include <utility>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/layers/recurrent_layer.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
template <typename Dtype> class RecurrentLayer;
template <typename Dtype>
class LSTMLayer : public RecurrentLayer<Dtype> {
public:
explicit LSTMLayer(const LayerParameter& param)
: RecurrentLayer<Dtype>(param) {}
virtual inline const char* type() const { return "LSTM"; }
protected:
virtual void FillUnrolledNet(NetParameter* net_param) const;
virtual void RecurrentInputBlobNames(vector<string>* names) const;
virtual void RecurrentOutputBlobNames(vector<string>* names) const;
virtual void RecurrentInputShapes(vector<BlobShape>* shapes) const;
virtual void OutputBlobNames(vector<string>* names) const;
};
template <typename Dtype>
class LSTMUnitLayer : public Layer<Dtype> {
public:
explicit LSTMUnitLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline const char* type() const { return "LSTMUnit"; }
virtual inline int ExactNumBottomBlobs() const { return 3; }
virtual inline int ExactNumTopBlobs() const { return 2; }
virtual inline bool AllowForceBackward(const int bottom_index) const {
// Can't propagate to sequence continuation indicators.
return bottom_index != 2;
}
protected:
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);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
int hidden_dim_;
Blob<Dtype> X_acts_;
};
} // namespace caffe
#endif // CAFFE_LSTM_LAYER_HPP_
caffe::LSTMLayer
Processes sequential inputs using a "Long Short-Term Memory" (LSTM) [1] style recurrent neural networ...
Definition: lstm_layer.hpp:48
caffe::RecurrentLayer
An abstract class for implementing recurrent behavior inside of an unrolled network....
Definition: recurrent_layer.hpp:26
caffe::LSTMLayer::OutputBlobNames
virtual void OutputBlobNames(vector< string > *names) const
Fills names with the names of the output blobs, concatenated across all timesteps....
Definition: lstm_layer.cpp:41
caffe::LSTMUnitLayer::Backward_cpu
virtual void Backward_cpu(const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)
Computes the error gradient w.r.t. the LSTMUnit inputs.
Definition: lstm_unit_layer.cpp:72
caffe::LSTMUnitLayer::ExactNumTopBlobs
virtual int ExactNumTopBlobs() const
Returns the exact number of top blobs required by the layer, or -1 if no exact number is required.
Definition: lstm_layer.hpp:78
caffe::LSTMLayer::RecurrentInputShapes
virtual void RecurrentInputShapes(vector< BlobShape > *shapes) const
Fills shapes with the shapes of the recurrent input Blob&s. Subclasses should define this – see RNNLa...
Definition: lstm_layer.cpp:28
caffe::LSTMUnitLayer::Forward_gpu
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.
caffe::LSTMLayer::RecurrentOutputBlobNames
virtual void RecurrentOutputBlobNames(vector< string > *names) const
Fills names with the names of the Tth timestep recurrent output Blob&s. Subclasses should define this...
Definition: lstm_layer.cpp:21
caffe::Blob
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers,...
Definition: blob.hpp:24
caffe::LSTMUnitLayer::Forward_cpu
virtual void Forward_cpu(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Definition: lstm_unit_layer.cpp:41
caffe::LSTMUnitLayer::Backward_gpu
virtual void Backward_gpu(const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)
Using the GPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
caffe::Layer
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
caffe::LSTMLayer::FillUnrolledNet
virtual void FillUnrolledNet(NetParameter *net_param) const
Fills net_param with the recurrent network architecture. Subclasses should define this – see RNNLayer...
Definition: lstm_layer.cpp:47
caffe::LSTMUnitLayer
A helper for LSTMLayer: computes a single timestep of the non-linearity of the LSTM,...
Definition: lstm_layer.hpp:69
caffe::LSTMLayer::RecurrentInputBlobNames
virtual void RecurrentInputBlobNames(vector< string > *names) const
Fills names with the names of the 0th timestep recurrent input Blob&s. Subclasses should define this ...
Definition: lstm_layer.cpp:14
caffe::LSTMUnitLayer::ExactNumBottomBlobs
virtual int ExactNumBottomBlobs() const
Returns the exact number of bottom blobs required by the layer, or -1 if no exact number is required.
Definition: lstm_layer.hpp:77
caffe::LSTMUnitLayer::type
virtual const char * type() const
Returns the layer type.
Definition: lstm_layer.hpp:76
caffe::LSTMUnitLayer::AllowForceBackward
virtual bool AllowForceBackward(const int bottom_index) const
Return whether to allow force_backward for a given bottom blob index.
Definition: lstm_layer.hpp:80
caffe::LSTMUnitLayer::hidden_dim_
int hidden_dim_
The hidden and output dimension.
Definition: lstm_layer.hpp:148
caffe
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
caffe::LSTMUnitLayer::Reshape
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: lstm_unit_layer.cpp:21