MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
EmbedLayer.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;
9using System.Diagnostics;
10
11namespace MyCaffe.layers
12{
22 public class EmbedLayer<T> : Layer<T>
23 {
24 int m_nM;
25 int m_nK;
26 int m_nN;
27 bool m_bBiasTerm;
28 Blob<T> m_blobBiasMultiplier;
29 bool m_bWarningShown = false;
30#if DEBUG
31 Blob<T> m_blobWork;
32#endif
33
53 : base(cuda, log, p)
54 {
56 m_blobBiasMultiplier = new common.Blob<T>(cuda, log);
57 m_blobBiasMultiplier.Name = m_param.name + " biasmult";
58
59#if DEBUG
60 m_blobWork = new common.Blob<T>(cuda, log);
61 m_blobWork.Name = m_param.name + " work";
62#endif
64 }
65
67 protected override void dispose()
68 {
69 if (m_blobBiasMultiplier != null)
70 {
71 m_blobBiasMultiplier.Dispose();
72 m_blobBiasMultiplier = null;
73 }
74
75#if DEBUG
76 if (m_blobWork != null)
77 {
78 m_blobWork.Dispose();
79 m_blobWork = null;
80 }
81#endif
82
83 base.dispose();
84 }
85
87 protected override void setup_internal_blobs(BlobCollection<T> col)
88 {
89 if (col.Count > 0)
90 return;
91
92 col.Add(m_blobBiasMultiplier);
93 }
94
98 public override int MinBottomBlobs
99 {
100 get { return 1; }
101 }
102
109 public override int MaxBottomBlobs
110 {
111 get { return 2; }
112 }
113
117 public override int ExactNumTopBlobs
118 {
119 get { return 1; }
120 }
121
127 public override bool ReInitializeParameters(WEIGHT_TARGET target)
128 {
129 base.ReInitializeParameters(target);
130
131 if (target == WEIGHT_TARGET.BOTH || target == WEIGHT_TARGET.WEIGHTS)
132 {
134 weight_filler.Fill(m_colBlobs[0]);
135 }
136
137 if (m_param.embed_param.bias_term && m_colBlobs.Count > 1 && (target == WEIGHT_TARGET.BOTH || target == WEIGHT_TARGET.BIAS))
138 {
140 bias_filler.Fill(m_colBlobs[1]);
141 }
142
143 return true;
144 }
145
151 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
152 {
153 m_bWarningShown = false;
154
155 if (colBottom.Count > 1)
156 m_param.embed_param.input_dim = (uint)convertF(colBottom[1].GetData(0));
157
158 m_nN = (int)m_param.embed_param.num_output;
159 m_log.CHECK_GT(m_nN, 0, "EmbedLayer num_output must be positive.");
160
161 m_nK = (int)m_param.embed_param.input_dim;
162 m_log.CHECK_GT(m_nK, 0, "EmbedLayer input_dim must be positive.");
163
164 m_bBiasTerm = m_param.embed_param.bias_term;
165
166 if (m_colBlobs.Count > 0)
167 {
168 m_log.WriteLine("Skipping parameter initialization.");
169 }
170 else
171 {
172 m_colBlobs.Clear();
173
174 // Initialize the weights --
175 // transposed from InnerProductLayer for spacial locality.
176 List<int> rgWeightShape = new List<int>() { m_nK, m_nN };
177 Blob<T> blobWeight = new Blob<T>(m_cuda, m_log);
178 blobWeight.Name = m_param.name + " weights";
179 blobWeight.type = BLOB_TYPE.WEIGHT;
180
181 if (!shareParameter(blobWeight, rgWeightShape))
182 {
183 blobWeight.Reshape(rgWeightShape);
184
185 // fill the weights
187 weight_filler.Fill(blobWeight);
188 }
189 m_colBlobs.Add(blobWeight);
190
191
192 // If necessary, initialize and fill the bias term
193 if (m_bBiasTerm)
194 {
195 List<int> rgBiasShape = new List<int>() { m_nN };
196 Blob<T> blobBias = new Blob<T>(m_cuda, m_log);
197 blobBias.Name = m_param.name + " bias";
198 blobBias.type = BLOB_TYPE.WEIGHT;
199
200 if (!shareParameter(blobBias, rgBiasShape))
201 {
202 blobBias.Reshape(rgBiasShape);
204 bias_filler.Fill(blobBias);
205 }
206 m_colBlobs.Add(blobBias);
207 }
208 }
209
210 m_rgbParamPropagateDown = new DictionaryMap<bool>(m_colBlobs.Count, true);
211 }
212
218 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
219 {
220 // Figure out the dimensions
221 m_nM = colBottom[0].count();
222 List<int> rgTopShape = Utility.Clone<int>(colBottom[0].shape());
223
224 rgTopShape.Add(m_nN);
225 colTop[0].Reshape(rgTopShape);
226
227 // Set up the bias multiplier
228 if (m_bBiasTerm)
229 {
230 List<int> rgBiasShape = new List<int>() { m_nM };
231 shareLayerBlob(m_blobBiasMultiplier, rgBiasShape);
232 m_blobBiasMultiplier.Reshape(rgBiasShape);
233 m_blobBiasMultiplier.SetData(1.0);
234 }
235 }
236
242 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
243 {
244 long hBottomData = colBottom[0].gpu_data;
245 long hTopData = colTop[0].mutable_gpu_data;
246 long hWeight = m_colBlobs[0].gpu_data;
247 int nCount = colTop[0].count();
248
249#if DEBUG
250 Tuple<double, double, double, double> minmax = colBottom[0].minmax_data(m_blobWork, true);
251 double dfMin = minmax.Item1;
252 double dfMax = minmax.Item2;
253 if (dfMin < 0 || dfMax >= m_nK)
254 throw new Exception("A data element within '" + colBottom[0].Name + "' is out of range [0," + m_nK.ToString() + ") non inclusive. Data Min = " + dfMin.ToString() + " Max = " + dfMax.ToString() + ".");
255#endif
256
257 m_cuda.embed_fwd(nCount, hBottomData, hWeight, m_nM, m_nN, m_nK, hTopData);
258
259 if (m_bBiasTerm)
260 m_cuda.gemm(false, false, m_nM, m_nN, 1, 1.0, m_blobBiasMultiplier.gpu_data, m_colBlobs[1].gpu_data, 1.0, hTopData);
261 }
262
269 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
270 {
271 if (rgbPropagateDown[0] && !m_bWarningShown)
272 {
273 m_log.WriteLine("WARNING: Can't backpropagate to EmbedLayer input.");
274 m_bWarningShown = true;
275 }
276
278 {
279 int nTopCount = colTop[0].count();
280 long hTopDiff = colTop[0].gpu_diff;
281 long hBottomData = colBottom[0].gpu_data;
282 long hWeightDiff = m_colBlobs[0].mutable_gpu_diff;
283 m_cuda.embed_bwd(nTopCount, hBottomData, hTopDiff, m_nM, m_nN, m_nK, hWeightDiff);
284 }
285
286 if (m_bBiasTerm && m_rgbParamPropagateDown[1])
287 {
288 long hTopDiff = colTop[0].gpu_diff;
289 long hBiasDiff = m_colBlobs[1].mutable_gpu_diff;
290 m_cuda.gemv(true, m_nM, m_nN, 1.0, hTopDiff, m_blobBiasMultiplier.gpu_data, 1.0, hBiasDiff);
291 }
292 }
293 }
294}
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
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
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
Definition: Blob.cs:64
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
BLOB_TYPE type
Returns the BLOB_TYPE of the Blob.
Definition: Blob.cs:2761
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
Abstract Filler class used to fill blobs with values.
Definition: Filler.cs:19
void Fill(Blob< T > b)
Fill the blob with values based on the actual filler used.
Definition: Filler.cs:50
static Filler< T > Create(CudaDnn< T > cuda, Log log, FillerParameter p)
Create a new Filler instance.
Definition: Filler.cs:79
The EmbedLayer is a layer for learning 'embeddings' of one-hot vector input. This layer is initialize...
Definition: EmbedLayer.cs:23
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: EmbedLayer.cs:67
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The Forward computation.
Definition: EmbedLayer.cs:242
override int MaxBottomBlobs
Returns the exact number of required bottom (intput) Blobs: input, input_dim.
Definition: EmbedLayer.cs:110
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: EmbedLayer.cs:151
override int MinBottomBlobs
Returns the exact number of required bottom (intput) Blobs: input.
Definition: EmbedLayer.cs:99
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
Definition: EmbedLayer.cs:87
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: EmbedLayer.cs:218
EmbedLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The EmbedLayer constructor
Definition: EmbedLayer.cs:52
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: embed
Definition: EmbedLayer.cs:118
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the input.
Definition: EmbedLayer.cs:269
override bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
Definition: EmbedLayer.cs:127
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
bool shareLayerBlob(Blob< T > b, List< int > rgMinShape)
Attempts to share a Layer Blob if another parameter Blob with the same name and acceptable size is fo...
Definition: Layer.cs:1170
bool shareParameter(Blob< T > b, List< int > rgMinShape, bool bAllowEndsWithComparison=false)
Attempts to share a parameter Blob if another parameter Blob with the same name and accpetable size i...
Definition: Layer.cs:1152
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
BlobCollection< T > m_colInternalBlobs
Specifies internal blobs used by the layer.
Definition: Layer.cs:59
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
BlobCollection< T > m_colBlobs
Specifies the learnable parameter Blobs of the Layer.
Definition: Layer.cs:55
DictionaryMap< bool > m_rgbParamPropagateDown
Specifies whether or not to compute the learnable diff of each parameter Blob.
Definition: Layer.cs:63
uint num_output
Specifies the number of outputs for the layer.
FillerParameter bias_filler
Specifies the filler for the bias.
FillerParameter weight_filler
Specifies the filler for the weights.
uint input_dim
Specifies the input given as integers to be interpreted as one-hot vector indices with dimension num_...
bool bias_term
Specifies whether to use a bias term or not.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
EmbedParameter embed_param
Returns the parameter set when initialized with LayerType.EMBED
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
BLOB_TYPE
Defines the tpe of data held by a given Blob.
Definition: Interfaces.cs:62
WEIGHT_TARGET
Defines the type of weight to target in re-initializations.
Definition: Interfaces.cs:38
The MyCaffe.fillers namespace contains all fillers including the Filler class.
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