MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ChannelEmbeddingLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using MyCaffe.basecode;
7using MyCaffe.common;
8using MyCaffe.param;
9
10namespace MyCaffe.layers.tft
11{
24 public class ChannelEmbeddingLayer<T> : Layer<T>
25 {
26 Layer<T> m_numericLayer = null;
27 Layer<T> m_categoricalLayer = null;
28 BlobCollection<T> m_colBtm = new BlobCollection<T>();
29 BlobCollection<T> m_colNumericTop = new BlobCollection<T>();
30 BlobCollection<T> m_colCategoricalTop = new BlobCollection<T>();
31 List<int> m_rgShape = new List<int>(4);
32
40 : base(cuda, log, p)
41 {
42 m_type = LayerParameter.LayerType.CHANNEL_EMBEDDING;
43 }
44
46 protected override void dispose()
47 {
48 dispose(ref m_numericLayer);
49 dispose(ref m_categoricalLayer);
50
51 m_colNumericTop.Dispose();
52 m_colCategoricalTop.Dispose();
53 }
54
56 protected override void setup_internal_blobs(BlobCollection<T> col)
57 {
58 if (col.Count > 0)
59 return;
60 }
61
65 public override int MinBottomBlobs
66 {
67 get { return 1; }
68 }
69
73 public override int MaxBottomBlobs
74 {
75 get { return 2; }
76 }
77
81 public override int ExactNumTopBlobs
82 {
83 get { return 1; }
84 }
85
86 private void getBlobs(BlobCollection<T> colBottom, out Blob<T> blobNumeric, out Blob<T> blobCategorical)
87 {
88 blobNumeric = null;
89 blobCategorical = null;
90
91 m_log.CHECK_EQ(colBottom.Count, 2, "The bottom must have a count = 2.");
92
93 if (m_param.numeric_trans_param.num_input > 0)
94 {
95 m_log.CHECK_GT(colBottom[0].count(), 0, "The bottom(0) must have a count > 0!");
96 blobNumeric = colBottom[0];
97 }
98 if (m_param.categorical_trans_param.num_input > 0)
99 {
100 m_log.CHECK_GT(colBottom[1].count(), 0, "The bottom(1) must have a count > 0!");
101 blobCategorical = colBottom[1];
102 }
103
104 if (blobNumeric == null && blobCategorical == null)
105 m_log.FAIL("At least one of the numeric or categorical num_input must be > 0.");
106 }
107
113 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
114 {
115 Blob<T> blobNumeric = null;
116 Blob<T> blobCategorical = null;
117
118 m_log.CHECK_EQ(m_param.numeric_trans_param.state_size, m_param.categorical_trans_param.state_size, "The numeric and categorical parameters must have the same sate_size.");
119
120 getBlobs(colBottom, out blobNumeric, out blobCategorical);
121
122 if (blobNumeric != null)
123 {
124 m_colNumericTop.Clear();
125 for (int i=0; i<m_param.numeric_trans_param.num_input; i++)
126 {
127 Blob<T> blobTop = new Blob<T>(m_cuda, m_log);
128 m_colNumericTop.Add(blobTop);
129 }
130
131 LayerParameter p = new LayerParameter(LayerParameter.LayerType.NUMERIC_TRANS, m_param.name + ".numeric");
134
135 m_colBtm.Clear();
136 m_colBtm.Add(blobNumeric);
137 m_numericLayer.LayerSetUp(m_colBtm, m_colNumericTop);
138 blobs.Add(m_numericLayer.blobs);
139 }
140
141 if (blobCategorical != null)
142 {
143 m_colCategoricalTop.Clear();
144 for (int i = 0; i < m_param.categorical_trans_param.num_input; i++)
145 {
146 Blob<T> blobTop = new Blob<T>(m_cuda, m_log);
147 m_colCategoricalTop.Add(blobTop);
148 }
149
150 LayerParameter p = new LayerParameter(LayerParameter.LayerType.CATEGORICAL_TRANS, m_param.name + ".categorical");
153
154 m_colBtm.Clear();
155 m_colBtm.Add(blobCategorical);
156 m_categoricalLayer.LayerSetUp(m_colBtm, m_colCategoricalTop);
157 blobs.Add(m_categoricalLayer.blobs);
158 }
159 }
160
166 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
167 {
168 Blob<T> blobNumeric = null;
169 Blob<T> blobCategorical = null;
170 int nN = 0;
171 int nC = 0;
172 int? nH = null;
173
174 getBlobs(colBottom, out blobNumeric, out blobCategorical);
175
176 if (blobNumeric != null)
177 {
178 nN = colBottom[0].num;
179 m_colBtm.Clear();
180 m_colBtm.Add(blobNumeric);
181 m_numericLayer.Reshape(m_colBtm, m_colNumericTop);
182
183 if (colBottom[0].num_axes > 2)
184 {
185 nC = colBottom[0].channels;
186 nH = (int)m_param.numeric_trans_param.num_input;
187 }
188 else
189 {
190 nC += (int)m_param.numeric_trans_param.num_input;
191 }
192 }
193
194 if (blobCategorical != null)
195 {
196 if (nN != 0)
197 m_log.CHECK_EQ(colBottom[1].num, nN, "The bottom(0).num and bottom(1).num must be equal!");
198
199 nN = colBottom[1].num;
200 m_colBtm.Clear();
201 m_colBtm.Add(blobCategorical);
202 m_categoricalLayer.Reshape(m_colBtm, m_colCategoricalTop);
203
204 if (colBottom[0].num_axes > 2)
205 {
206 if (nC != 0)
207 m_log.CHECK_EQ(colBottom[1].channels, nC, "The bottom(0).channels and bottom(1).channels must be equal!");
208
209 nC = colBottom[1].channels;
210
211 if (!nH.HasValue)
212 nH = 0;
213 nH += (int)m_param.categorical_trans_param.num_input;
214 }
215 else
216 {
217 nC += (int)m_param.categorical_trans_param.num_input;
218 }
219 }
220
221 int nEmb = (int)m_param.categorical_trans_param.state_size;
222
223 m_rgShape.Clear();
224 m_rgShape.Add(nN);
225
226 if (!nH.HasValue)
227 {
228 m_rgShape.Add(nC * nEmb);
229 }
230 else
231 {
232 m_rgShape.Add(nC);
233 m_rgShape.Add(nH.Value * nEmb);
234 }
235
236 colTop[0].Reshape(m_rgShape);
237 }
238
252 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
253 {
254 Blob<T> blobNumeric = null;
255 Blob<T> blobCategorical = null;
256 int nCount = 0;
257 int nBlocks = (int)m_param.numeric_trans_param.num_input + (int)m_param.categorical_trans_param.num_input;
258 int nEmb = (int)m_param.numeric_trans_param.state_size;
259 int nIdx = 0;
260
261 getBlobs(colBottom, out blobNumeric, out blobCategorical);
262
263 if (blobNumeric != null)
264 {
265 m_colBtm.Clear();
266 m_colBtm.Add(blobNumeric);
267 m_numericLayer.Forward(m_colBtm, m_colNumericTop);
268 nCount = m_colNumericTop[0].count();
269
270 for (int i = 0; i < m_colNumericTop.Count; i++)
271 {
272 m_cuda.channel_copy(nCount, m_colNumericTop[0].num, 1, nBlocks, nEmb, nIdx, colTop[0].mutable_gpu_data, m_colNumericTop[i].gpu_data, DIR.BWD);
273 nIdx++;
274 }
275 }
276
277 if (blobCategorical != null)
278 {
279 m_colBtm.Clear();
280 m_colBtm.Add(blobCategorical);
281 m_categoricalLayer.Forward(m_colBtm, m_colCategoricalTop);
282 nCount = m_colCategoricalTop[0].count();
283
284 for (int i = 0; i < m_colCategoricalTop.Count; i++)
285 {
286 m_cuda.channel_copy(nCount, m_colCategoricalTop[0].num, 1, nBlocks, nEmb, nIdx, colTop[0].mutable_gpu_data, m_colCategoricalTop[i].gpu_data, DIR.BWD);
287 nIdx++;
288 }
289 }
290 }
291
310 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
311 {
312 Blob<T> blobNumeric = null;
313 Blob<T> blobCategorical = null;
314 int nCount;
315 int nBlocks = (int)m_param.numeric_trans_param.num_input + (int)m_param.categorical_trans_param.num_input;
316 int nEmb = (int)m_param.numeric_trans_param.state_size;
317 int nIdx = 0;
318
319 getBlobs(colBottom, out blobNumeric, out blobCategorical);
320
321 if (blobNumeric != null)
322 {
323 nCount = m_colNumericTop[0].count();
324
325 for (int i = 0; i < m_colNumericTop.Count; i++)
326 {
327 m_cuda.channel_copy(nCount, m_colNumericTop[0].num, 1, nBlocks, nEmb, nIdx, colTop[0].gpu_diff, m_colNumericTop[i].mutable_gpu_diff, DIR.FWD);
328 nIdx++;
329 }
330
331 m_colBtm.Clear();
332 m_colBtm.Add(blobNumeric);
333 m_numericLayer.Backward(m_colNumericTop, new List<bool>() { true }, m_colBtm);
334 }
335
336 if (blobCategorical != null)
337 {
338 nCount = m_colCategoricalTop[0].count();
339
340 for (int i = 0; i < m_colCategoricalTop.Count; i++)
341 {
342 m_cuda.channel_copy(nCount, m_colCategoricalTop[0].num, 1, nBlocks, nEmb, nIdx, colTop[0].gpu_diff, m_colCategoricalTop[i].mutable_gpu_diff, DIR.FWD);
343 nIdx++;
344 }
345
346 m_colBtm.Clear();
347 m_colBtm.Add(blobCategorical);
348 m_categoricalLayer.Backward(m_colCategoricalTop, new List<bool>() { true }, m_colBtm);
349 }
350 }
351 }
352}
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
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
The BlobCollection contains a list of Blobs.
void Dispose()
Release all resource used by the collection and its Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void Clear(bool bDispose=false)
Remove all items from 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
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
abstract void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Performs Layer specific setup. Derived layers should override this function as well as the Reshape fu...
void Backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Given the top Blob error gradients, compute the bottom Blob error gradients.
Definition: Layer.cs:815
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
Definition: Layer.cs:728
abstract void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Adjust the shapes of top blobs and internal buffers to accomodate the shapes of the bottom blobs.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
Definition: Layer.cs:1468
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
BlobCollection< T > blobs
Returns the collection of learnable parameter Blobs for the Layer.
Definition: Layer.cs:875
LayerParameter convertLayerParam(LayerParameter pChild, LayerParameter pParent)
Called to convert a parent LayerParameterEx, used in blob sharing, with a child layer parameter.
Definition: Layer.cs:1134
The CategoricalTransformationLayer implements the transforming/embeddings for the set of categorical ...
The ChannelEmbeddingLayer implements the transforming/embeddings for both the numeric and categorical...
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the top (output) blobs.
override int MaxBottomBlobs
Returns the max number of required bottom (input) Blobs: numeric data, categorical data
override void dispose()
Releases all GPU and host resources used by the Layer.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
ChannelEmbeddingLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The constructor.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: norm
override int MinBottomBlobs
Returns the min number of required bottom (input) Blobs: numeric data or categorical data (determined...
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the stacked embedding numeric and categorical value inputs.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
The NumericTransformationLayer implements the transforming/embeddings for the set of numeric input va...
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
CategoricalTransformationParameter categorical_trans_param
Returns the parameter set when initialized with LayerType.CATEGORICAL_TRANS
NumericTransformationParameter numeric_trans_param
Returns the parameter set when initialized with LayerType.NUMERIC_TRANS
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
DIR
Defines the direction of data flow.
Definition: CudaDnn.cs:22
The MyCaffe.layers.tft namespace contains all TFT related layers.
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