MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
OneHotLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.fillers;
9
10namespace MyCaffe.layers.nt
11{
35 public class OneHotLayer<T> : Layer<T>
36 {
37 int m_nAxis;
38 BucketCollection m_colBuckets;
39 float[] m_rgOneHotVector;
40 float[] m_rgTop = null;
41
58 : base(cuda, log, p)
59 {
61 }
62
64 protected override void dispose()
65 {
66 base.dispose();
67 }
68
72 public override int ExactNumBottomBlobs
73 {
74 get { return 1; }
75 }
76
80 public override int ExactNumTopBlobs
81 {
82 get { return 1; }
83 }
84
90 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
91 {
92 m_rgOneHotVector = new float[m_param.onehot_param.num_output];
93 m_colBuckets = new BucketCollection(m_param.onehot_param.min, m_param.onehot_param.max, (int)m_param.onehot_param.num_output);
94 m_nAxis = colBottom[0].CanonicalAxisIndex(m_param.onehot_param.axis);
95 }
96
102 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
103 {
104 int nCount = colBottom[0].count(m_nAxis);
105 m_log.CHECK_EQ(nCount, 1, "The bottom[0] count at axis " + m_nAxis.ToString() + " must equal 1");
106
107 List<int> rgTopShape = Utility.Clone<int>(colBottom[0].shape());
108 rgTopShape[m_nAxis] = m_colBuckets.Count;
109
110 while (rgTopShape.Count < m_param.onehot_param.min_axes)
111 {
112 rgTopShape.Add(1);
113 }
114
115 colTop[0].Reshape(rgTopShape);
116
117 int nTopCount = colTop[0].count();
118 if (m_rgTop == null || m_rgTop.Length < nTopCount)
119 m_rgTop = new float[nTopCount];
120 }
121
127 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
128 {
129 float[] rgBottom = convertF(colBottom[0].mutable_cpu_data);
130 int nCount = colBottom[0].count(0, m_nAxis);
131
132 for (int i = 0; i < nCount; i++)
133 {
134 int nIdx = m_colBuckets.Add(rgBottom[i]);
135
136 for (int j = 0; j < m_rgOneHotVector.Length; j++)
137 {
138 if (j == nIdx)
139 m_rgOneHotVector[j] = 1.0f;
140 else
141 m_rgOneHotVector[j] = 0;
142 }
143
144 Array.Copy(m_rgOneHotVector, 0, m_rgTop, i * m_rgOneHotVector.Length, m_rgOneHotVector.Length);
145 }
146
147 colTop[0].mutable_cpu_data = convert(m_rgTop);
148 }
149
156 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
157 {
158 int nItemCount = colTop[0].count(m_nAxis);
159 m_log.CHECK_EQ(nItemCount, m_colBuckets.Count, "The count at the top[axis] is incorrect!");
160
161 int nCount1 = colTop[0].count(0, m_nAxis);
162 int nCount2 = colBottom[0].count(0, m_nAxis);
163 m_log.CHECK_EQ(nCount1, nCount2, "The top and bottom have incompatible sizes.");
164
165 // Convert top one-hot vectors to softmax indexes.
166 float[] rgBottomDiff = convertF(colBottom[0].mutable_cpu_diff);
167 float[] rgTopData = convertF(colTop[0].mutable_cpu_data);
168 float[] rgTopDiff = convertF(colTop[0].mutable_cpu_diff);
169
170 for (int i = 0; i < nCount1; i++)
171 {
172 int nItemIdx = i * nItemCount;
173 float fDiff = 0;
174 float fDiffSum = 0;
175
176 for (int j = 0; j < nItemCount; j++)
177 {
178 fDiff = rgTopDiff[nItemIdx + j];
179
180 if (rgTopData[nItemIdx + j] == 0)
181 fDiff *= -1;
182
183 fDiffSum += fDiff;
184 }
185
186 rgBottomDiff[i] = fDiffSum / nItemCount;
187 }
188
189 colBottom[0].mutable_cpu_diff = convert(rgBottomDiff);
190 }
191 }
192}
The BucketCollection contains a set of Buckets.
int Count
Returns the number of Buckets.
int Add(double fVal)
Finds the correct Bucket and adds the value to it.
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 Utility class provides general utility funtions.
Definition: Utility.cs:35
The BlobCollection contains a list of Blobs.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
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
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
The OneHotLayer is a layer for converting real values into a one-hot vector where a 1 is placed withi...
Definition: OneHotLayer.cs:36
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the input.
Definition: OneHotLayer.cs:156
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: OneHotLayer.cs:64
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: OneHotLayer.cs:90
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: onehot
Definition: OneHotLayer.cs:81
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: OneHotLayer.cs:102
override int ExactNumBottomBlobs
Returns the exact number of required bottom (intput) Blobs: input.
Definition: OneHotLayer.cs:73
OneHotLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The OneHotLayer constructor
Definition: OneHotLayer.cs:57
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The Forward computation.
Definition: OneHotLayer.cs:127
Specifies the base parameter for all layers.
OneHotParameter onehot_param
Returns the parameter set when initialized with LayerType.ONEHOT
LayerType
Specifies the layer type.
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.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers.nt namespace contains all Neural Transfer related layers.
Definition: LayerFactory.cs:19
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