MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ReductionLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using MyCaffe.basecode;
7using MyCaffe.common;
8using MyCaffe.param;
9
10namespace MyCaffe.layers
11{
19 public class ReductionLayer<T> : Layer<T>
20 {
28 double m_dfCoeff;
32 int m_nAxis;
36 int m_nNum;
40 int m_nDim;
44 Blob<T> m_blobSumMultiplier;
45
60 : base(cuda, log, p)
61 {
63 m_blobSumMultiplier = new Blob<T>(cuda, log);
64 m_blobSumMultiplier.Name = m_param.name + " summult";
65 }
66
68 protected override void dispose()
69 {
70 base.dispose();
71
72 if (m_blobSumMultiplier != null)
73 {
74 m_blobSumMultiplier.Dispose();
75 m_blobSumMultiplier = null;
76 }
77 }
78
80 protected override void setup_internal_blobs(BlobCollection<T> col)
81 {
82 if (col.Count > 0)
83 return;
84
85 col.Add(m_blobSumMultiplier);
86 }
87
91 public override int ExactNumBottomBlobs
92 {
93 get { return 1; }
94 }
95
99 public override int ExactNumTopBlobs
100 {
101 get { return 1; }
102 }
103
109 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
110 {
112 }
113
119 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
120 {
121 m_nAxis = colBottom[0].CanonicalAxisIndex(m_param.reduction_param.axis);
122
123 // In the output, we'll keep all axes up to the reduction axis, but
124 // throw away all after than.
125 // Note: currently reducing along non-tail axes is not supported; otherwise,
126 // we'd need to also copy any axes followign an 'end_axis'.
127 List<int> rgTopShape = Utility.Clone<int>(colBottom[0].shape(), m_nAxis);
128 colTop[0].Reshape(rgTopShape);
129 m_nNum = colBottom[0].count(0, m_nAxis);
130 m_nDim = colBottom[0].count(m_nAxis);
131 m_log.CHECK_EQ(m_nNum, colTop[0].count(), "The 'num' should equal the top[0].count!");
132
133 if (m_op == ReductionParameter.ReductionOp.SUM ||
134 m_op == ReductionParameter.ReductionOp.MEAN)
135 {
136 List<int> rgSumMultShape = new List<int>() { m_nDim };
137 m_blobSumMultiplier.Reshape(rgSumMultShape);
138 m_blobSumMultiplier.SetData(1);
139 }
140
141 m_dfCoeff = m_param.reduction_param.coeff;
142
143 if (m_op == ReductionParameter.ReductionOp.MEAN)
144 m_dfCoeff /= m_nDim;
145 }
146
160 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
161 {
162 long hBottomData = colBottom[0].gpu_data;
163 long hMultData = 0;
164
165 if (m_blobSumMultiplier.count() > 0)
166 hMultData = m_blobSumMultiplier.gpu_data;
167
168 T[] rgTop = colTop[0].mutable_cpu_data;
169 int nOffset = 0;
170 long lPos;
171
172 for (int i = 0; i < m_nNum; i++)
173 {
174 switch (m_op)
175 {
178 rgTop[i] = m_cuda.dot(m_nDim, hMultData, hBottomData, 0, nOffset);
179 break;
180
182 rgTop[i] = m_cuda.asum(m_nDim, hBottomData, nOffset);
183 break;
184
186 rgTop[i] = m_cuda.dot(m_nDim, hBottomData, hBottomData, nOffset, nOffset);
187 break;
188
190 rgTop[i] = convert(m_cuda.min(m_nDim, hBottomData, out lPos, nOffset));
191 break;
192
194 rgTop[i] = convert(m_cuda.max(m_nDim, hBottomData, out lPos, nOffset));
195 break;
196
197 default:
198 m_log.FAIL("Unknown reduction op: " + m_op.ToString());
199 break;
200 }
201
202 nOffset += m_nDim;
203 }
204
205 colTop[0].mutable_cpu_data = rgTop;
206
207 if (m_dfCoeff != 1.0)
208 m_cuda.scal(m_nNum, m_dfCoeff, colTop[0].mutable_gpu_data);
209 }
210
227 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
228 {
229 if (!rgbPropagateDown[0])
230 return;
231
232 // Get bottom_data, if needed.
233 long hBottomData = 0;
234
235 if (m_op == ReductionParameter.ReductionOp.ASUM ||
236 m_op == ReductionParameter.ReductionOp.SUMSQ)
237 hBottomData = colBottom[0].gpu_data;
238
239 T[] rgTopDiff = colTop[0].update_cpu_diff();
240 long hBottomDiff = colBottom[0].mutable_gpu_diff;
241 int nOffset = 0;
242
243 for (int i = 0; i < m_nNum; i++)
244 {
245 double dfBottomCoeff = convertD(rgTopDiff[i]) * m_dfCoeff;
246
247 switch (m_op)
248 {
251 m_cuda.set(m_nDim, hBottomDiff, convert(dfBottomCoeff), -1, nOffset);
252 break;
253
257 m_cuda.sign(m_nDim, hBottomData, hBottomDiff, nOffset, nOffset);
258 m_cuda.scal(m_nDim, dfBottomCoeff, hBottomDiff, nOffset);
259 break;
260
262 m_cuda.scale(m_nDim, convert(2 * dfBottomCoeff), hBottomData, hBottomDiff, nOffset, nOffset);
263 break;
264
265 default:
266 m_log.FAIL("Unknown reduction op: " + m_op.ToString());
267 break;
268 }
269
270 nOffset += m_nDim;
271 }
272 }
273 }
274}
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 Utility class provides general utility funtions.
Definition: Utility.cs:35
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 Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
Definition: Blob.cs:1922
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
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 ReductionLayer computes the 'reductions' – operations that return a scalar output Blob for an inp...
ReductionLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The ReductionLayer constructor.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the Reduction inputs.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: reduction
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input
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)
Forward operation
override void dispose()
Releases all GPU and host resources used by the Layer.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
ReductionParameter reduction_param
Returns the parameter set when initialized with LayerType.REDUCTION
LayerType
Specifies the layer type.
Specifies the parameters used by ReductionLayer.
int axis
The first axis to reduce to scalar – may be negative index from the end (e.g., -1 for the last axis)....
ReductionOp
Defines the reduction operation.
double coeff
Specifies the coefficient used to scale the output.
ReductionOp operation
Specifies the reduction operation.
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