2using System.Collections.Generic;
29 double m_dfNormalizer = 0;
43 m_blobSoftmaxOutput =
new Blob<T>(cuda, log);
45 m_blobLoss =
new Blob<T>(cuda, log);
57 m_softmaxLayer.Dispose();
60 if (m_blobTarget !=
null)
97 base.LayerSetUp(colBottom, colTop);
100 m_colSoftmaxBottomVec.
Add(colBottom[0]);
102 m_colSoftmaxTopVec.
Add(m_blobSoftmaxOutput);
103 m_softmaxLayer.Setup(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
113 base.Reshape(colBottom, colTop);
118 if (colBottom[0].count() != colBottom[1].count())
120 m_log.
CHECK_EQ(colBottom[0].count(0, nAxis), colBottom[1].count(0, nAxis),
"SOFTMAX_CROSS_ENTROPY_LOSS layer inputs must have the same count, or the target must have 'num' items of indexes.");
123 if (m_blobTarget ==
null)
126 m_blobTarget.
Name =
"full_label";
132 m_softmaxLayer.Reshape(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
160 if (m_blobTarget !=
null)
163 m_log.
CHECK_EQ(colBottom[0].count(0, nAxis), colBottom[1].count(0, nAxis),
"SOFTMAX_CROSS_ENTROPY_LOSS layer inputs must have the same count, or the target must have 'num' items of indexes.");
166 float[] rgfTarget =
convertF(colBottom[1].mutable_cpu_data);
167 for (
int i = 0; i < colBottom[1].num; i++)
169 int nTargetIdx = (int)rgfTarget[i];
175 m_colSoftmaxBottomVec[0] = colBottom[0];
176 m_softmaxLayer.Forward(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
179 int nCount = colBottom[0].count();
182 long hInputData = colBottom[0].gpu_data;
183 long hTarget = (m_blobTarget !=
null) ? m_blobTarget.
gpu_data : colBottom[1].gpu_data;
187 long hLossData = colBottom[0].mutable_gpu_diff;
188 long hCountData = (m_blobTarget !=
null) ? m_blobTarget.
mutable_gpu_diff : colBottom[1].mutable_gpu_diff;
190 m_cuda.sigmoid_cross_entropy_fwd(nCount, hInputData, hTarget, hLossData,
false, -1, hCountData);
192 double dfValidCount = nCount;
193 double dfLoss =
m_cuda.asum_double(nCount, hLossData);
196 colTop[0].
SetData(dfLoss / m_dfNormalizer, 0);
199 if (colTop.
Count == 2)
202 colTop[1].ShareData(m_blobLoss);
209 if (m_blobTarget !=
null)
247 if (rgbPropagateDown[1])
250 if (rgbPropagateDown[0])
253 int nCount = colBottom[0].count();
254 long hSoftmaxOutputData = m_blobSoftmaxOutput.
gpu_data;
255 long hTarget = (m_blobTarget !=
null) ? m_blobTarget.
gpu_data : colBottom[1].gpu_data;
256 long hBottomDiff = colBottom[0].mutable_gpu_diff;
258 m_cuda.copy(nCount, hSoftmaxOutputData, hBottomDiff);
262 double dfLossWeight =
convertD(colTop[0].GetDiff(0)) / m_dfNormalizer;
263 m_cuda.scal(nCount, dfLossWeight, hBottomDiff);
The Log class provides general output in text form.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
void SetData(double df)
Set all blob data to the value specified.
void SetDiff(double df)
Set all blob diff to the value specified.
int Count
Returns the number of items in the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
float convertF(T df)
Converts a generic to a float value.
double convertD(T df)
Converts a generic to a double value.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The LossLayer provides an interface for Layer's that take two blobs as input – usually (1) prediction...
int m_nOuterNum
Specifies the outer num, such as the batch count (e.g. count(0, axis)). Each derivative class must se...
int m_nInnerNum
Specifies the inner num, such as the channel + height + width (e.g. count(axis + 1))....
virtual double get_normalizer(LossParameter.NormalizationMode normalization_mode, int nValidCount)
Returns the normalizer used to normalize the loss.
LossParameter.NormalizationMode m_normalization
Specifies the normalization mode used to normalize the loss.
The SoftmaxCrossEntropyLossLayer computes the cross-entropy (logisitic) loss and is often used for pr...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override void dispose()
Releases all GPU and host resources used by the Layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the softmax cross-entropy loss error gradient w.r.t. the predictions.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: loss.
SoftmaxCrossEntropyLossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SoftmaxCrossEntropyLayer constructor.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs as variable.
override int MaxTopBlobs
Returns the maximum number of required top (output) Blobs: loss, loss values
The SoftmaxLayer computes the softmax function. This layer is initialized with the MyCaffe....
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
virtual LayerParameter Clone(bool bCloneBlobs)
Creates a new copy of this instance of the parameter.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...