2using System.Collections.Generic;
52 Blob<T> m_blobFocusedInput =
null;
71 List<int> rgDimClip =
new List<int>() { 1, 0 };
78 ipUaParam.
name =
"ipUa";
93 ipWaParam.
name =
"ipWa";
108 addParam.
name =
"add";
114 tanhParam.
name =
"tanh";
120 ipVParam.
name =
"ipV";
134 m_blobX =
new Blob<T>(cuda, log);
137 m_blobClip =
new Blob<T>(cuda, log);
140 m_blobX1 =
new Blob<T>(cuda, log);
143 m_blobState =
new Blob<T>(cuda, log);
146 m_blobUh =
new Blob<T>(cuda, log);
149 m_blobWc =
new Blob<T>(cuda, log);
152 m_blobFullWc =
new Blob<T>(cuda, log);
155 m_blobAddOutput =
new Blob<T>(cuda, log);
158 m_blobGG =
new Blob<T>(cuda, log);
161 m_blobAA =
new Blob<T>(cuda, log);
164 m_blobScale =
new Blob<T>(cuda, log,
false);
167 m_blobSoftmax =
new Blob<T>(cuda, log);
170 m_blobFocusedInput =
new Blob<T>(cuda, log);
173 m_blobContext =
new Blob<T>(cuda, log);
176 m_blobWork =
new Blob<T>(cuda, log);
203 dispose(ref m_blobFocusedInput);
216 col.
Add(m_blobState);
219 col.
Add(m_blobFullWc);
220 col.
Add(m_blobAddOutput);
223 col.
Add(m_blobScale);
224 col.
Add(m_blobFocusedInput);
225 col.
Add(m_blobContext);
251 base.ReInitializeParameters(target);
261 m_colInternalBottom.
Clear();
262 m_colInternalBottom.
Add(bottom);
264 m_colInternalTop.
Clear();
265 m_colInternalTop.
Add(top);
270 m_colInternalBottom.
Clear();
272 for (
int i=0; i<rgBottom.Count; i++)
274 m_colInternalBottom.
Add(rgBottom[i]);
277 m_colInternalTop.
Clear();
278 m_colInternalTop.
Add(top);
290 Blob<T> blobClip = colBottom[2];
296 List<int> rgDimX =
new List<int>() { 1, 0 };
297 while (rgDimX.Count < colBottom[0].num_axes)
299 rgDimX.Add(rgDimX.Count);
307 addInternal(blobX, m_blobX);
308 m_transposeX.Setup(m_colInternalBottom, m_colInternalTop);
311 addInternal(m_blobX, m_blobUh);
312 m_ipUa.
Setup(m_colInternalBottom, m_colInternalTop);
314 addInternal(blobClip, m_blobClip);
315 m_transposeClip.
Setup(m_colInternalBottom, m_colInternalTop);
321 addInternal(m_blobState, m_blobWc);
322 m_ipWa.
Setup(m_colInternalBottom, m_colInternalTop);
326 addInternal(
new List<
Blob<T>>() { m_blobUh, m_blobFullWc }, m_blobAddOutput);
327 m_add1.
Setup(m_colInternalBottom, m_colInternalTop);
329 addInternal(m_blobAddOutput, m_blobGG);
330 m_tanh.
Setup(m_colInternalBottom, m_colInternalTop);
332 addInternal(m_blobGG, m_blobAA);
333 m_ipV.
Setup(m_colInternalBottom, m_colInternalTop);
335 List<int> rgFocusShape =
Utility.Clone<
int>(blobX.
shape());
336 rgFocusShape[0] = blobX.
shape(1);
337 rgFocusShape[1] = blobX.
shape(0);
338 m_blobFocusedInput.
Reshape(rgFocusShape);
340 List<int> rgContextShape =
Utility.Clone<
int>(blobX.
shape());
341 rgContextShape[0] = rgContextShape[1];
342 rgContextShape[1] = 1;
343 m_blobContext.
Reshape(rgContextShape);
345 List<int> rgTopShape =
Utility.Clone<
int>(m_blobContext.
shape());
346 rgTopShape[0] = m_blobContext.
shape(1);
347 rgTopShape[1] = m_blobContext.
shape(0);
378 Blob<T> blobClip = colBottom[2];
382 addInternal(blobX, m_blobX);
383 m_transposeX.Reshape(m_colInternalBottom, m_colInternalTop);
386 addInternal(m_blobX, m_blobUh);
387 m_ipUa.
Reshape(m_colInternalBottom, m_colInternalTop);
389 addInternal(blobClip, m_blobClip);
390 m_transposeClip.
Reshape(m_colInternalBottom, m_colInternalTop);
396 addInternal(m_blobState, m_blobWc);
397 m_ipWa.
Reshape(m_colInternalBottom, m_colInternalTop);
401 addInternal(
new List<
Blob<T>>() { m_blobUh, m_blobFullWc }, m_blobAddOutput);
402 m_add1.
Reshape(m_colInternalBottom, m_colInternalTop);
404 addInternal(m_blobAddOutput, m_blobGG);
405 m_tanh.
Reshape(m_colInternalBottom, m_colInternalTop);
407 addInternal(m_blobGG, m_blobAA);
408 m_ipV.
Reshape(m_colInternalBottom, m_colInternalTop);
413 List<int> rgFocusShape =
Utility.Clone<
int>(blobX.
shape());
414 rgFocusShape[0] = blobX.
shape(1);
415 rgFocusShape[1] = blobX.
shape(0);
416 m_blobFocusedInput.
Reshape(rgFocusShape);
418 List<int> rgContextShape =
Utility.Clone<
int>(blobX.
shape());
419 rgContextShape[0] = rgContextShape[1];
420 rgContextShape[1] = 1;
421 m_blobContext.
Reshape(rgContextShape);
423 List<int> rgTopShape =
Utility.Clone<
int>(m_blobContext.
shape());
424 rgTopShape[0] = m_blobContext.
shape(1);
425 rgTopShape[1] = m_blobContext.
shape(0);
440 int nCount = blobBottom.
count();
441 int nOuterNum = blobBottom.
count(0, nAxis);
442 int nInnerNum = blobBottom.
count(nAxis + 1);
443 int nChannels = blobTop.
shape(nAxis);
444 long hBottomData = blobBottom.
gpu_data;
448 m_cuda.copy(nCount, hBottomData, hTopData);
452 if (nValidValues < nCount)
453 blobTop.
SetData(-1000000000.0, nValidValues, nCount - nValidValues);
458 m_cuda.channel_max(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, hTopData, hScaleData);
461 m_cuda.channel_sub(nCount, nOuterNum, nChannels, nInnerNum, hScaleData, hTopData);
464 m_cuda.exp(nCount, hTopData, hTopData);
467 m_cuda.channel_sum(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, hTopData, hScaleData);
470 m_cuda.channel_div(nCount, nOuterNum, nChannels, nInnerNum, hScaleData, hTopData);
478 int nOuterNum = blobBottom.
count(0, nAxis);
479 int nInnerNum = blobBottom.
count(nAxis + 1);
484 int nCount = blobTop.
count();
485 int nChannels = blobTop.
shape(nAxis);
487 m_cuda.copy(nCount, hTopDiff, hBottomDiff);
490 m_cuda.channel_dot(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, hTopDiff, hTopData, hScaleData);
491 m_cuda.channel_sub(nCount, nOuterNum, nChannels, nInnerNum, hScaleData, hBottomDiff);
494 m_cuda.channel_scale(nCount, nOuterNum, nChannels, nInnerNum, hTopData, blobClip.
gpu_data, hTopData);
497 m_cuda.mul(nCount, hBottomDiff, hTopData, hBottomDiff);
501 private float sum_diff(
int nCount,
Blob<T> b,
int nOffset)
506 for (
int i = 0; i < nCount; i++)
508 fSum += rg[nOffset + i];
514 private void fill(
Blob<T> blob1,
Blob<T> blob2,
Blob<T> blobFull,
bool bUseDiff =
false,
int nBlob1AxisOffset = 0)
517 int nM = blob1.
shape(nAxis + nBlob1AxisOffset);
518 int nN = blobFull.
shape(nAxis);
519 int nK = blob1.
count(nAxis + nBlob1AxisOffset + 1);
523 List<int> rgShape =
new List<int>();
524 rgShape.Add(blob1.
count(0, nAxis));
566 Blob<T> blobClip = colBottom[2];
571 addInternal(blobX, m_blobX);
572 m_transposeX.Forward(m_colInternalBottom, m_colInternalTop);
574 addInternal(blobClip, m_blobClip);
575 m_transposeClip.
Forward(m_colInternalBottom, m_colInternalTop);
580 apply_clip(m_blobX, blobClip, m_blobX);
585 addInternal(m_blobX, m_blobUh);
586 m_ipUa.
Forward(m_colInternalBottom, m_colInternalTop);
588 addInternal(m_blobState, m_blobWc);
589 m_ipWa.
Forward(m_colInternalBottom, m_colInternalTop);
592 fill(m_blobWc,
null, m_blobFullWc);
594 addInternal(
new List<
Blob<T>>() { m_blobUh, m_blobFullWc }, m_blobAddOutput);
595 m_add1.
Forward(m_colInternalBottom, m_colInternalTop);
597 addInternal(m_blobAddOutput, m_blobGG);
598 m_tanh.
Forward(m_colInternalBottom, m_colInternalTop);
600 addInternal(m_blobGG, m_blobAA);
601 m_ipV.
Forward(m_colInternalBottom, m_colInternalTop);
603 softmax_fwd(m_blobAA, m_blobClip, m_blobScale, m_blobSoftmax, 1);
606 m_blobFocusedInput.
CopyFrom(m_blobX);
608 int nCount = m_blobFocusedInput.
count(2);
609 int nOuterNum = m_blobFocusedInput.
count(0, 2);
631 if (rgbPropagateDown[0])
635 Blob<T> blobClip = colBottom[2];
645 List<bool> rgbPropagate =
new List<bool>() {
true,
true };
652 fill(m_blobContext, m_blobSoftmax, m_blobX1,
true, 1);
658 softmax_bwd(m_blobSoftmax, m_blobClip, m_blobScale, m_blobAA, 1);
660 addInternal(m_blobGG, m_blobAA);
661 m_ipV.
Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
663 addInternal(m_blobAddOutput, m_blobGG);
664 m_tanh.
Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
666 addInternal(
new List<
Blob<T>>() { m_blobUh, m_blobFullWc }, m_blobAddOutput);
667 m_add1.
Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
672 addInternal(m_blobState, m_blobWc);
673 m_ipWa.
Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
675 addInternal(m_blobX, m_blobUh);
676 m_ipUa.
Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
682 addInternal(blobX, m_blobX);
683 m_transposeX.Backward(m_colInternalTop, rgbPropagate, m_colInternalBottom);
The Log class provides general output in text form.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to 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 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.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
T[] mutable_cpu_diff
Get diff from the GPU and bring it over to the host, or Set diff from the Host and send it over to th...
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
T asum_data()
Compute the sum of absolute values (L1 norm) of the data.
int count()
Returns the total number of items in the Blob.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
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.
[DEPRECIATED] The AttentionLayer provides focus for LSTM based encoder/decoder models.
AttentionLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The AttentionLayer constructor.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the loss error gradient w.r.t the outputs.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward computation.
override bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: ip
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override void dispose()
Releases all GPU and host resources used by the Layer.
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input, state (last ct),...
The EltwiseLayer computes elementwise oeprations, such as product and sum, along multiple input blobs...
The InnerProductLayer, also know as a 'fully-connected' layer, computes the inner product with a set ...
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.
void Backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Given the top Blob error gradients, compute the bottom Blob error gradients.
virtual bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
float convertF(T df)
Converts a generic to a float value.
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.
virtual bool reshapeNeeded(BlobCollection< T > colBottom, BlobCollection< T > colTop, bool bReset=true)
Tests the shapes of both the bottom and top blobs and if they are the same as the previous sizing,...
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
LayerParameter.LayerType m_type
Specifies the Layer type.
BlobCollection< T > blobs
Returns the collection of learnable parameter Blobs for the Layer.
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.
The LayerParameterEx class is used when sharing another Net to conserve GPU memory and extends the La...
BlobCollection< T > SharedBlobs
Returns the shared parameter Blobs.
Layer< T > SharedLayer
Returns the layer in the shared Net that matches this one.
BlobCollection< T > SharedLayerBlobs
Returns the shared Layer Blobs.
The TanhLayer is a neuron layer that calculates the tanh function, popular with auto-encoders....
The TransposeLayer performs a permute and transpose operation similar to numpy.transpose.
FillerParameter bias_filler
The filler for the bias.
FillerParameter weight_filler
The filler for the weights.
uint dim
Specifies the dim of the attention unit which should match the LSTM output size.
Specifies the parameters for the EltwiseLayer.
EltwiseOp
Defines the operation to perform.
EltwiseOp operation
Specifies the element-wise operation.
Specifies whether to use the NVIDIA cuDnn version or Caffe version of a given forward/backward operat...
Engine engine
Specifies the Engine in use.
Engine
Defines the type of engine to use.
FillerParameter weight_filler
The filler for the weights.
int axis
Specifies the first axis to be lumped into a single inner product computation; all preceding axes are...
FillerParameter bias_filler
The filler for the bias.
uint num_output
The number of outputs for the layer.
bool bias_term
Whether to have bias terms or not.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
EltwiseParameter eltwise_param
Returns the parameter set when initialized with LayerType.ELTWISE
InnerProductParameter inner_product_param
Returns the parameter set when initialized with LayerType.INNERPRODUCT
TransposeParameter transpose_param
Returns the parameter set when initialized with LayerType.TRANSPOSE
AttentionParameter attention_param
Returns the parameter set when initialized with LayerType.ATTENTION
LayerType
Specifies the layer type.
TanhParameter tanh_param
Returns the parameter set when initialized with LayerType.TANH
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
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.beta namespace contains all beta stage layers.
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-...