OpenCV  3.2.0
Open Source Computer Vision
Public Member Functions | Static Public Member Functions | List of all members
cv::dnn::LSTMLayer Class Referenceabstract

LSTM recurrent layer. More...

#include "all_layers.hpp"

Inheritance diagram for cv::dnn::LSTMLayer:
cv::dnn::Layer

Public Member Functions

void forward (std::vector< Blob *> &input, std::vector< Blob > &output)
 
virtual Blob getC () const =0
 Returns current \( c_{t-1} \) value (deep copy). More...
 
virtual Blob getH () const =0
 Returns current \( h_{t-1} \) value (deep copy). More...
 
int inputNameToIndex (String inputName)
 Returns index of input blob into the input array. More...
 
int outputNameToIndex (String outputName)
 Returns index of output blob in output array. More...
 
virtual void setC (const Blob &C)=0
 Set \( c_{t-1} \) value that will be used in next forward() calls. More...
 
virtual void setH (const Blob &H)=0
 Set \( h_{t-1} \) value that will be used in next forward() calls. More...
 
virtual void setOutShape (const BlobShape &outTailShape=BlobShape::empty())=0
 Specifies shape of output blob which will be [[T], N] + outTailShape. More...
 
virtual void setProduceCellOutput (bool produce=false)=0
 If this flag is set to true then layer will produce \( c_t \) as second output. More...
 
virtual void setUseTimstampsDim (bool use=true)=0
 Specifies either interpet first dimension of input blob as timestamp dimenion either as sample. More...
 
virtual void setWeights (const Blob &Wh, const Blob &Wx, const Blob &b)=0
 
- Public Member Functions inherited from cv::dnn::Layer
 Layer ()
 
 Layer (const LayerParams &params)
 Initializes only name, type and blobs fields. More...
 
virtual ~Layer ()
 
virtual void allocate (const std::vector< Blob *> &input, std::vector< Blob > &output)=0
 Allocates internal buffers and output blobs with respect to the shape of inputs. More...
 
void allocate (const std::vector< Blob > &inputs, std::vector< Blob > &outputs)
 
std::vector< Bloballocate (const std::vector< Blob > &inputs)
 
void forward (const std::vector< Blob > &inputs, std::vector< Blob > &outputs)
 
void run (const std::vector< Blob > &inputs, std::vector< Blob > &outputs)
 Allocates layer and computes output. More...
 
void setParamsFrom (const LayerParams &params)
 Initializes only name, type and blobs fields. More...
 

Static Public Member Functions

static Ptr< LSTMLayercreate ()
 

Additional Inherited Members

- Public Attributes inherited from cv::dnn::Layer
std::vector< Blobblobs
 List of learned parameters must be stored here to allow read them by using Net::getParam(). More...
 
String name
 Name of the layer instance, can be used for logging or other internal purposes. More...
 
String type
 Type name which was used for creating layer by layer factory. More...
 

Detailed Description

LSTM recurrent layer.

Member Function Documentation

§ create()

static Ptr<LSTMLayer> cv::dnn::LSTMLayer::create ( )
static

Creates instance of LSTM layer

§ forward()

void cv::dnn::LSTMLayer::forward ( std::vector< Blob *> &  input,
std::vector< Blob > &  output 
)
virtual

In common case it use single input with \(x_t\) values to compute output(s) \(h_t\) (and \(c_t\)).

Parameters
inputshould contain packed values \(x_t\)
outputcontains computed outputs: \(h_t\) (and \(c_t\) if setProduceCellOutput() flag was set to true).

If setUseTimstampsDim() is set to true then input[0] should has at least two dimensions with the following shape: [T, N, [data dims]], where T specifies number of timpestamps, N is number of independent streams (i.e. \( x_{t_0 + t}^{stream} \) is stored inside input[0][t, stream, ...]).

If setUseTimstampsDim() is set to fase then input[0] should contain single timestamp, its shape should has form [N, [data dims]] with at least one dimension. (i.e. \( x_{t}^{stream} \) is stored inside input[0][stream, ...]).

Implements cv::dnn::Layer.

§ getC()

virtual Blob cv::dnn::LSTMLayer::getC ( ) const
pure virtual

Returns current \( c_{t-1} \) value (deep copy).

§ getH()

virtual Blob cv::dnn::LSTMLayer::getH ( ) const
pure virtual

Returns current \( h_{t-1} \) value (deep copy).

§ inputNameToIndex()

int cv::dnn::LSTMLayer::inputNameToIndex ( String  inputName)
virtual

Returns index of input blob into the input array.

