2using System.Collections.Generic;
29 bool m_bWarningShown =
false;
56 m_blobBiasMultiplier =
new common.
Blob<T>(cuda, log);
60 m_blobWork =
new common.
Blob<T>(cuda, log);
69 if (m_blobBiasMultiplier !=
null)
72 m_blobBiasMultiplier =
null;
76 if (m_blobWork !=
null)
92 col.
Add(m_blobBiasMultiplier);
129 base.ReInitializeParameters(target);
153 m_bWarningShown =
false;
155 if (colBottom.
Count > 1)
159 m_log.
CHECK_GT(m_nN, 0,
"EmbedLayer num_output must be positive.");
162 m_log.
CHECK_GT(m_nK, 0,
"EmbedLayer input_dim must be positive.");
176 List<int> rgWeightShape =
new List<int>() { m_nK, m_nN };
183 blobWeight.
Reshape(rgWeightShape);
187 weight_filler.
Fill(blobWeight);
195 List<int> rgBiasShape =
new List<int>() { m_nN };
204 bias_filler.
Fill(blobBias);
221 m_nM = colBottom[0].count();
222 List<int> rgTopShape =
Utility.Clone<
int>(colBottom[0].shape());
224 rgTopShape.Add(m_nN);
230 List<int> rgBiasShape =
new List<int>() { m_nM };
232 m_blobBiasMultiplier.
Reshape(rgBiasShape);
233 m_blobBiasMultiplier.
SetData(1.0);
244 long hBottomData = colBottom[0].gpu_data;
245 long hTopData = colTop[0].mutable_gpu_data;
247 int nCount = colTop[0].count();
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() +
".");
257 m_cuda.embed_fwd(nCount, hBottomData, hWeight, m_nM, m_nN, m_nK, hTopData);
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);
271 if (rgbPropagateDown[0] && !m_bWarningShown)
273 m_log.
WriteLine(
"WARNING: Can't backpropagate to EmbedLayer input.");
274 m_bWarningShown =
true;
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);
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);
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
The Utility class provides general utility funtions.
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.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
BLOB_TYPE type
Returns the BLOB_TYPE of the Blob.
string Name
Get/set the name of the Blob.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Abstract Filler class used to fill blobs with values.
void Fill(Blob< T > b)
Fill the blob with values based on the actual filler used.
static Filler< T > Create(CudaDnn< T > cuda, Log log, FillerParameter p)
Create a new Filler instance.
The EmbedLayer is a layer for learning 'embeddings' of one-hot vector input. This layer is initialize...
override void dispose()
Releases all GPU and host resources used by the Layer.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The Forward computation.
override int MaxBottomBlobs
Returns the exact number of required bottom (intput) Blobs: input, input_dim.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override int MinBottomBlobs
Returns the exact number of required bottom (intput) Blobs: input.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
EmbedLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The EmbedLayer constructor
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: embed
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the input.
override bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
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...
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...
float convertF(T df)
Converts a generic to a float value.
BlobCollection< T > m_colInternalBlobs
Specifies internal blobs used by the layer.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
BlobCollection< T > m_colBlobs
Specifies the learnable parameter Blobs of the Layer.
DictionaryMap< bool > m_rgbParamPropagateDown
Specifies whether or not to compute the learnable diff of each parameter Blob.
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.
The MyCaffe.common namespace contains common MyCaffe classes.
BLOB_TYPE
Defines the tpe of data held by a given Blob.
WEIGHT_TARGET
Defines the type of weight to target in re-initializations.
The MyCaffe.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
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-...