2using System.Collections.Generic;
5using System.Runtime.InteropServices;
27 int m_nTrueInFeatures;
28 int m_nReshapeCount = 0;
41 List<BlobCollection<T>> m_rgrgInternalBlobs =
new List<BlobCollection<T>>();
45 Blob<T> m_blobForwardInput =
null;
46 Blob<T> m_blobForwardInput1 =
null;
47 Blob<T> m_blobForwardInput2 =
null;
48 Blob<T> m_blobForwardOutput =
null;
49 Blob<T> m_blobForwardOutput1 =
null;
50 Blob<T> m_blobForwardOutput2 =
null;
51 Blob<T> m_blobTimeSinceUpdate =
null;
52 Blob<T> m_blobTimeSinceUpdate1 =
null;
55 Blob<T> m_blobCurrentMask =
null;
56 Blob<T> m_blobCurrentMaskFull =
null;
57 Blob<T> m_blobCurrentOutput =
null;
58 Blob<T> m_blobOutputSequence =
null;
59 int[] m_rgShape =
new int[] { 1, 1, 1, 1 };
60 bool m_bSetup =
false;
61 int m_nHiddenSize = 0;
139 for (
int i = 0; i < rg.Count; i++)
141 for (
int j = 0; j < rg[i].Count; j++)
143 if (rg[i][j] !=
null)
158 dispose(ref m_rgrgInternalBlobs);
166 dispose(ref m_blobForwardInput);
167 dispose(ref m_blobForwardInput1);
168 dispose(ref m_blobForwardInput2);
169 dispose(ref m_blobForwardOutput);
170 dispose(ref m_blobForwardOutput1);
171 dispose(ref m_blobForwardOutput2);
172 dispose(ref m_blobTimeSinceUpdate);
173 dispose(ref m_blobTimeSinceUpdate1);
176 dispose(ref m_blobCurrentMask);
177 dispose(ref m_blobCurrentMaskFull);
178 dispose(ref m_blobCurrentOutput);
179 dispose(ref m_blobOutputSequence);
224 base.ReInitializeParameters(target);
238 m_nBatchSize = colBottom[0].num;
239 m_nSeqLen = colBottom[0].channels;
240 m_nTrueInFeatures = colBottom[0].count(2);
243 m_rgShape[0] = m_nBatchSize;
244 m_rgShape[1] = m_nHiddenSize;
245 m_blobHState1.
Reshape(m_rgShape);
246 m_blobHState.
Reshape(m_rgShape);
248 for (
int i = 0; i < m_nSeqLen; i++)
251 blobHStateT.
Name =
"h_state_" + i.ToString();
252 m_rgBlobHState.
Add(blobHStateT);
255 m_rgrgInternalBlobs.
Add(col);
259 m_blobTs.Reshape(m_rgShape);
261 m_rgShape[1] = m_nTrueInFeatures;
262 m_blobInputs.
Reshape(m_rgShape);
265 m_nMaskCount = m_blobMask.
count(2);
268 m_blobCurrentMask.
Reshape(m_rgShape);
270 m_rgShape[1] = m_nTrueInFeatures;
271 m_blobForwardInput.
Reshape(m_rgShape);
272 m_blobForwardInput1.
Reshape(m_rgShape);
273 m_blobForwardInput2.
Reshape(m_rgShape);
274 m_blobTimeSinceUpdate.
Reshape(m_rgShape);
275 m_blobTimeSinceUpdate1.
Reshape(m_rgShape);
276 m_blobTsFull.
Reshape(m_rgShape);
279 m_blobForwardOutput.
Reshape(m_rgShape);
280 m_blobForwardOutput1.
Reshape(m_rgShape);
281 m_blobForwardOutput2.
Reshape(m_rgShape);
283 addBtmTop(m_blobForwardInput, m_blobInputs1);
285 m_colBtm.
Add(m_blobTimeSinceUpdate);
286 m_colBtm.
Add(m_blobMask);
287 m_cat.
Setup(m_colBtm, m_colTop);
289 ((
LnnUnitLayer<T>)m_rnn_cell).SetInternalSharedBlobs(m_rgrgInternalBlobs[0]);
291 addBtmTop(m_blobInputs1, m_blobHState);
292 m_colBtm.
Add(m_blobHState1);
293 m_colBtm.
Add(m_blobTs);
294 m_rnn_cell.
Setup(m_colBtm, m_colTop);
299 addBtmTop(m_blobHState, m_blobCurrentOutput);
300 m_fc.
Setup(m_colBtm, m_colTop);
303 m_blobCurrentMaskFull.
ReshapeLike(m_blobCurrentOutput);
305 addBtmTop(m_blobHState, colTop[0]);
306 m_fc.
Reshape(m_colBtm, m_colTop);
318 int nTrueInFeatures = (int)colBottom[0].count(2);
321 if (m_nReshapeCount > 0 &&
322 m_nBatchSize > 0 && m_nBatchSize == colBottom[0].num &&
323 m_nSeqLen > 0 && m_nSeqLen == colBottom[0].channels &&
324 m_nTrueInFeatures > 0 && m_nTrueInFeatures == nTrueInFeatures)
328 m_nBatchSize = colBottom[0].num;
329 m_nSeqLen = colBottom[0].channels;
330 m_nTrueInFeatures = nTrueInFeatures;
332 m_rgShape[0] = m_nBatchSize;
333 m_rgShape[1] = m_nHiddenSize;
335 m_blobHState1.
Reshape(m_rgShape);
336 m_blobHState.
Reshape(m_rgShape);
338 for (
int i=0; i<m_nSeqLen; i++)
340 m_rgBlobHState[i].
Reshape(m_rgShape);
344 m_blobTs.Reshape(m_rgShape);
346 m_rgShape[1] = m_nTrueInFeatures;
347 m_blobInputs.
Reshape(m_rgShape);
351 m_nMaskCount = m_blobMask.
count(2);
354 m_blobCurrentMask.
Reshape(m_rgShape);
356 m_rgShape[1] = m_nTrueInFeatures;
357 m_blobForwardInput.
Reshape(m_rgShape);
358 m_blobForwardInput1.
Reshape(m_rgShape);
359 m_blobForwardInput2.
Reshape(m_rgShape);
360 m_blobTimeSinceUpdate.
Reshape(m_rgShape);
361 m_blobTimeSinceUpdate1.
Reshape(m_rgShape);
365 m_blobForwardOutput.
Reshape(m_rgShape);
366 m_blobForwardOutput1.
Reshape(m_rgShape);
367 m_blobForwardOutput2.
Reshape(m_rgShape);
369 addBtmTop(m_blobForwardInput, m_blobInputs1);
371 m_colBtm.
Add(m_blobTimeSinceUpdate);
372 m_colBtm.
Add(m_blobMask);
373 m_cat.
Reshape(m_colBtm, m_colTop);
375 for (
int i = 0; i < m_nSeqLen; i++)
377 addBtmTop(m_blobInputs1, m_blobHState);
378 m_colBtm.
Add(m_blobHState1);
379 m_colBtm.
Add(m_blobTs);
381 ((
LnnUnitLayer<T>)m_rnn_cell).SetInternalSharedBlobs(m_rgrgInternalBlobs[i]);
383 m_rnn_cell.
Reshape(m_colBtm, m_colTop);
388 addBtmTop(m_blobHState, m_blobCurrentOutput);
389 m_fc.
Reshape(m_colBtm, m_colTop);
391 m_blobCurrentMaskFull.
ReshapeLike(m_blobCurrentOutput);
393 addBtmTop(m_blobHState, colTop[0]);
394 m_fc.
Reshape(m_colBtm, m_colTop);
413 m_blobForwardOutput.
SetData(0);
414 m_blobTimeSinceUpdate.
SetData(0);
417 for (
int t = 0; t < m_nSeqLen; t++)
422 m_cuda.channel_copy(m_blobTs.count(), m_blobTs.num, 1, m_nSeqLen, 1, t, colBottom[1].gpu_data, m_blobTs.mutable_gpu_data,
DIR.FWD);
426 if (colBottom.
Count() > 2)
428 int nMaskCount = colBottom[2].count(2);
429 if (nMaskCount == m_nTrueInFeatures)
453 addBtmTop(m_blobForwardInput, m_blobInputs1);
454 m_colBtm.
Add(m_blobTimeSinceUpdate);
455 m_colBtm.
Add(m_blobMask);
456 m_cat.
Forward(m_colBtm, m_colTop);
461 addBtmTop(m_blobForwardInput, m_blobInputs1);
462 m_colBtm.
Add(m_blobMask);
463 m_cat.
Forward(m_colBtm, m_colTop);
468 m_blobInputs1.
CopyFrom(m_blobInputs);
472 addBtmTop(m_blobInputs1, m_blobHState1);
473 m_colBtm.
Add(m_blobHState);
474 m_colBtm.
Add(m_blobTs);
476 ((
LnnUnitLayer<T>)m_rnn_cell).SetInternalSharedBlobs(m_rgrgInternalBlobs[t]);
478 m_rnn_cell.
Forward(m_colBtm, m_colTop);
481 m_rgBlobHState[t].
CopyFrom(m_blobHState1);
484 if (colBottom.
Count > 2)
489 m_blobCurrentMask.
SetDiff(1.0);
495 addBtmTop(m_blobHState1, m_blobCurrentOutput);
496 m_fc.
Forward(m_colBtm, m_colTop);
504 m_blobHState.
CopyFrom(m_blobHState1);
509 throw new NotImplementedException(
"return sequences is not implemented yet.");
511 else if (colBottom.
Count > 2)
513 colTop[0].
CopyFrom(m_blobForwardOutput);
517 addBtmTop(m_blobHState, colTop[0]);
518 m_fc.
Forward(m_colBtm, m_colTop);
546 throw new NotImplementedException(
"return sequences is not implemented yet.");
548 else if (colBottom.
Count > 2)
550 m_blobForwardOutput.
CopyFrom(colTop[0],
true);
554 addBtmTop(m_blobHState, colTop[0]);
555 m_fc.
Backward(m_colTop, rgbPropagateDown, m_colBtm);
558 int nMaskCount = colBottom[2].count(2);
559 for (
int t = m_nSeqLen - 1; t >= 0; t--)
561 m_blobHState1.
CopyFrom(m_rgBlobHState[t]);
563 if (colBottom.
Count > 2)
573 m_blobCurrentMask.
SetDiff(1.0);
579 m_blobForwardOutput1.
CopyFrom(m_blobForwardOutput,
true);
580 m_blobForwardOutput2.
CopyFrom(m_blobForwardOutput,
true);
586 addBtmTop(m_blobHState1, m_blobCurrentOutput);
587 m_fc.
Backward(m_colTop, rgbPropagateDown, m_colBtm);
590 if (t < m_nSeqLen - 1)
594 addBtmTop(m_blobInputs1, m_blobHState1);
595 m_colBtm.
Add(m_blobHState);
596 m_colBtm.
Add(m_blobTs);
598 ((
LnnUnitLayer<T>)m_rnn_cell).SetInternalSharedBlobs(m_rgrgInternalBlobs[t]);
600 m_rnn_cell.
Backward(m_colTop,
new List<bool>() {
true,
true,
true }, m_colBtm);
601 m_rgBlobHState[t].
CopyFrom(m_blobHState,
true);
604 if (colBottom.
Count() > 2)
608 addBtmTop(m_blobForwardInput1, m_blobInputs1);
609 m_colBtm.
Add(m_blobTimeSinceUpdate);
610 m_colBtm.
Add(m_blobMask);
611 m_cat.
Backward(m_colTop,
new List<bool>() {
true,
true,
true }, m_colBtm);
615 addBtmTop(m_blobForwardInput1, m_blobInputs1);
616 m_colBtm.
Add(m_blobMask);
617 m_cat.
Backward(m_colTop,
new List<bool>() {
true,
true }, m_colBtm);
624 if (nMaskCount == m_nTrueInFeatures)
638 m_blobInputs.
CopyFrom(m_blobInputs1,
true);
The Log class provides general output in text form.
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.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
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.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void Unsqueeze(int nNumAxes)
Unsqueeze the shape by adding shape=1 on each axis until the 'nNumAxes' is reached.
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.
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.
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.
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
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.
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
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.
LayerParameter.LayerType m_type
Specifies the Layer type.
BlobCollection< T > blobs
Returns the collection of learnable parameter Blobs for the Layer.
LayerParameter convertLayerParam(LayerParameter pChild, LayerParameter pParent)
Called to convert a parent LayerParameterEx, used in blob sharing, with a child layer parameter.
The CfcLayer implements the Closed form Continuous layer.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: attn
CfcLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The CfcLayer constructor.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input, hx, ts
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the Cfc value inputs.
override void dispose()
Releases all GPU and host resources used by the Layer.
override bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
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.
The LnnUnitLayer implements the base class to the Cfc and Ltc Unit layers.
int axis
The axis along which to concatenate – may be negative to index from the end (e.g.,...
Specifies the filler parameters used to create each Filler.
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.
CfcParameter cfc_param
Returns the parameter set when initialized with LayerType.CFC
LtcUnitParameter ltc_unit_param
Returns the parameter set when initialized with LayerType.LTC_UNIT
InnerProductParameter inner_product_param
Returns the parameter set when initialized with LayerType.INNERPRODUCT
ConcatParameter concat_param
Returns the parameter set when initialized with LayerType.CONCAT
CfcUnitParameter cfc_unit_param
Returns the parameter set when initialized with LayerType.CFC_UNIT
LayerType
Specifies the layer type.
Specifies the parameters used by the CfcLayer. Note, you must also fill out the CfcUnitParameter.
CELL_TYPE cell_type
Specifies the cell type to use (default = CFC).
CELL_TYPE
Defines the cell type.
int input_features
Specifies the number of input features.
int output_features
Specifies the number of output features
bool return_sequences
Specifies whether or not to return the sequence.
int hidden_size
Specifies the hidden size used to size the backbone units and other internal layers.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
int hidden_size
Specifies the number of hidden units (default = 256).
override void Copy(LayerParameterBase src)
Copy on parameter to another.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
DIR
Defines the direction of data flow.
WEIGHT_TARGET
Defines the type of weight to target in re-initializations.
The MyCaffe.layers.lnn namespace contains all Liquid Neural Network (LNN) related layers.
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-...