MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
GatherLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.param.beta;
9
10namespace MyCaffe.layers.beta
11{
18 public class GatherLayer<T> : Layer<T>
19 {
20 int m_nAxis = 0;
21 int m_nM = 0;
22 int m_nN = 0;
23 int m_nDim = 0;
24 int m_nDimAtAxis = 0;
25
34 : base(cuda, log, p)
35 {
37 }
38
42 public override int ExactNumBottomBlobs
43 {
44 get { return 2; }
45 }
46
50 public override int ExactNumTopBlobs
51 {
52 get { return 1; }
53 }
54
60 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
61 {
62 m_nAxis = colBottom[0].CanonicalAxisIndex(m_param.gather_param.axis);
63
64 if (m_nAxis != 0 && m_nAxis != 1)
65 m_log.FAIL("Currently only axis = 0 or axis = 1 are supported.");
66
67 m_nDim = colBottom[0].count(m_nAxis + 1);
68 m_nDimAtAxis = colBottom[0].shape()[m_nAxis];
69 m_nM = colBottom[0].count(0, m_nAxis);
70 m_nN = colBottom[1].count();
71 }
72
78 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
79 {
80 string strErr;
81 float[] rgIdxf = convertF(colBottom[1].mutable_cpu_data);
82 List<int> rgTopShape = GatherParameter.Reshape(m_nAxis, colBottom[0].shape(), colBottom[1].shape(), rgIdxf, out m_nDim, out m_nDimAtAxis, out m_nM, out m_nN, out strErr);
83 if (rgTopShape == null)
84 m_log.FAIL(strErr);
85
86 colTop[0].Reshape(rgTopShape);
87 }
88
98 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
99 {
100 int nCount = colBottom[0].count();
101 long hBottom = colBottom[0].gpu_data;
102 long hIdx = colBottom[1].gpu_data;
103 long hTop = colTop[0].mutable_gpu_data;
104 int nExpectedCount = (m_nAxis == 0) ? (m_nN * m_nDim) : (m_nN * m_nM);
105
106 m_log.CHECK_EQ(colTop[0].count(), nExpectedCount, "The top count should equal " + nExpectedCount.ToString() + "!");
107
108 m_cuda.gather_fwd(nCount, hBottom, hTop, m_nAxis, m_nDim, m_nDimAtAxis, m_nM, m_nN, hIdx);
109 }
110
119 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
120 {
121 int nCount = colBottom[0].count();
122 long hBottomDiff = colBottom[0].mutable_gpu_diff;
123 long hIdx = colBottom[1].gpu_data;
124 long hTopDiff = colTop[0].gpu_diff;
125
126 m_cuda.gather_bwd(nCount, hTopDiff, hBottomDiff, m_nAxis, m_nDim, m_nDimAtAxis, m_nM, m_nN, hIdx);
127 }
128 }
129}
The Log class provides general output in text form.
Definition: Log.cs:13
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
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
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
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
The GatherLayer extracts (gathers) data from specified indices along a given axis from the input and ...
Definition: GatherLayer.cs:19
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
Definition: GatherLayer.cs:98
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: flatten
Definition: GatherLayer.cs:51
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input.
Definition: GatherLayer.cs:43
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: GatherLayer.cs:78
GatherLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The GatherLayer constructor.
Definition: GatherLayer.cs:33
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the concatenate inputs.
Definition: GatherLayer.cs:119
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: GatherLayer.cs:60
Specifies the base parameter for all layers.
GatherParameter gather_param
Returns the parameter set when initialized with LayerType.GATHER
LayerType
Specifies the layer type.
Specifies the parameters for the GatherLayer.
int axis
Specifies the first axis to gather: all preceding axes are retained in the output....
static List< int > Reshape(int nAxis, List< int > rgBtmShape, List< int > rgIdxShape, float[] rgIdxF, out int nDim, out int nDimAtAxis, out int nM, out int nN, out string strErr)
Calculate the reshape array given the parameters.
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
The MyCaffe.layers.beta namespace contains all beta stage layers.
Definition: LayerFactory.cs:9
The MyCaffe.param.beta parameters are used by the MyCaffe.layer.beta 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-...
Definition: Annotation.cs:12