MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MultinomialLogisticLossLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
9namespace MyCaffe.layers
10{
32 {
46 : base(cuda, log, p)
47 {
48 m_type = LayerParameter.LayerType.MULTINOMIALLOGISTIC_LOSS;
49 }
50
56 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
57 {
58 base.Reshape(colBottom, colTop);
59
60 m_log.CHECK_EQ(1, colBottom[1].channels, "The bottom[1] should have 1 channel.");
61 m_log.CHECK_EQ(1, colBottom[1].height, "The bottom[1] should have height = 1.");
62 m_log.CHECK_EQ(1, colBottom[1].width, "The bottom[1] should have width = 1.");
63 }
64
86 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
87 {
88 int nNum = colBottom[0].num;
89 int nDim = colBottom[0].count() / nNum;
90 double dfLoss = 0;
91
92 if (typeof(T) == typeof(double))
93 {
94 double[] rgBottomData = (double[])Convert.ChangeType(colBottom[0].update_cpu_data(), typeof(double[]));
95 double[] rgBottomLabel = (double[])Convert.ChangeType(colBottom[1].update_cpu_data(), typeof(double[]));
96
97 for (int i = 0; i < nNum; i++)
98 {
99 int nLabel = (int)rgBottomLabel[i];
100
101 double dfProb = Math.Max(rgBottomData[i * nDim + nLabel], kLOG_THRESHOLD);
102 dfLoss -= Math.Log(dfProb);
103 }
104 }
105 else
106 {
107 float[] rgBottomData = (float[])Convert.ChangeType(colBottom[0].update_cpu_data(), typeof(float[]));
108 float[] rgBottomLabel = (float[])Convert.ChangeType(colBottom[1].update_cpu_data(), typeof(float[]));
109
110 for (int i = 0; i < nNum; i++)
111 {
112 int nLabel = (int)rgBottomLabel[i];
113
114 double dfProb = Math.Max(rgBottomData[i * nDim + nLabel], (float)kLOG_THRESHOLD);
115 dfLoss -= Math.Log(dfProb);
116 }
117 }
118
119 colTop[0].SetData(dfLoss / nNum, 0);
120 }
121
153 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
154 {
155 if (rgbPropagateDown[1])
156 m_log.FAIL(type.ToString() + " Layer cannot backpropagate to label inputs.");
157
158 if (rgbPropagateDown[0])
159 {
160 int nNum = colBottom[0].num;
161 int nDim = colBottom[0].count() / nNum;
162 double dfScale = -1 * convertD(colTop[0].GetDiff(0)) / nNum;
163
164 colBottom[0].SetDiff(0);
165
166 if (typeof(T) == typeof(double))
167 {
168 double[] rgBottomData = (double[])Convert.ChangeType(colBottom[0].update_cpu_data(), typeof(double[]));
169 double[] rgBottomLabel = (double[])Convert.ChangeType(colBottom[1].update_cpu_data(), typeof(double[]));
170 double[] rgBottomDiff = (double[])Convert.ChangeType(colBottom[0].mutable_cpu_diff, typeof(double[]));
171
172 for (int i = 0; i < nNum; i++)
173 {
174 int nLabel = (int)rgBottomLabel[i];
175
176 double dfProb = Math.Max(rgBottomData[i * nDim + nLabel], kLOG_THRESHOLD);
177 rgBottomDiff[i * nDim + nLabel] = dfScale / dfProb;
178 }
179
180 colBottom[0].mutable_cpu_data = (T[])Convert.ChangeType(rgBottomDiff, typeof(T[]));
181 }
182 else
183 {
184 float[] rgBottomData = (float[])Convert.ChangeType(colBottom[0].update_cpu_data(), typeof(float[]));
185 float[] rgBottomLabel = (float[])Convert.ChangeType(colBottom[1].update_cpu_data(), typeof(float[]));
186 float[] rgBottomDiff = (float[])Convert.ChangeType(colBottom[0].mutable_cpu_diff, typeof(float[]));
187
188 for (int i = 0; i < nNum; i++)
189 {
190 int nLabel = (int)rgBottomLabel[i];
191
192 double dfProb = Math.Max(rgBottomData[i * nDim + nLabel], kLOG_THRESHOLD);
193 rgBottomDiff[i * nDim + nLabel] = (float)(dfScale / dfProb);
194 }
195
196 colBottom[0].mutable_cpu_data = (T[])Convert.ChangeType(rgBottomDiff, typeof(T[]));
197 }
198 }
199 }
200 }
201}
The Log class provides general output in text form.
Definition: Log.cs:13
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
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.
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
LayerParameter.LayerType type
Returns the LayerType of this Layer.
Definition: Layer.cs:927
double convertD(T df)
Converts a generic to a double value.
Definition: Layer.cs:1349
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
const double kLOG_THRESHOLD
Specifies the minimum threshold for loss values.
Definition: LossLayer.cs:27
The MultinomialLogicistLossLayer computes the multinomial logistc loss for a one-of-many classificati...
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the infogain 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 void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward computation.
MultinomialLogisticLossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
Constructor.
Specifies the base parameter for all layers.
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
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
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
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