2using System.Collections.Generic;
31 List<int> m_rgScaleDims =
null;
34 long m_hBottomDesc = 0;
52 m_blobScale =
new Blob<T>(cuda, log);
63 m_cuda.FreeCuDNN(m_hCudnn);
67 if (m_hBottomDesc != 0)
69 m_cuda.FreeTensorDesc(m_hBottomDesc);
75 m_cuda.FreeTensorDesc(m_hTopDesc);
130 m_log.
WriteLine(
"WARNING: SoftmaxLayer: Caffe mode does not support the ACCURATE algorithm, the default FAST algorithm will be used instead.");
137 m_hCudnn =
m_cuda.CreateCuDNN();
138 m_hBottomDesc =
m_cuda.CreateTensorDesc();
139 m_hTopDesc =
m_cuda.CreateTensorDesc();
156 m_nOuterNum = colBottom[0].count(0, m_nSoftmaxAxis);
157 m_nInnerNum = colBottom[0].count(m_nSoftmaxAxis + 1);
164 m_rgScaleDims =
Utility.Clone<
int>(colBottom[0].shape());
165 m_rgScaleDims[m_nSoftmaxAxis] = 1;
167 m_blobScale.
Reshape(m_rgScaleDims);
172 int nN = m_nOuterNum;
173 int nK = colBottom[0].shape(m_nSoftmaxAxis);
174 int nH = m_nInnerNum;
177 if (nH == 1 && nW == 1)
180 m_cuda.SetTensorDesc(m_hBottomDesc, nN, nK, nH, nW);
181 m_cuda.SetTensorDesc(m_hTopDesc, nN, nK, nH, nW);
232 long hBottomData = colBottom[0].gpu_data;
233 long hTopData = colTop[0].mutable_gpu_data;
236 int nCount = colBottom[0].count();
237 int nChannels = colTop[0].shape(m_nSoftmaxAxis);
239 m_cuda.copy(nCount, hBottomData, hTopData);
244 m_cuda.channel_max(m_nOuterNum * m_nInnerNum, m_nOuterNum, nChannels, m_nInnerNum, hTopData, hScaleData);
248 m_cuda.channel_sub(nCount, m_nOuterNum, nChannels, m_nInnerNum, hScaleData, hTopData);
252 m_cuda.exp(nCount, hTopData, hTopData);
256 m_cuda.channel_sum(m_nOuterNum * m_nInnerNum, m_nOuterNum, nChannels, m_nInnerNum, hTopData, hScaleDiff);
261 m_cuda.log(m_nOuterNum * m_nInnerNum, hScaleDiff, hScaleDiff);
263 m_cuda.add(m_nOuterNum * m_nInnerNum, hScaleData, hScaleDiff, hScaleData);
265 m_cuda.copy(nCount, hBottomData, hTopData);
266 m_cuda.channel_sub(nCount, m_nOuterNum, nChannels, m_nInnerNum, hScaleData, hTopData);
271 m_cuda.channel_div(nCount, m_nOuterNum, nChannels, m_nInnerNum, hScaleDiff, hTopData);
290 long hTopDiff = colTop[0].gpu_diff;
291 long hTopData = colTop[0].gpu_data;
292 long hBottomDiff = colBottom[0].mutable_gpu_diff;
294 int nCount = colTop[0].count();
295 int nChannels = colTop[0].shape(m_nSoftmaxAxis);
300 m_blobScale.
Reshape(m_rgScaleDims);
304 m_cuda.exp(nCount, hTopData, hBottomDiff);
311 m_cuda.mul(nCount, hBottomDiff, m_blobScale.
gpu_data, hBottomDiff);
312 m_blobScale.
Reshape(m_rgScaleDims);
315 m_cuda.sub(nCount, hTopDiff, hBottomDiff, hBottomDiff);
319 m_cuda.copy(nCount, hTopDiff, hBottomDiff);
322 m_cuda.channel_dot(m_nOuterNum * m_nInnerNum, m_nOuterNum, nChannels, m_nInnerNum, hTopDiff, hTopData, hScaleData);
323 m_cuda.channel_sub(nCount, m_nOuterNum, nChannels, m_nInnerNum, hScaleData, hBottomDiff);
326 m_cuda.mul(nCount, hBottomDiff, hTopData, hBottomDiff);
341 long hBottomData = colBottom[0].gpu_data;
342 long hTopData = colTop[0].mutable_gpu_data;
344 m_cuda.SoftmaxForward(m_hCudnn, m_algorithm, m_mode,
m_tOne, m_hBottomDesc, hBottomData,
m_tZero, m_hTopDesc, hTopData);
361 long hTopData = colTop[0].gpu_data;
362 long hTopDiff = colTop[0].gpu_diff;
363 long hBottomData = colBottom[0].gpu_data;
364 long hBottomDiff = colBottom[0].mutable_gpu_diff;
366 m_cuda.SoftmaxBackward(m_hCudnn, m_algorithm, m_mode,
m_tOne, m_hTopDesc, hTopData, m_hTopDesc, hTopDiff,
m_tZero, m_hBottomDesc, hBottomDiff);
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
The Utility class provides general utility funtions.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
The Blob is the main holder of data that moves through the Layers of the Net.
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 Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
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.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
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.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
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 fo...
T m_tZero
Specifies a generic type equal to 0.0.
T m_tOne
Specifies a generic type equal to 1.0.
BlobCollection< T > m_colInternalBlobs
Specifies internal blobs used by the layer.
Phase m_phase
Specifies the Phase under which the Layer is run.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The SoftmaxLayer computes the softmax function. This layer is initialized with the MyCaffe....
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int MinBottomBlobs
Returns the minimum number of bottom blobs (input) Blobs: input.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer to run in either Engine.CAFFE or Engine.CUDNN mode.
void forward_cuda(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation using the Engine.CAFFE mode.
override void dispose()
Releases all GPU and host resources used by the Layer.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs using either the Engine.CAFFE or Engine....
SoftmaxLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SoftmaxLayer constructor.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: softmax
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation using either the Engine.CAFFE or Engine.CUDNN mode.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
void backward_cudnn(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs using either the Engine.CUDNN.
void forward_cudnn(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation using the Engine.CUDNN mode.
void backward_cuda(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs using either the Engine.CAFFE.
override int MaxBottomBlobs
Returns the maximum number of bottom blobs (input) Blobs: input, target (ignored)
Specifies whether to use the NVIDIA cuDnn version or Caffe version of a given forward/backward operat...
Engine engine
Specifies the Engine in use.
Engine
Defines the type of engine to use.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
LayerType
Specifies the layer type.
SOFTMAX_ALGORITHM algorithm
Specifies the softmax algorithm to use during the running and testing.
bool useCudnn()
Queries whether or not to use NVIDIA's cuDnn. Softmax uses cuDNN as the default.
SOFTMAX_ALGORITHM? algorithm_train
Optionally, specifies the softmax algorithm to use during the training phase, when null,...
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.
Phase
Defines the Phase under which to run a Net.
The MyCaffe.common namespace contains common MyCaffe classes.
DIR
Defines the direction of data flow.
SOFTMAX_MODE
Specifies the SOFTMAX mode to use.
SOFTMAX_ALGORITHM
Specifies the SOFTMAX algorithm to use.
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-...