caffe::Layer< Dtype > Class Template Referenceabstract

An interface for the units of computation which can be composed into a Net. More...

`#include <layer.hpp>`

Inheritance diagram for caffe::Layer< Dtype >:

## Public Member Functions

| Layer (const LayerParameter &param)
void | SetUp (const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
| Implements common layer setup functionality. More...
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. More...
virtual void | Reshape (const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)=0
| Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs. More...
Dtype | Forward (const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
| Given the bottom blobs, compute the top blobs and the loss. More...
void | Backward (const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)
| Given the top blob error gradients, compute the bottom blob error gradients. More...
vector< shared_ptr< Blob< Dtype > > > & | blobs ()
| Returns the vector of learnable parameter blobs.
const LayerParameter & | layer_param () const
| Returns the layer parameter.
virtual void | ToProto (LayerParameter *param, bool write_diff=false)
| Writes the layer parameter to a protocol buffer.
Dtype | loss (const int top_index) const
| Returns the scalar loss associated with a top blob at a given index.
void | set_loss (const int top_index, const Dtype value)
| Sets the loss associated with a top blob at a given index.
virtual const char * | type () const
| Returns the layer type.
virtual int | ExactNumBottomBlobs () const
| Returns the exact number of bottom blobs required by the layer, or -1 if no exact number is required. More...
virtual int | MinBottomBlobs () const
| Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is required. More...
virtual int | MaxBottomBlobs () const
| Returns the maximum number of bottom blobs required by the layer, or -1 if no maximum number is required. More...
virtual int | ExactNumTopBlobs () const
| Returns the exact number of top blobs required by the layer, or -1 if no exact number is required. More...
virtual int | MinTopBlobs () const
| Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required. More...
virtual int | MaxTopBlobs () const
| Returns the maximum number of top blobs required by the layer, or -1 if no maximum number is required. More...
virtual bool | EqualNumBottomTopBlobs () const
| Returns true if the layer requires an equal number of bottom and top blobs. More...
virtual bool | AutoTopBlobs () const
| Return whether "anonymous" top blobs are created automatically by the layer. More...
virtual bool | AllowForceBackward (const int bottom_index) const
| Return whether to allow force_backward for a given bottom blob index. More...
bool | param_propagate_down (const int param_id)
| Specifies whether the layer should compute gradients w.r.t. a parameter at a particular index given by param_id. More...
void | set_param_propagate_down (const int param_id, const bool value)
| Sets whether the layer should compute gradients w.r.t. a parameter at a particular index given by param_id.

## Protected Member Functions

virtual void | Forward_cpu (const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)=0
| Using the CPU device, compute the layer output.
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.
virtual void | Backward_cpu (const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)=0
| Using the CPU device, compute the gradients for any parameters and for the bottom blobs if propagate_down is true.
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_down is true. Fall back to Backward_cpu() if unavailable.
virtual void | CheckBlobCounts (const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
void | SetLossWeights (const vector< Blob< Dtype > * > &top)

## Protected Attributes

LayerParameter | layer_param_
Phase | phase_
vector< shared_ptr< Blob< Dtype > > > | blobs_
vector< bool > | param_propagate_down_
vector< Dtype > | loss_

## Detailed Description

### template<typename Dtype>
class caffe::Layer< Dtype >

An interface for the units of computation which can be composed into a Net.

Layers must implement a Forward function, in which they take their input (bottom) Blobs (if any) and compute their output Blobs (if any). They may also implement a Backward function, in which they compute the error gradients with respect to their input Blobs, given the error gradients with their output Blobs.

## Constructor & Destructor Documentation

## Layer()

template<typename Dtype >

caffe::Layer< Dtype >::Layer | ( | const LayerParameter & | param | ) |

inline explicit

You should not implement your own constructor. Any set up code should go to SetUp(), where the dimensions of the bottom blobs are provided to the layer.

## Member Function Documentation

## AllowForceBackward()

template<typename Dtype >

virtual bool caffe::Layer< Dtype >::AllowForceBackward | ( | const int | bottom_index | ) | const

inline virtual

Return whether to allow force_backward for a given bottom blob index.

If AllowForceBackward(i) == false, we will ignore the force_backward setting and backpropagate to blob i only if it needs gradient information (as is done when force_backward == false).

Reimplemented in caffe::LSTMUnitLayer< Dtype >, caffe::LossLayer< Dtype >, caffe::EuclideanLossLayer< Dtype >, and caffe::ContrastiveLossLayer< Dtype >.

## AutoTopBlobs()

template<typename Dtype >

virtual bool caffe::Layer< Dtype >::AutoTopBlobs | ( |  | ) | const

inline virtual

Return whether "anonymous" top blobs are created automatically by the layer.

If this method returns true, Net::Init will create enough "anonymous" top blobs to fulfill the requirement specified by ExactNumTopBlobs() or MinTopBlobs().

Reimplemented in caffe::LossLayer< Dtype >.

## Backward()

template<typename Dtype >

void caffe::Layer< Dtype >::Backward | ( | const vector< Blob< Dtype > * > & | top,
|  | const vector< bool > & | propagate_down,
|  | const vector< Blob< Dtype > * > & | bottom
| ) |  |

inline

Given the top blob error gradients, compute the bottom blob error gradients.

Parameters

  top | the output blobs, whose diff fields store the gradient of the error with respect to themselves
  propagate_down | a vector with equal length to bottom, with each index indicating whether to propagate the error gradients down to the bottom blob at the corresponding index
  bottom | the input blobs, whose diff fields will store the gradient of the error with respect to themselves after Backward is run

The Backward wrapper calls the relevant device wrapper function (Backward_cpu or Backward_gpu) to compute the bottom blob diffs given the top blob diffs.

Your layer should implement Backward_cpu and (optionally) Backward_gpu.

## CheckBlobCounts()

template<typename Dtype >

virtual void caffe::Layer< Dtype >::CheckBlobCounts | ( | const vector< Blob< Dtype > * > & | bottom,
|  | const vector< Blob< Dtype > * > & | top
| ) |  |

inline protected virtual

Called by the parent Layer's SetUp to check that the number of bottom and top Blobs provided as input match the expected numbers specified by the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.

## EqualNumBottomTopBlobs()

template<typename Dtype >

virtual bool caffe::Layer< Dtype >::EqualNumBottomTopBlobs | ( |  | ) | const

inline virtual

Returns true if the layer requires an equal number of bottom and top blobs.

This method should be overridden to return true if your layer expects an equal number of bottom and top blobs.

Reimplemented in caffe::BaseConvolutionLayer< Dtype >.

## ExactNumBottomBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::ExactNumBottomBlobs | ( |  | ) | const

inline virtual

Returns the exact number of bottom blobs required by the layer, or -1 if no exact number is required.

This method should be overridden to return a non-negative value if your layer expects some exact number of bottom blobs.

Reimplemented in caffe::WindowDataLayer< Dtype >, caffe::TileLayer< Dtype >, caffe::SPPLayer< Dtype >, caffe::SplitLayer< Dtype >, caffe::SoftmaxLayer< Dtype >, caffe::SliceLayer< Dtype >, caffe::ReshapeLayer< Dtype >, caffe::ReductionLayer< Dtype >, caffe::PoolingLayer< Dtype >, caffe::ParameterLayer< Dtype >, caffe::NeuronLayer< Dtype >, caffe::MVNLayer< Dtype >, caffe::MemoryDataLayer< Dtype >, caffe::LSTMUnitLayer< Dtype >, caffe::LRNLayer< Dtype >, caffe::LossLayer< Dtype >, caffe::InputLayer< Dtype >, caffe::InnerProductLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::ImageDataLayer< Dtype >, caffe::Im2colLayer< Dtype >, caffe::HDF5OutputLayer< Dtype >, caffe::HDF5DataLayer< Dtype >, caffe::FlattenLayer< Dtype >, caffe::EmbedLayer< Dtype >, caffe::DummyDataLayer< Dtype >, caffe::DataLayer< Dtype >, caffe::CropLayer< Dtype >, caffe::ContrastiveLossLayer< Dtype >, caffe::BatchReindexLayer< Dtype >, caffe::BatchNormLayer< Dtype >, caffe::ArgMaxLayer< Dtype >, caffe::AccuracyLayer< Dtype >, and caffe::AbsValLayer< Dtype >.

## ExactNumTopBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::ExactNumTopBlobs | ( |  | ) | const

inline virtual

Returns the exact number of top blobs required by the layer, or -1 if no exact number is required.

This method should be overridden to return a non-negative value if your layer expects some exact number of top blobs.

Reimplemented in caffe::WindowDataLayer< Dtype >, caffe::TileLayer< Dtype >, caffe::SPPLayer< Dtype >, caffe::SoftmaxWithLossLayer< Dtype >, caffe::SoftmaxLayer< Dtype >, caffe::SilenceLayer< Dtype >, caffe::ScaleLayer< Dtype >, caffe::ReshapeLayer< Dtype >, caffe::ReductionLayer< Dtype >, caffe::ParameterLayer< Dtype >, caffe::NeuronLayer< Dtype >, caffe::MVNLayer< Dtype >, caffe::MemoryDataLayer< Dtype >, caffe::LSTMUnitLayer< Dtype >, caffe::LRNLayer< Dtype >, caffe::LossLayer< Dtype >, caffe::InnerProductLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::ImageDataLayer< Dtype >, caffe::Im2colLayer< Dtype >, caffe::HDF5OutputLayer< Dtype >, caffe::FlattenLayer< Dtype >, caffe::EmbedLayer< Dtype >, caffe::EltwiseLayer< Dtype >, caffe::CropLayer< Dtype >, caffe::ConcatLayer< Dtype >, caffe::BiasLayer< Dtype >, caffe::BatchReindexLayer< Dtype >, caffe::BatchNormLayer< Dtype >, caffe::ArgMaxLayer< Dtype >, and caffe::AbsValLayer< Dtype >.

## Forward()

template<typename Dtype >

Dtype caffe::Layer< Dtype >::Forward | ( | const vector< Blob< Dtype > * > & | bottom,
|  | const vector< Blob< Dtype > * > & | top
| ) |  |

inline

Given the bottom blobs, compute the top blobs and the loss.

Parameters

  bottom | the input blobs, whose data fields store the input data for this layer
  top | the preshaped output blobs, whose data fields will store this layers' outputs

Returns
  The total loss from the layer.

The Forward wrapper calls the relevant device wrapper function (Forward_cpu or Forward_gpu) to compute the top blob values given the bottom blobs. If the layer has any non-zero loss_weights, the wrapper then computes and returns the loss.

Your layer should implement Forward_cpu and (optionally) Forward_gpu.

## LayerSetUp()

template<typename Dtype >

virtual void caffe::Layer< Dtype >::LayerSetUp | ( | const vector< Blob< Dtype > * > & | bottom,
|  | const vector< Blob< Dtype > * > & | top
| ) |  |

inline virtual

Does layer-specific setup: your layer should implement this function as well as Reshape.

Parameters

  bottom | the preshaped input blobs, whose data fields store the input data for this layer
  top | the allocated but unshaped output blobs

This method should do one-time layer specific setup. This includes reading and processing relevent parameters from the `layer_param_`. Setting up the shapes of top blobs and internal buffers should be done in `Reshape`, which will be called before the forward pass to adjust the top blob sizes.

Reimplemented in caffe::ThresholdLayer< Dtype >, caffe::SwishLayer< Dtype >, caffe::SPPLayer< Dtype >, caffe::SoftmaxWithLossLayer< Dtype >, caffe::SliceLayer< Dtype >, caffe::SigmoidCrossEntropyLossLayer< Dtype >, caffe::ScaleLayer< Dtype >, caffe::ReshapeLayer< Dtype >, caffe::ReductionLayer< Dtype >, caffe::PythonLayer< Dtype >, caffe::PReLULayer< Dtype >, caffe::PowerLayer< Dtype >, caffe::PoolingLayer< Dtype >, caffe::ParameterLayer< Dtype >, caffe::LRNLayer< Dtype >, caffe::LossLayer< Dtype >, caffe::LogLayer< Dtype >, caffe::InputLayer< Dtype >, caffe::InnerProductLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::Im2colLayer< Dtype >, caffe::HDF5OutputLayer< Dtype >, caffe::HDF5DataLayer< Dtype >, caffe::FilterLayer< Dtype >, caffe::ExpLayer< Dtype >, caffe::EmbedLayer< Dtype >, caffe::EltwiseLayer< Dtype >, caffe::DummyDataLayer< Dtype >, caffe::DropoutLayer< Dtype >, caffe::CropLayer< Dtype >, caffe::ContrastiveLossLayer< Dtype >, caffe::ConcatLayer< Dtype >, caffe::BiasLayer< Dtype >, caffe::BatchNormLayer< Dtype >, caffe::BasePrefetchingDataLayer< Dtype >, caffe::BaseDataLayer< Dtype >, caffe::BaseConvolutionLayer< Dtype >, caffe::ArgMaxLayer< Dtype >, caffe::AccuracyLayer< Dtype >, and caffe::AbsValLayer< Dtype >.

## MaxBottomBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::MaxBottomBlobs | ( |  | ) | const

inline virtual

Returns the maximum number of bottom blobs required by the layer, or -1 if no maximum number is required.

This method should be overridden to return a non-negative value if your layer expects some maximum number of bottom blobs.

Reimplemented in caffe::ScaleLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, and caffe::BiasLayer< Dtype >.

## MaxTopBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::MaxTopBlobs | ( |  | ) | const

inline virtual

Returns the maximum number of top blobs required by the layer, or -1 if no maximum number is required.

This method should be overridden to return a non-negative value if your layer expects some maximum number of top blobs.

Reimplemented in caffe::SoftmaxWithLossLayer< Dtype >, caffe::PoolingLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::DataLayer< Dtype >, and caffe::AccuracyLayer< Dtype >.

## MinBottomBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::MinBottomBlobs | ( |  | ) | const

inline virtual

Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is required.

This method should be overridden to return a non-negative value if your layer expects some minimum number of bottom blobs.

Reimplemented in caffe::SilenceLayer< Dtype >, caffe::ScaleLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::FilterLayer< Dtype >, caffe::EltwiseLayer< Dtype >, caffe::ConcatLayer< Dtype >, caffe::BiasLayer< Dtype >, and caffe::BaseConvolutionLayer< Dtype >.

## MinTopBlobs()

template<typename Dtype >

virtual int caffe::Layer< Dtype >::MinTopBlobs | ( |  | ) | const

inline virtual

Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required.

This method should be overridden to return a non-negative value if your layer expects some minimum number of top blobs.

Reimplemented in caffe::SplitLayer< Dtype >, caffe::SoftmaxWithLossLayer< Dtype >, caffe::SliceLayer< Dtype >, caffe::PoolingLayer< Dtype >, caffe::InputLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::HDF5DataLayer< Dtype >, caffe::FilterLayer< Dtype >, caffe::DummyDataLayer< Dtype >, caffe::DataLayer< Dtype >, caffe::BaseConvolutionLayer< Dtype >, and caffe::AccuracyLayer< Dtype >.

## param_propagate_down()

template<typename Dtype >

bool caffe::Layer< Dtype >::param_propagate_down | ( | const int | param_id | ) |

inline

Specifies whether the layer should compute gradients w.r.t. a parameter at a particular index given by param_id.

You can safely ignore false values and always compute gradients for all parameters, but possibly with wasteful computation.

## Reshape()

template<typename Dtype >

virtual void caffe::Layer< Dtype >::Reshape | ( | const vector< Blob< Dtype > * > & | bottom,
|  | const vector< Blob< Dtype > * > & | top
| ) |  |

pure virtual

Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs.

Parameters

  bottom | the input blobs, with the requested input shapes
  top | the top blobs, which should be reshaped as needed

This method should reshape top blobs as needed according to the shapes of the bottom (input) blobs, as well as reshaping any internal buffers and making any other necessary adjustments so that the layer can accommodate the bottom blobs.

Implemented in caffe::TileLayer< Dtype >, caffe::SwishLayer< Dtype >, caffe::SPPLayer< Dtype >, caffe::SplitLayer< Dtype >, caffe::SoftmaxWithLossLayer< Dtype >, caffe::SoftmaxLayer< Dtype >, caffe::SliceLayer< Dtype >, caffe::SilenceLayer< Dtype >, caffe::SigmoidCrossEntropyLossLayer< Dtype >, caffe::ScaleLayer< Dtype >, caffe::ReshapeLayer< Dtype >, caffe::ReductionLayer< Dtype >, caffe::PythonLayer< Dtype >, caffe::PReLULayer< Dtype >, caffe::PoolingLayer< Dtype >, caffe::ParameterLayer< Dtype >, caffe::NeuronLayer< Dtype >, caffe::MVNLayer< Dtype >, caffe::MultinomialLogisticLossLayer< Dtype >, caffe::LSTMUnitLayer< Dtype >, caffe::LRNLayer< Dtype >, caffe::LossLayer< Dtype >, caffe::InputLayer< Dtype >, caffe::InnerProductLayer< Dtype >, caffe::InfogainLossLayer< Dtype >, caffe::Im2colLayer< Dtype >, caffe::HDF5OutputLayer< Dtype >, caffe::HDF5DataLayer< Dtype >, caffe::FlattenLayer< Dtype >, caffe::FilterLayer< Dtype >, caffe::EuclideanLossLayer< Dtype >, caffe::EmbedLayer< Dtype >, caffe::EltwiseLayer< Dtype >, caffe::DummyDataLayer< Dtype >, caffe::DropoutLayer< Dtype >, caffe::CropLayer< Dtype >, caffe::ConcatLayer< Dtype >, caffe::BiasLayer< Dtype >, caffe::BatchReindexLayer< Dtype >, caffe::BatchNormLayer< Dtype >, caffe::BaseDataLayer< Dtype >, caffe::BaseConvolutionLayer< Dtype >, caffe::ArgMaxLayer< Dtype >, and caffe::AccuracyLayer< Dtype >.

## SetLossWeights()

template<typename Dtype >

void caffe::Layer< Dtype >::SetLossWeights | ( | const vector< Blob< Dtype > * > & | top | ) |

inline protected

Called by SetUp to initialize the weights associated with any top blobs in the loss function. Store non-zero loss weights in the diff blob.

## SetUp()

template<typename Dtype >

void caffe::Layer< Dtype >::SetUp | ( | const vector< Blob< Dtype > * > & | bottom,
|  | const vector< Blob< Dtype > * > & | top
| ) |  |

inline

Implements common layer setup functionality.

Parameters

  bottom | the preshaped input blobs
  top | the allocated but unshaped output blobs, to be shaped by Reshape

Checks that the number of bottom and top blobs is correct. Calls LayerSetUp to do special layer setup for individual layer types, followed by Reshape to set up sizes of top blobs and internal buffers. Sets up the loss weight multiplier blobs for any non-zero loss weights. This method may not be overridden.

## Member Data Documentation

## blobs_

template<typename Dtype >

vector<shared_ptr<Blob<Dtype> > > caffe::Layer< Dtype >::blobs_

protected

The vector that stores the learnable parameters as a set of blobs.

## layer_param_

template<typename Dtype >

LayerParameter caffe::Layer< Dtype >::layer_param_

protected

The protobuf that stores the layer parameters

## loss_

template<typename Dtype >

vector<Dtype> caffe::Layer< Dtype >::loss_

protected

The vector that indicates whether each top blob has a non-zero weight in the objective function.

## param_propagate_down_

template<typename Dtype >

vector<bool> caffe::Layer< Dtype >::param_propagate_down_

protected

Vector indicating whether to compute the diff of each param blob.

## phase_

template<typename Dtype >

Phase caffe::Layer< Dtype >::phase_

protected

The phase: TRAIN or TEST

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

- include/caffe/layer.hpp