Apache Singa
A General Distributed Deep Learning Library
Public Member Functions | Protected Attributes | List of all members
singa::Layer Class Reference

The base layer class. More...

#include <layer.h>

Collaboration diagram for singa::Layer:
Collaboration graph
[legend]

Public Member Functions

void Setup (const Shape &in_shape, const string &proto_str)
 Set meta data fields from a string representing a proto message. More...
 
void Setup (const vector< Shape > &in_shapes, const string &proto_str)
 'in_shapes' is the shape of the input feature for one sample
 
virtual ~Layer ()
 Destruct objects created by this layer.
 
virtual const std::string layer_type () const
 Each layer sub-class would optionaly have a type name. More...
 
virtual void Setup (const Shape &in_sample, const LayerConf &conf)
 Set meta data fields configured in 'conf' (a proto message). More...
 
virtual void Setup (const vector< Shape > &in_samples, const LayerConf &conf)
 Used for layers that have multiple input tensors, e.g., concatenate layer.
 
virtual const Shape GetOutputSampleShape () const
 Return the shape of the generated Tensor without the batchsize dimension.
 
virtual const Shape GetOutputSampleShape (int k)
 Return the shape of the k-th generated tensor without the batchsize dimension. More...
 
virtual const Tensor Forward (int flag, const Tensor &input)
 Do feature transformation for the given 'input' tensor (denoted as x). More...
 
virtual const vector< TensorForward (int flag, const vector< Tensor > &inputs)
 Do feature transformation for the given 'input' tensor (denoted as x). More...
 
virtual const std::pair< Tensor, vector< Tensor > > Backward (int flag, const Tensor &grad)
 Compute gradients of this layer. More...
 
virtual const std::pair< vector< Tensor >, vector< Tensor > > Backward (int flag, const vector< Tensor > &grads)
 
virtual void ToDevice (std::shared_ptr< Device > device)
 Clone the layer to the given device. More...
 
virtual void AsType (DataType dtype)
 Set the data type of Tensor in this layer.
 
virtual void ToProto (LayerConf *conf) const
 Serialize the layer info (including params) into a LayerConf proto message.
 
std::string ToProtoStr () const
 Serialize the layer info, including params_, into a string representing a LayerParameter message. More...
 
const vector< ParamSpec > param_specs ()
 Return specs/configuration of all parameter instances of this layer. More...
 
const ParamSpec & param_specs (size_t i)
 Return the i-th ParamSpec.
 
virtual const vector< Tensorparam_values ()
 Return pointers to parameter Tensor s.
 
const vector< string > param_names ()
 Return names of all parmaeters.
 
const string & param_name (size_t i)
 Return the 'i'-th parameter name.
 
const std::string name () const
 Each layer instance would optionally have a name. More...
 

Protected Attributes

std::string name_
 
vector< ParamSpec > param_specs_
 

Detailed Description

The base layer class.

Generally, a layer conducts feature transformation against a set of Tensor to generate a set of Tensor. Each layer may have some parameters.

Member Function Documentation

◆ Backward() [1/2]

virtual const std::pair<Tensor, vector<Tensor> > singa::Layer::Backward ( int  flag,
const Tensor grad 
)
inlinevirtual

Compute gradients of this layer.

Specifically, there are two types of gradients:

  1. gradient of the preceding layer, i.e., dx.
  2. gradients of parameters of this layer, e.g., dw for weight matrix. 1 is an empty tensor if there is no preceding layer or there is no need to compute dx (e.g., x is from a data layer); 2 is an empty vector if this 'flag' is either kTrain or kEval for feed-forward nets, and would be used for other phases when training other nets. 'grad' is a Tensor for gradient (dy) from the upper layer.

◆ Backward() [2/2]

virtual const std::pair<vector<Tensor>, vector<Tensor> > singa::Layer::Backward ( int  flag,
const vector< Tensor > &  grads 
)
inlinevirtual

◆ Forward() [1/2]

virtual const Tensor singa::Layer::Forward ( int  flag,
const Tensor input 
)
inlinevirtual

Do feature transformation for the given 'input' tensor (denoted as x).

'flag' is either kTrain or kEval for feed-forward nets, and would be used for other phases of training other nets. For example, when training RBM, we may create an alias of this function as ComputeFeature where flag could be kPositive and kNegative. It will return a Tensor (denoted as y). If the 'input' or 'output' is required for computing the gradients in Backward(), then buffer them as internal data.

◆ Forward() [2/2]

virtual const vector<Tensor> singa::Layer::Forward ( int  flag,
const vector< Tensor > &  inputs 
)
inlinevirtual

Do feature transformation for the given 'input' tensor (denoted as x).

'flag' is either kTrain or kEval for feed-forward nets, and would be used for other phases of training other nets. For example, when training RBM, we may create an alias of this function as ComputeFeature where flag could be kPositive and kNegative. It will return a Tensor (denoted as y). If the 'input' or 'output' is required for computing the gradients in Backward(), then buffer them as internal data. Accept multiple input tensors and generate multiple output tensors. If there is only one input tensor, it will call Forward(int, const Tensor&) by default. Users can override this function for layers who generate more than one outputs.

◆ GetOutputSampleShape()

virtual const Shape singa::Layer::GetOutputSampleShape ( int  k)
inlinevirtual

Return the shape of the k-th generated tensor without the batchsize dimension.

Used for layers that generate multiple tensors.

◆ layer_type()

virtual const std::string singa::Layer::layer_type ( ) const
inlinevirtual

Each layer sub-class would optionaly have a type name.

Used for debugging and logging.

◆ name()

const std::string singa::Layer::name ( ) const
inline

Each layer instance would optionally have a name.

Used for debugging and logging.

◆ param_specs()

const vector<ParamSpec> singa::Layer::param_specs ( )
inline

Return specs/configuration of all parameter instances of this layer.

ParamSpec.

◆ Setup() [1/2]

void singa::Layer::Setup ( const Shape &  in_shape,
const string &  proto_str 
)
inline

Set meta data fields from a string representing a proto message.

'in_shape' is the shape of the input feature for one sample

◆ Setup() [2/2]

virtual void singa::Layer::Setup ( const Shape &  in_sample,
const LayerConf &  conf 
)
inlinevirtual

Set meta data fields configured in 'conf' (a proto message).

Some layers would use input tensor shapes for setting its parameter shapes (e.g, desen layer and convolution layer). 'in_shape' provides such shape info. It represents the shape of the Tensor (with a single sample) from the last layer. After calling Setup, the shape info of parameters should be accssed correctly. Internal buffer/fields are set assuming batchsize is 1.

◆ ToDevice()

virtual void singa::Layer::ToDevice ( std::shared_ptr< Device device)
inlinevirtual

Clone the layer to the given device.

Layer data (e.g., parameters) are deep copied. If 'device' is nullptr, then clone it one the current device. Move the layer (including its parameters and other internal Tensor) onto the given device

◆ ToProtoStr()

std::string singa::Layer::ToProtoStr ( ) const
inline

Serialize the layer info, including params_, into a string representing a LayerParameter message.


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