MyCaffe  0.11.2.9-beta1
Deep learning software for Windows C# programmers.
MyCaffe.layers.SoftmaxLossLayer< T > Class Template Reference

Computes the multinomial logistic loss for a one-of-many classification task, passing real-valued predictions through a softmax to get a probability distribution over classes. More...

Inheritance diagram for MyCaffe.layers.SoftmaxLossLayer< T >:
MyCaffe.layers.LossLayer< T > MyCaffe.layers.Layer< T >

Public Member Functions

 SoftmaxLossLayer (CudaDnn< T > cuda, Log log, LayerParameter p)
 Constructor. More...
 
override void LayerSetUp (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Setup the layer. More...
 
override void Reshape (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Reshape the bottom (input) and top (output) blobs. More...
 
- Public Member Functions inherited from MyCaffe.layers.LossLayer< T >
 LossLayer (CudaDnn< T > cuda, Log log, LayerParameter p)
 The LossLayer constructor. More...
 
double GetNormalizer (LossParameter.NormalizationMode normalization_mode, int nOuterNum, int nInnerNum, int nValidCount)
 Returns the normalizer used to normalize the loss. More...
 
override bool AllowForceBackward (int nBottomIdx)
 We usually cannot backpropagate to the labels; ignore force_backward for these inputs. More...
 
- Public Member Functions inherited from MyCaffe.layers.Layer< T >
 Layer (CudaDnn< T > cuda, Log log, LayerParameter p)
 The Layer constructor. More...
 
void Dispose ()
 Releases all GPU and host resources used by the Layer. More...
 
virtual bool ReInitializeParameters (WEIGHT_TARGET target)
 Re-initialize the parameters of the layer. More...
 
void SetPhase (Phase phase)
 Changes the layer's Phase to the one specified. More...
 
void Setup (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Implements common Layer setup functionality. More...
 
virtual void SetNetParameterUsed (NetParameter np)
 This function allows other layers to gather needed information from the NetParameters if any, and is called when initialzing the Net. More...
 
void ConvertToBase (BlobCollection< T > col)
 ConvertToBase converts any blobs in a collection that are in half size to the base size. More...
 
double Forward (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss. More...
 
void Backward (BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
 Given the top Blob error gradients, compute the bottom Blob error gradients. More...
 
double loss (int nTopIdx)
 Returns the scalar loss associated with the top Blob at a given index. More...
 
void set_loss (int nTopIdx, double dfLoss)
 Sets the loss associated with a top Blob at a given index. More...
 
bool param_propagate_down (int nParamIdx)
 Returns whether or not the Layer should compute gradients w.r.t. a parameter at a particular index given by a parameter index. More...
 
void set_param_propagate_down (int nParamIdx, bool bPropagate)
 Sets whether or not the Layer should compute gradients w.r.t. a parameter at a particular index given by a parameter index. More...
 
void SetEnablePassthrough (bool bEnable)
 Enables/disables the pass-through mode. More...
 

Protected Member Functions

override void dispose ()
 Releases all GPU and host resources used by the Layer. More...
 
override void forward (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 The forward computation. More...
 
override void backward (BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
 Computes the softmax loss error gradient w.r.t the predictions. More...
 
- Protected Member Functions inherited from MyCaffe.layers.LossLayer< T >
virtual double get_normalizer (LossParameter.NormalizationMode normalization_mode, int nValidCount)
 Returns the normalizer used to normalize the loss. More...
 
- Protected Member Functions inherited from MyCaffe.layers.Layer< T >
GetIterationArgs getCurrentIteration ()
 Fires the OnGetIteration event to query the current iteration. More...
 
long convert_to_full (int nCount, long hMem)
 Convert half memory to full memory. More...
 
void convert (BlobCollection< T > col)
 Convert a collection of blobs from / to half size. More...
 
bool reshapeNeeded (BlobCollection< T > colBottom, BlobCollection< T > colTop, bool bReset=true)
 Tests the shapes of both the bottom and top blobs and if they are the same as the previous sizing, returns false indicating that no reshape is needed. More...
 
bool compareShapes (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Compare the shapes of the top and bottom and if the same, return true, otherwise false. More...
 
void CheckBlobCounts (BlobCollection< T > colBottom, BlobCollection< T > colTop)
 Called by the Layer::Setup function to check the number of bottom (input) and top (output) Blobs provided match the expected number of blobs expected via the {EactNum,Min,Max}{Bottom,Top}Blobs functions. More...
 
void SetLossWeights (BlobCollection< T > colTop)
 Called by Layer::Setup to initialize the weights associated with any top (output) Blobs in the loss function ans store non-zero loss weights in the diff Blob. More...
 
bool shareParameter (Blob< T > b, List< int > rgMinShape)
 Attempts to share a parameter Blob if another parameter Blob with the same name and accpetable size is found. More...
 
bool shareLayerBlob (Blob< T > b, List< int > rgMinShape)
 Attempts to share a Layer Blob if another parameter Blob with the same name and acceptable size is found. More...
 
bool shareLayerBlobs (Layer< T > layer)
 Attempts to share the Layer blobs and internal_blobs with matching names and sizes with those in another matching layer. More...
 
virtual WorkspaceArgs getWorkspace ()
 Returns the WorkspaceArgs used to share a workspace between Layers. More...
 
virtual bool setWorkspace (ulong lSize)
 Sets the workspace size (in items) and returns true if set, false otherwise. More...
 
void check_nan (Blob< T > b)
 Checks a Blob for NaNs and throws an exception if found. More...
 
convert (double df)
 Converts a double to a generic. More...
 
double convertD (T df)
 Converts a generic to a double value. More...
 
float convertF (T df)
 Converts a generic to a float value. More...
 
double [] convertD (T[] rg)
 Converts an array of generic values into an array of double values. More...
 
T [] convert (double[] rg)
 Converts an array of double values into an array of generic values. More...
 
float [] convertF (T[] rg)
 Converts an array of float values into an array of generic values. More...
 
T [] convert (float[] rg)
 Converts an array of float values into an array of generic values. More...
 
int val_at (T[] rg, int nIdx)
 Returns the integer value at a given index in a generic array. More...
 
Size size_at (Blob< T > b)
 Returns the Size of a given two element Blob, such as one that stores Blob size information. More...
 

Properties

override BlobCollection< T > internal_blobs [get]
 Returns the collection of internal Blobs used by the Layer. More...
 
override int ExactNumTopBlobs [get]
 Returns the exact number of required top (output) Blobs as variable. More...
 
override int MinTopBlobs [get]
 Returns the minimum number of required top (output) Blobs: loss. More...
 
override int MaxTopBlobs [get]
 Returns the maximum number of required top (output) Blobs: loss, labels More...
 
- Properties inherited from MyCaffe.layers.LossLayer< T >
override int ExactNumBottomBlobs [get]
 Returns the exact number of required bottom (intput) Blobs: prediction, label More...
 
override int ExactNumTopBlobs [get]
 Returns the exact number of required top (output) Blobs: loss More...
 
override bool AutoTopBlobs [get]
 For convenience and backwards compatibility, insturct the Net to automatically allocate a single top Blob for LossLayers, into which they output their singleton loss, (even if the user didn't specify one in the prototxt, etc.). More...
 
- Properties inherited from MyCaffe.layers.Layer< T >
BlobCollection< T > blobs [get]
 Returns the collection of learnable parameter Blobs for the Layer. More...
 
virtual BlobCollection< T > internal_blobs [get]
 Returns the collection of internal Blobs used by the Layer. More...
 
LayerParameter layer_param [get]
 Returns the LayerParameter for this Layer. More...
 
LayerParameter.LayerType type [get]
 Returns the LayerType of this Layer. More...
 
virtual int ExactNumBottomBlobs [get]
 Returns the exact number of bottom (input) Blobs required by the Layer, or -1 if no exact number is required. More...
 
virtual int MinBottomBlobs [get]
 Returns the minimum number of bottom (input) Blobs required by the Layer, or -1 if no minimum number is required. More...
 
virtual int MaxBottomBlobs [get]
 Returns the maximum number of bottom (input) Blobs required by the Layer, or -1 if no maximum number is required. More...
 
virtual int ExactNumTopBlobs [get]
 Returns the exact number of top (output) Blobs required by the Layer, or -1 if no exact number is required. More...
 
virtual int MinTopBlobs [get]
 Returns the minimum number of top (output) Blobs required by the Layer, or -1 if no minimum number is required. More...
 
virtual int MaxTopBlobs [get]
 Returns the maximum number of top (output) Blobs required by the Layer, or -1 if no maximum number is required. More...
 
virtual bool EqualNumBottomTopBlobs [get]
 Returns true if the Layer requires and equal number of bottom (input) and top (output) Blobs. More...
 
virtual bool AutoTopBlobs [get]
 Return whether "anonymous" top (output) Blobs are created automatically by the Layer. More...
 
double forward_timing [get]
 Returns the timing of the last forward pass in milliseconds. More...
 
double forward_timing_average [get]
 Returns the average timing of the forward passes in milliseconds. More...
 
double backward_timing [get]
 Returns the timing of the last backward pass in milliseconds. More...
 
double backward_timing_average [get]
 Returns the average timing of the backward passes in milliseconds. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from MyCaffe.layers.Layer< T >
static Layer< T > Create (CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXImageDatabaseBase imgDb=null, TransferInput trxinput=null)
 Create a new Layer based on the LayerParameter. More...
 
- Public Attributes inherited from MyCaffe.layers.LossLayer< T >
const double kLOG_THRESHOLD = 1e-20
 Specifies the minimum threshold for loss values. More...
 
- Protected Attributes inherited from MyCaffe.layers.LossLayer< T >
bool m_bIgnoreLabels = false
 Set to true when labels are to be ignored. More...
 
LossParameter.NormalizationMode m_normalization = LossParameter.NormalizationMode.NONE
 Specifies the normalization mode used to normalize the loss. More...
 
int m_nOuterNum = 0
 Specifies the outer num, such as the batch count (e.g. count(0, axis)). Each derivative class must set this value appropriately. More...
 
int m_nInnerNum = 0
 Specifies the inner num, such as the channel + height + width (e.g. count(axis + 1)). Each derivative class must set this value appropriately. More...
 
- Protected Attributes inherited from MyCaffe.layers.Layer< T >
LayerParameter.LayerType m_type = LayerParameter.LayerType._MAX
 Specifies the Layer type. More...
 
CudaDnn< T > m_cuda
 Specifies the CudaDnn connection to Cuda. More...
 
Log m_log
 Specifies the Log for output. More...
 
LayerParameter m_param
 Specifies the LayerParameter describing the Layer. More...
 
Phase m_phase
 Specifies the Phase under which the Layer is run. More...
 
BlobCollection< T > m_colBlobs
 Specifies the learnable parameter Blobs of the Layer. More...
 
DictionaryMap< bool > m_rgbParamPropagateDown
 Specifies whether or not to compute the learnable diff of each parameter Blob. More...
 
DictionaryMap< double > m_rgLoss
 Specifies the loss values that indeicate whether each top (output) Blob has a non-zero weight in the objective function.. More...
 
m_tOne
 Specifies a generic type equal to 1.0. More...
 
m_tZero
 Specifies a generic type equal to 0.0. More...
 
bool m_bEnablePassthrough = false
 Enables/disables the pass-through mode for the layer. Default = false. More...
 
bool m_bUseHalfSize = false
 Specifies that the half size of the top (if any) should be converted to the base size. More...
 
bool m_bConvertTopOnFwd = false
 Specifies whether or not the layer should convert the top on the forward pass when using half sized memory (typically only done with input data). More...
 
bool m_bConvertTopOnBwd = true
 Specifies whether or not to convert the top on the backward pass when using half sized memory (typically not done on loss layers). More...
 
bool m_bConvertBottom = true
 Specifies whether or not the layer should convert the bottom when using half sized memory. More...
 
bool m_bReshapeOnForwardNeeded = true
 Specifies whether or not the reshape on forward is needed or not. More...
 
- Events inherited from MyCaffe.layers.Layer< T >
EventHandler< WorkspaceArgsOnGetWorkspace
 Specifies the OnGetWorkspace event that fires when the getWorkspace() function is called by a layer to get a shareable workspace to conserve GPU memory. More...
 
EventHandler< WorkspaceArgsOnSetWorkspace
 Specifies the OnSetWorkspace event that fires when the setWorkspace() function is called by a layer to get a shareable workspace to conserve GPU memory. More...
 
EventHandler< GetIterationArgsOnGetIteration
 Specifies the OnGetIteration event that fires when a layer needs to get the current iteration from the solver. More...
 
EventHandler< GetWorkBlobArgs< T > > OnDebug
 Specifies the OnGetWorkBlob event that is only supported when debugging to get a work blob from the primary Net holding this layer. More...
 

Detailed Description

Computes the multinomial logistic loss for a one-of-many classification task, passing real-valued predictions through a softmax to get a probability distribution over classes.

This layer should be preferred over separate SofmaxLayer + MultinomialLogisticLossLayer as its gradient computation is more numerically stable. At test time, this layer can be replaced simply by a SofmaxLayer.

See also
Large-Margin Softmax Loss for Convolutional Neural Networks by Weiyang Liu, Yandong Wen, Zhiding Yu and Meng Yang, 2016.
Generalisation and Sharing in Triplet Convnets for Sketch based Visual Search by Tu Bui, Leonardo Ribeiro, Moacir Ponti, and John Collomosse, 2016.
Template Parameters
T

Definition at line 26 of file SoftmaxLossLayer.cs.

Constructor & Destructor Documentation

◆ SoftmaxLossLayer()

Constructor.

Parameters
cudaCuda engine.
logGeneral log.
pprovides LossParameter loss_param, with options:
  • ignore_label (optional) Specify a label value that whould be ignored when computing the loss.
  • normalize (optional, default true) If true, the loss is normalized by the number of (nonignored) labels present; otherwise the loss is imply summed over spatial locations.

Definition at line 47 of file SoftmaxLossLayer.cs.

Member Function Documentation

◆ backward()

override void MyCaffe.layers.SoftmaxLossLayer< T >.backward ( BlobCollection< T >  colTop,
List< bool >  rgbPropagateDown,
BlobCollection< T >  colBottom 
)
protectedvirtual

Computes the softmax loss error gradient w.r.t the predictions.

Gradients cannot be computed with respect to the label inputs (bottom[1]), so this method ignores bottom[1] and requires !propagate_down[1], crashing if propagate_down[1] == true.

Parameters
colToptop output blob vector (length 1), providing the error gradient with respect to the outputs.
  1. $ (1 \times 1 \times 1 \times 1) $ This blob's diff will simply contain the loss_weight * $ \lambda $ as $ \lambda $ is the coefficient of this layer's output $ \ell_i $ in the overall Net loss. $ E = \lambda_i \ell_i + \mbox{other loss terms}; hence \frac{partial E}{\partial \ell_i} = \lambda_i $ (*Assuming that this top blob is not used as a bottom (input) by any other layer of the Net.)
rgbPropagateDownsee Layer::Backward. propagate_down[1] must be false as we can't compute gradients with respect to the labels.
colBottombottom input blob vector (length 2)
  1. $ (N \times C \times H \times W) $ the predictions $ x $; backward computes diff $ \frac{\partial E}{\partial x} $
  2. $ (N \times 1 \times 1 \times 1) $ the labels – ignored as we can't compute their error gradients.

Implements MyCaffe.layers.Layer< T >.

Definition at line 238 of file SoftmaxLossLayer.cs.

◆ dispose()

override void MyCaffe.layers.SoftmaxLossLayer< T >.dispose ( )
protectedvirtual

Releases all GPU and host resources used by the Layer.

Reimplemented from MyCaffe.layers.Layer< T >.

Definition at line 56 of file SoftmaxLossLayer.cs.

◆ forward()

override void MyCaffe.layers.SoftmaxLossLayer< T >.forward ( BlobCollection< T >  colBottom,
BlobCollection< T >  colTop 
)
protectedvirtual

The forward computation.

Parameters
colBottombottom input blob vector (length 2)
  1. $ (N \times C \times H \times W) $ the predictions $ x $, a blob with values in $ [-\infty, +\infty] $ indicating the predicted score for eachy of the K = CHW classes. This layer maps these scores to a probability distribution over classes using the softmax function $ \hat{p}_{nk} = \exp(x_{nk}) / \left[\sum_{k'} \exp(x_{nk'})\right] $ (see SoftmaxLayer).
  2. $ (N \times 1 \times 1 \times 1) $ the labels l, an integer valued blob with values $ l_n \in [0, 1, 2, ..., K-1] $ indicating the correct class label among the K classes.
colToptop output blob vector (length 1) the computed cross_entropy classification loss: $ E = \frac{-1}{N} \sum\limits_{n=1}^N \log(\hat{p}_{n,l_n}) $ for softmax output class probabilities $ \hat{p} $.

Implements MyCaffe.layers.Layer< T >.

Definition at line 170 of file SoftmaxLossLayer.cs.

◆ LayerSetUp()

override void MyCaffe.layers.SoftmaxLossLayer< T >.LayerSetUp ( BlobCollection< T >  colBottom,
BlobCollection< T >  colTop 
)
virtual

Setup the layer.

Parameters
colBottomSpecifies the collection of bottom (input) Blobs.
colTopSpecifies the collection of top (output) Blobs.

Reimplemented from MyCaffe.layers.LossLayer< T >.

Definition at line 106 of file SoftmaxLossLayer.cs.

◆ Reshape()

override void MyCaffe.layers.SoftmaxLossLayer< T >.Reshape ( BlobCollection< T >  colBottom,
BlobCollection< T >  colTop 
)
virtual

Reshape the bottom (input) and top (output) blobs.

Parameters
colBottomSpecifies the collection of bottom (input) Blobs.
colTopSpecifies the collection of top (output) Blobs.

Reimplemented from MyCaffe.layers.LossLayer< T >.

Definition at line 131 of file SoftmaxLossLayer.cs.

Property Documentation

◆ ExactNumTopBlobs

override int MyCaffe.layers.SoftmaxLossLayer< T >.ExactNumTopBlobs
get

Returns the exact number of required top (output) Blobs as variable.

Definition at line 81 of file SoftmaxLossLayer.cs.

◆ internal_blobs

override BlobCollection<T> MyCaffe.layers.SoftmaxLossLayer< T >.internal_blobs
get

Returns the collection of internal Blobs used by the Layer.

Definition at line 68 of file SoftmaxLossLayer.cs.

◆ MaxTopBlobs

override int MyCaffe.layers.SoftmaxLossLayer< T >.MaxTopBlobs
get

Returns the maximum number of required top (output) Blobs: loss, labels

Definition at line 97 of file SoftmaxLossLayer.cs.

◆ MinTopBlobs

override int MyCaffe.layers.SoftmaxLossLayer< T >.MinTopBlobs
get

Returns the minimum number of required top (output) Blobs: loss.

Definition at line 89 of file SoftmaxLossLayer.cs.


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