MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MeanErrorLossLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.param.beta;
9
10namespace MyCaffe.layers.beta
11{
28 public class MeanErrorLossLayer<T> : LossLayer<T>
29 {
30 int m_nAxis = 1;
31 MEAN_ERROR m_meanType = MEAN_ERROR.MAE;
32 Blob<T> m_blobWork;
33
51 : base(cuda, log, p)
52 {
53 m_type = LayerParameter.LayerType.MEAN_ERROR_LOSS;
54 m_nAxis = p.mean_error_loss_param.axis;
56
57 m_blobWork = new Blob<T>(cuda, log);
58 }
59
61 protected override void dispose()
62 {
63 if (m_blobWork != null)
64 {
65 m_blobWork.Dispose();
66 m_blobWork = null;
67 }
68
69 base.dispose();
70 }
71
75 public override int ExactNumTopBlobs
76 {
77 get { return -1; }
78 }
79
83 public override int MinTopBlobs
84 {
85 get { return 1; }
86 }
87
91 public override int MaxTopBlobs
92 {
93 get { return 1; }
94 }
95
101 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
102 {
103 base.LayerSetUp(colBottom, colTop);
104 }
105
111 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
112 {
113 base.Reshape(colBottom, colTop);
114
115 m_blobWork.ReshapeLike(colBottom[0]);
116
117 if (m_nOuterNum == 0)
118 m_nOuterNum = (int)colBottom[0].count(0, m_nAxis);
119
120 if (m_nInnerNum == 0)
121 m_nInnerNum = colBottom[0].count(m_nAxis);
122 }
123
142 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
143 {
144 long hPredicted = colBottom[0].gpu_data;
145 long hTarget = colBottom[1].gpu_data;
146 int nCount = colBottom[0].count();
147
148 double dfLoss = 0;
149
150 m_log.CHECK_EQ(nCount, colBottom[1].count(), "The bottom(0) predicted and bottom(1) target must have the same shapes!");
151
152 switch (m_meanType)
153 {
154 case MEAN_ERROR.MSE:
155 m_cuda.sub(nCount, hTarget, hPredicted, colBottom[0].mutable_gpu_diff);
156 m_cuda.powx(nCount, colBottom[0].gpu_diff, 2.0, colBottom[0].mutable_gpu_diff);
157 dfLoss = m_cuda.asum_double(nCount, colBottom[0].gpu_diff);
158 break;
159
160 case MEAN_ERROR.MAE:
161 m_cuda.sub(nCount, hTarget, hPredicted, colBottom[0].mutable_gpu_diff);
162 m_cuda.abs(nCount, colBottom[0].gpu_diff, colBottom[0].mutable_gpu_diff);
163 dfLoss = m_cuda.asum_double(nCount, colBottom[0].gpu_diff);
164 break;
165 }
166
167 double dfNormalizer = get_normalizer(m_normalization, -1);
168 colTop[0].SetData(dfLoss / dfNormalizer, 0);
169
170 // Clear scratch memory to prevent with interfering with backward pass (see #602)
171 colBottom[0].SetDiff(0);
172 }
173
212 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
213 {
214 if (!rgbPropagateDown[0])
215 return;
216
217 long hPredicted = colBottom[0].gpu_data;
218 long hTarget = colBottom[1].gpu_data;
219 long hBottomDiff = colBottom[0].mutable_gpu_diff;
220 int nCount = colBottom[0].count();
221
222 m_cuda.mean_error_loss_bwd(nCount, hPredicted, hTarget, hBottomDiff, m_meanType);
223
224 double dfTopDiff = convertD(colTop[0].GetDiff(0));
225 double dfNormalizer = get_normalizer(m_normalization, -1);
226 double dfLossWeight = dfTopDiff / dfNormalizer;
227
228 m_cuda.scal(nCount, dfLossWeight, hBottomDiff);
229
230 if (colBottom.Count > 1)
231 {
232 long hBottomDiff2 = colBottom[1].mutable_gpu_diff;
233 m_cuda.scale(nCount, -1, hBottomDiff, hBottomDiff2);
234 }
235 }
236 }
237}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
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.
Definition: Blob.cs:25
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
Definition: Blob.cs:648
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
double convertD(T df)
Converts a generic to a double value.
Definition: Layer.cs:1349
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
The LossLayer provides an interface for Layer's that take two blobs as input – usually (1) prediction...
Definition: LossLayer.cs:23
int m_nOuterNum
Specifies the outer num, such as the batch count (e.g. count(0, axis)). Each derivative class must se...
Definition: LossLayer.cs:39
int m_nInnerNum
Specifies the inner num, such as the channel + height + width (e.g. count(axis + 1))....
Definition: LossLayer.cs:43
virtual double get_normalizer(LossParameter.NormalizationMode normalization_mode, int nValidCount)
Returns the normalizer used to normalize the loss.
Definition: LossLayer.cs:92
LossParameter.NormalizationMode m_normalization
Specifies the normalization mode used to normalize the loss.
Definition: LossLayer.cs:35
The MeanErrorLossLayer computes losses based on various different Mean Error methods as shown below....
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward computation.
MeanErrorLossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
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.
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 softmax loss error gradient w.r.t the predictions.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: loss.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Specifies the base parameter for all layers.
MeanErrorLossParameter mean_error_loss_param
Returns the parameter set when initialized with LayerType.MEAN_ERROR_LOSS
LayerType
Specifies the layer type.
MEAN_ERROR mean_error_type
[optional, default = MSE] Specifies the type of mean error to use.
int axis
[optional, default = 1] Specifies the axis of the probability.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
MEAN_ERROR
Defines the type of Mean Error to use.
Definition: CudaDnn.cs:37
The MyCaffe.layers.beta namespace contains all beta stage layers.
Definition: LayerFactory.cs:9
The MyCaffe.param.beta parameters are used by the MyCaffe.layer.beta layers.
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-...
Definition: Annotation.cs:12