2using System.Collections.Generic;
5using System.Net.Http.Headers;
41 List<Layer<T>> m_rgSingleVarGrn =
new List<Layer<T>>();
45 List<int> m_rgShape =
new List<int>(4);
46 List<int> m_rgShapeOringal =
new List<int>(4);
59 m_blobSparseWts =
new Blob<T>(cuda, log);
60 m_blobSparseWts.
Name = p.
name +
".spwts";
61 m_blobSparseWtsSmx =
new Blob<T>(cuda, log);
62 m_blobSparseWtsSmx.
Name = p.
name +
".spwts_smx";
63 m_blobSparseWtsSmxT =
new Blob<T>(cuda, log);
64 m_blobSparseWtsSmxT.
Name = p.
name +
".spwts_smxT";
65 m_blobGrn1 =
new Blob<T>(cuda, log);
67 m_blobProcessedInputs =
new Blob<T>(cuda, log);
68 m_blobProcessedInputs.
Name = p.
name +
".proc_in";
69 m_blobProcessedInputsT =
new Blob<T>(cuda, log);
70 m_blobProcessedInputsT.
Name = p.
name +
".proc_inT";
71 m_blobProcessedInputs1 =
new Blob<T>(cuda, log);
72 m_blobProcessedInputs1.
Name = p.
name +
".proc_in1";
73 m_blobBtm =
new Blob<T>(cuda, log);
81 dispose(ref m_blobSparseWtsSmx);
82 dispose(ref m_blobSparseWtsSmxT);
84 dispose(ref m_blobProcessedInputs);
85 dispose(ref m_blobProcessedInputsT);
86 dispose(ref m_blobProcessedInputs1);
89 if (m_colSingleVarGrn !=
null)
91 m_colSingleVarGrn.Dispose();
92 m_colSingleVarGrn =
null;
97 if (m_rgSingleVarGrn !=
null)
99 foreach (
Layer<T> layer
in m_rgSingleVarGrn)
103 m_rgSingleVarGrn =
null;
113 col.
Add(m_blobSparseWts);
114 col.
Add(m_blobSparseWtsSmx);
115 col.
Add(m_blobSparseWtsSmxT);
117 col.
Add(m_blobProcessedInputs);
118 col.
Add(m_blobProcessedInputsT);
119 col.
Add(m_blobProcessedInputs1);
170 List<int> rgShape =
new List<int>();
171 Blob<T> blobStaticSelection =
null;
173 if (colBottom.
Count > 1)
174 blobStaticSelection = colBottom[1];
178 if (m_grnFlatten ==
null)
191 addBtmTop(colBottom[0], m_blobSparseWts);
192 if (blobStaticSelection !=
null)
193 m_colBtm.
Add(blobStaticSelection);
194 m_grnFlatten.Setup(m_colBtm, m_colTop);
195 blobs.Add(m_grnFlatten.blobs);
199 if (m_softmax ==
null)
206 addBtmTop(m_blobSparseWts, m_blobSparseWtsSmx);
207 m_softmax.
Setup(m_colBtm, m_colTop);
210 rgShape =
Utility.Clone<
int>(m_blobSparseWtsSmx.
shape());
213 m_blobSparseWtsSmx.
Reshape(rgShape);
216 if (m_transpose ==
null)
223 addBtmTop(m_blobSparseWtsSmx, m_blobSparseWtsSmxT);
224 m_transpose.
Setup(m_colBtm, m_colTop);
229 rgShape.Add(colBottom[0].num);
251 m_rgSingleVarGrn.Add(grn);
252 m_colSingleVarGrn.Add(blobGrn);
254 addBtmTop(m_blobGrn1, m_colSingleVarGrn[i]);
255 m_rgSingleVarGrn[i].Setup(m_colBtm, m_colTop);
261 rgShape.Add(colBottom[0].num);
264 m_blobProcessedInputs.
Reshape(rgShape);
265 m_blobProcessedInputs1.
Reshape(rgShape);
276 Blob<T> blobStaticSelection =
null;
278 if (colBottom.
Count > 1)
279 blobStaticSelection = colBottom[1];
284 m_rgShapeOringal.Clear();
286 addBtmTop(colBottom[0], m_blobSparseWts);
287 if (colBottom.
Count > 1)
288 m_colBtm.
Add(blobStaticSelection);
289 m_grnFlatten.Reshape(m_colBtm, m_colTop);
291 addBtmTop(m_blobSparseWts, m_blobSparseWtsSmx);
292 m_softmax.
Reshape(m_colBtm, m_colTop);
294 rgShape =
Utility.Clone<
int>(m_blobSparseWtsSmx.
shape());
297 m_blobSparseWtsSmx.
Reshape(rgShape);
299 addBtmTop(m_blobSparseWtsSmx, m_blobSparseWtsSmxT);
300 m_transpose.
Reshape(m_colBtm, m_colTop);
303 rgShape.Add(colBottom[0].num);
309 m_colSingleVarGrn[i].ReshapeLike(m_blobGrn1);
311 addBtmTop(m_blobGrn1, m_colSingleVarGrn[i]);
312 m_rgSingleVarGrn[i].Reshape(m_colBtm, m_colTop);
315 rgShape =
Utility.Clone<
int>(m_colSingleVarGrn[0].shape());
317 m_blobProcessedInputs.
Reshape(rgShape);
318 m_blobProcessedInputs1.
Reshape(rgShape);
321 if (colTop.
Count > 1)
342 Blob<T> blobStaticSelection =
null;
344 if (colBottom.
Count > 1)
345 blobStaticSelection = colBottom[1];
351 addBtmTop(colBottom[0], m_blobSparseWts);
352 if (colBottom.
Count > 1)
353 m_colBtm.
Add(blobStaticSelection);
354 m_grnFlatten.Forward(m_colBtm, m_colTop);
357 addBtmTop(m_blobSparseWts, m_blobSparseWtsSmx);
358 m_softmax.
Forward(m_colBtm, m_colTop);
361 List<int> rgShape =
Utility.Clone<
int>(m_blobSparseWtsSmx.
shape());
364 m_blobSparseWtsSmx.
Reshape(rgShape);
374 addBtmTop(m_blobGrn1, m_colSingleVarGrn[i]);
375 m_rgSingleVarGrn[i].Forward(m_colBtm, m_colTop);
385 addBtmTop(m_blobSparseWtsSmx, m_blobSparseWtsSmxT);
386 m_transpose.
Forward(m_colBtm, m_colTop);
389 int nInnerNum = m_blobProcessedInputs.
count(2);
394 m_cuda.channel_sum(m_blobProcessedInputs1.
count(), m_blobProcessedInputs1.
num, m_blobProcessedInputs1.
channels, nInnerNum, m_blobProcessedInputs1.
gpu_data, colTop[0].mutable_gpu_data,
false);
395 if (colTop.
Count > 1)
396 colTop[1].
CopyFrom(m_blobSparseWts);
399 private void copyShape(List<int> rg,
Blob<T> b)
403 for (
int i = 0; i < b.
shape().Count; i++)
426 int nInnerNum = m_blobProcessedInputs.
count(2);
434 copyShape(m_rgShapeOringal, m_blobProcessedInputs);
435 copyShape(m_rgShape, m_blobProcessedInputs);
436 m_rgShape.Insert(1, 1);
437 m_blobProcessedInputs.
Reshape(m_rgShape);
439 m_blobProcessedInputs.
Reshape(m_rgShapeOringal);
441 copyShape(m_rgShapeOringal, colTop[0]);
442 copyShape(m_rgShape, colTop[0]);
443 m_rgShape.Insert(1, 1);
447 m_blobSparseWtsSmxT.
MatMul(m_blobProcessedInputsT, colTop[0],
true,
false,
false, 1,
false,
true,
true);
448 colTop[0].
Reshape(m_rgShapeOringal);
462 addBtmTop(m_blobGrn1, m_colSingleVarGrn[i]);
463 m_rgSingleVarGrn[i].Backward(m_colTop, rgbPropagateDown, m_colBtm);
472 addBtmTop(m_blobSparseWtsSmx, m_blobSparseWtsSmxT);
473 m_transpose.
Backward(m_colTop, rgbPropagateDown, m_colBtm);
476 addBtmTop(m_blobSparseWts, m_blobSparseWtsSmx);
477 m_softmax.
Backward(m_colTop, rgbPropagateDown, m_colBtm);
483 addBtmTop(colBottom[0], m_blobSparseWts);
484 if (colBottom.
Count > 1)
485 m_colBtm.
Add(colBottom[1]);
486 m_grnFlatten.Backward(m_colTop, rgbPropagateDown, m_colBtm);
489 m_cuda.add(colBottom[0].count(), colBottom[0].gpu_diff, m_blobBtm.
gpu_diff, colBottom[0].mutable_gpu_diff);
The Log class provides general output in text form.
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 ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
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 MatMul(Blob< T > blobA, Blob< T > blobB, bool bReshape=false, bool bTransA=false, bool bTransB=false, double dfScale=1.0, bool bADiff=false, bool bBDiff=false, bool bCDiff=false)
MatMul blobA with blobB and place the result in this blob (e.g. this = matmul(A, B))....
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 CopyFromAndTransposeHeightWidth(Blob< T > blobSrc, bool bCopyDiff=false, bool bUseCuda=true)
Copy from a source Blob and transpose the height and width of the copy.
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.
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.
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.
void Dispose()
Releases all GPU and host resources used by the Layer.
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 VarSetNetLayer implements the Variable Selection Network
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the top (output) blobs.
override int MinBottomBlobs
Returns the min number of required bottom (input) Blobs: flattened_embedding
override int MinTopBlobs
Returns the exact number of required top (output) Blobs: outputs_sum
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.
VarSetNetLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The constructor.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override int MaxTopBlobs
Returns the exact number of required top (output) Blobs: outputs_sum, sparse_wts
override void dispose()
Releases all GPU and host resources used by the Layer.
override int MaxBottomBlobs
Returns the min number of required bottom (input) Blobs: flattened_embedding, context
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.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
GrnParameter grn_param
Returns the parameter set when initialized with LayerType.GLU
TransposeParameter transpose_param
Returns the parameter set when initialized with LayerType.TRANSPOSE
VarSelNetParameter varselnet_param
Returns the parameter set when initialized with LayerType.VARSELNET
LayerType
Specifies the layer type.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
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.
BLOB_TYPE
Defines the tpe of data held by a given Blob.
The MyCaffe.layers.tft namespace contains all TFT 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-...