Parameters
inputNamelabel of input blob

Each layer input and output can be labeled to easily identify them using "%<layer_name%>[.output_name]" notation. This method maps label of input blob to its index into input vector.

Reimplemented from cv::dnn::Layer.

§ outputNameToIndex()

int cv::dnn::LSTMLayer::outputNameToIndex ( String  outputName)
virtual

Returns index of output blob in output array.

See also
inputNameToIndex()

Reimplemented from cv::dnn::Layer.

§ setC()

virtual void cv::dnn::LSTMLayer::setC ( const Blob C)
pure virtual

Set \( c_{t-1} \) value that will be used in next forward() calls.

By-default \( c_{t-1} \) is inited by zeros and updated after each forward() call.

§ setH()

virtual void cv::dnn::LSTMLayer::setH ( const Blob H)
pure virtual

Set \( h_{t-1} \) value that will be used in next forward() calls.

By-default \( h_{t-1} \) is inited by zeros and updated after each forward() call.

§ setOutShape()

virtual void cv::dnn::LSTMLayer::setOutShape ( const BlobShape outTailShape = BlobShape::empty())
pure virtual

Specifies shape of output blob which will be [[T], N] + outTailShape.

If this parameter is empty or unset then outTailShape = [Wh.size(0)] will be used, where Wh is parameter from setWeights().

§ setProduceCellOutput()

virtual void cv::dnn::LSTMLayer::setProduceCellOutput ( bool  produce = false)
pure virtual

If this flag is set to true then layer will produce \( c_t \) as second output.

Shape of the second output is the same as first output.

§ setUseTimstampsDim()

virtual void cv::dnn::LSTMLayer::setUseTimstampsDim ( bool  use = true)
pure virtual

Specifies either interpet first dimension of input blob as timestamp dimenion either as sample.

If flag is set to true then shape of input blob will be interpeted as [T, N, [data dims]] where T specifies number of timpestamps, N is number of independent streams. In this case each forward() call will iterate through T timestamps and update layer's state T times.

If flag is set to false then shape of input blob will be interpeted as [N, [data dims]]. In this case each forward() call will make one iteration and produce one timestamp with shape [N, [out dims]].

§ setWeights()

virtual void cv::dnn::LSTMLayer::setWeights ( const Blob Wh,
const Blob Wx,
const Blob b 
)
pure virtual

Set trained weights for LSTM layer. LSTM behavior on each step is defined by current input, previous output, previous cell state and learned weights.

Let \(x_t\) be current input, \(h_t\) be current output, \(c_t\) be current state. Than current output and current cell state is computed as follows:

\begin{eqnarray*} h_t &= o_t \odot tanh(c_t), \\ c_t &= f_t \odot c_{t-1} + i_t \odot g_t, \\ \end{eqnarray*}

where \(\odot\) is per-element multiply operation and \(i_t, f_t, o_t, g_t\) is internal gates that are computed using learned wights.

Gates are computed as follows:

\begin{eqnarray*} i_t &= sigmoid&(W_{xi} x_t + W_{hi} h_{t-1} + b_i), \\ f_t &= sigmoid&(W_{xf} x_t + W_{hf} h_{t-1} + b_f), \\ o_t &= sigmoid&(W_{xo} x_t + W_{ho} h_{t-1} + b_o), \\ g_t &= tanh &(W_{xg} x_t + W_{hg} h_{t-1} + b_g), \\ \end{eqnarray*}

where \(W_{x?}\), \(W_{h?}\) and \(b_{?}\) are learned weights represented as matrices: \(W_{x?} \in R^{N_h \times N_x}\), \(W_{h?} \in R^{N_h \times N_h}\), \(b_? \in R^{N_h}\).

For simplicity and performance purposes we use \( W_x = [W_{xi}; W_{xf}; W_{xo}, W_{xg}] \) (i.e. \(W_x\) is vertical contacentaion of \( W_{x?} \)), \( W_x \in R^{4N_h \times N_x} \). The same for \( W_h = [W_{hi}; W_{hf}; W_{ho}, W_{hg}], W_h \in R^{4N_h \times N_h} \) and for \( b = [b_i; b_f, b_o, b_g]\), \(b \in R^{4N_h} \).

Parameters
Whis matrix defining how previous output is transformed to internal gates (i.e. according to abovemtioned notation is \( W_h \))
Wxis matrix defining how current input is transformed to internal gates (i.e. according to abovemtioned notation is \( W_x \))
bis bias vector (i.e. according to abovemtioned notation is \( b \))

The documentation for this class was generated from the following file: