MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BiasLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.fillers;
9
10namespace MyCaffe.layers
11{
24 public class BiasLayer<T> : Layer<T>
25 {
26 Blob<T> m_blobBiasMultiplier;
27 int m_nOuterDim;
28 int m_nBiasDim;
29 int m_nInnerDim;
30 int m_nDim;
31
40 : base(cuda, log, p)
41 {
43 m_blobBiasMultiplier = new Blob<T>(cuda, log);
44 m_blobBiasMultiplier.Name = m_param.name + " bias_mult";
45 }
46
48 protected override void dispose()
49 {
50 m_blobBiasMultiplier.Dispose();
51 base.dispose();
52 }
53
55 protected override void setup_internal_blobs(BlobCollection<T> col)
56 {
57 if (col.Count > 0)
58 return;
59
60 col.Add(m_blobBiasMultiplier);
61 }
62
66 public override int MinBottomBlobs
67 {
68 get { return 1; }
69 }
70
74 public override int MaxBottomBlobs
75 {
76 get { return 2; }
77 }
78
82 public override int ExactNumTopBlobs
83 {
84 get { return 1; }
85 }
86
92 public override bool ReInitializeParameters(WEIGHT_TARGET target)
93 {
94 base.ReInitializeParameters(target);
95
96 if (target == WEIGHT_TARGET.BOTH || target == WEIGHT_TARGET.BIAS)
97 {
99 if (fp == null)
100 fp = new FillerParameter("constant", 0.0);
101
102 Filler<T> filler = Filler<T>.Create(m_cuda, m_log, fp);
103 filler.Fill(m_colBlobs[0]);
104 }
105
106 return true;
107 }
108
114 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
115 {
116 if (colBottom.Count == 1 && m_colBlobs.Count > 0)
117 {
118 m_log.WriteLine("Skipping parameter initialization.");
119 }
120 else if (colBottom.Count == 1)
121 {
122 // bias is a learned parameter; initialize it.
124 int nAxis = colBottom[0].CanonicalAxisIndex(p.axis);
125 int nNumAxes = p.num_axes;
126
127 m_log.CHECK_GE(nNumAxes, -1, "num_axes must be non-negative, or -1 to extend to end of bottom[0].");
128
129 if (nNumAxes >= 0)
130 m_log.CHECK_GE(colBottom[0].num_axes, nAxis + nNumAxes, "bias blob's shape extends past bottom[0]'s shape when applied starting with bottom[0] axis = " + nAxis.ToString());
131
133
134 List<int> rgBiasShape = new List<int>();
135 int nStart = nAxis;
136 int nEnd = (nNumAxes == -1) ? colBottom[0].shape().Count : nStart + nNumAxes;
137
138 for (int i = nStart; i < nEnd; i++)
139 {
140 rgBiasShape.Add(colBottom[0].shape(i));
141 }
142
143 Blob<T> blobBias = new Blob<T>(m_cuda, m_log);
144 blobBias.Name = m_param.name + " bias";
145 blobBias.type = BLOB_TYPE.INTERNAL;
146 blobBias.type = BLOB_TYPE.WEIGHT;
147
148 if (!shareParameter(blobBias, rgBiasShape))
149 {
150 blobBias.Reshape(rgBiasShape);
151 FillerParameter fp = p.filler;
152 if (fp == null)
153 fp = new FillerParameter("constant", 0.0);
154
155 Filler<T> filler = Filler<T>.Create(m_cuda, m_log, fp);
156 filler.Fill(blobBias);
157 }
158 m_colBlobs.Add(blobBias);
159 }
160
161 m_rgbParamPropagateDown = new DictionaryMap<bool>(m_colBlobs.Count, true);
162 }
163
169 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
170 {
172 Blob<T> blobBias = (colBottom.Count > 1) ? colBottom[1] : m_colBlobs[0];
173
174 // Always set axis == 0 in special case where bias is a scalar
175 // (num_axes == 0. Mathematically eqiuvalent for any choice of axis, os the
176 // actual setting can be safely ignored; and computation is most efficient
177 // with axis == 0 and (therefore) outer_dim == 1.
178 int nAxis = (blobBias.num_axes == 0) ? 0 : colBottom[0].CanonicalAxisIndex(p.axis);
179
180 m_log.CHECK_GE(colBottom[0].num_axes, nAxis + blobBias.num_axes, "bias blob's shape extends past bottom[0]'s shape when applied starting with bottom[0] axis = " + nAxis.ToString());
181
182 for (int i = 0; i < blobBias.num_axes; i++)
183 {
184 m_log.CHECK_EQ(colBottom[0].shape(nAxis + i), blobBias.shape(i), "dimension mismatch between bottom[0]->shape(" + (nAxis + i).ToString() + ") and bias->shape(" + i.ToString() + ")");
185 }
186
187 m_nOuterDim = colBottom[0].count(0, nAxis);
188 m_nBiasDim = blobBias.count();
189 m_nInnerDim = colBottom[0].count(nAxis + blobBias.num_axes);
190 m_nDim = m_nBiasDim * m_nInnerDim;
191
192 if (colBottom[0] != colTop[0])
193 colTop[0].ReshapeLike(colBottom[0]);
194
195 m_blobBiasMultiplier.Reshape(new List<int>() { m_nInnerDim });
196 m_blobBiasMultiplier.SetData(1.0);
197 }
198
204 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
205 {
206 int nCount = colTop[0].count();
207 long hBottomData = colBottom[0].gpu_data;
208 long hBiasData = ((colBottom.Count > 1) ? colBottom[1].gpu_data : m_colBlobs[0].gpu_data);
209 long hTopData = colTop[0].mutable_gpu_data;
210
211 m_cuda.bias_fwd(nCount, hBottomData, hBiasData, m_nBiasDim, m_nInnerDim, hTopData);
212 }
213
220 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
221 {
222 if (rgbPropagateDown[0] && colBottom[0] != colTop[0])
223 {
224 long hTopDiff = colTop[0].gpu_diff;
225 long hBottomDiff = colBottom[0].mutable_gpu_diff;
226 int nCount = colBottom[0].count();
227
228 m_cuda.copy(nCount, hTopDiff, hBottomDiff);
229 }
230
231 // in-place, we don't need to do anyting with the data diff.
232 bool bBiasParam = (colBottom.Count == 1) ? true : false;
233
234 if ((!bBiasParam && rgbPropagateDown[1]) || (bBiasParam && m_rgbParamPropagateDown[0]))
235 {
236 long hTopDiff = colTop[0].gpu_diff;
237 long hBiasDiff = (bBiasParam) ? m_colBlobs[0].mutable_gpu_diff : colBottom[1].mutable_gpu_diff;
238 double dfAccum = (bBiasParam) ? 1.0 : 0.0;
239 int nTopDiffOffset = 0;
240
241 for (int n = 0; n < m_nOuterDim; n++)
242 {
243 m_cuda.gemv(false, m_nBiasDim, m_nInnerDim, m_tOne, hTopDiff, m_blobBiasMultiplier.gpu_data, convert(dfAccum), hBiasDiff, nTopDiffOffset);
244 nTopDiffOffset += m_nDim;
245 dfAccum = 1.0;
246 }
247 }
248 }
249 }
250}
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
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 ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
int num_axes
Returns the number of axes in the Blob.
Definition: Blob.cs:705
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
BLOB_TYPE type
Returns the BLOB_TYPE of the Blob.
Definition: Blob.cs:2761
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
Definition: Blob.cs:684
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
Abstract Filler class used to fill blobs with values.
Definition: Filler.cs:19
void Fill(Blob< T > b)
Fill the blob with values based on the actual filler used.
Definition: Filler.cs:50
static Filler< T > Create(CudaDnn< T > cuda, Log log, FillerParameter p)
Create a new Filler instance.
Definition: Filler.cs:79
The BiasLayer computes a sum of two input Blobs, with the shape of the latter Blob 'broadcast' to mat...
Definition: BiasLayer.cs:25
BiasLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The BiasLayer constructor.
Definition: BiasLayer.cs:39
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
Definition: BiasLayer.cs:55
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The Forward computation.
Definition: BiasLayer.cs:204
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: BiasLayer.cs:48
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: bias
Definition: BiasLayer.cs:83
override bool ReInitializeParameters(WEIGHT_TARGET target)
Re-initialize the parameters of the layer.
Definition: BiasLayer.cs:92
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: BiasLayer.cs:114
override int MaxBottomBlobs
Returns the maximum number of required bottom (input) Blobs: input1, input2
Definition: BiasLayer.cs:75
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: BiasLayer.cs:169
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the input.
Definition: BiasLayer.cs:220
override int MinBottomBlobs
Returns the minimum number of required bottom (input) Blobs: input1
Definition: BiasLayer.cs:67
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
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
T m_tOne
Specifies a generic type equal to 1.0.
Definition: Layer.cs:72
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...
Definition: Layer.cs:1152
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
BlobCollection< T > m_colBlobs
Specifies the learnable parameter Blobs of the Layer.
Definition: Layer.cs:55
DictionaryMap< bool > m_rgbParamPropagateDown
Specifies whether or not to compute the learnable diff of each parameter Blob.
Definition: Layer.cs:63
Specifies the parameters for the BiasLayer
int axis
The first axis of bottom[0] (the first input Blob) along which to apply bottom[1] (the second input B...
int num_axes
(num_axes is ignored unless just one bottom is given and the bias is a learned parameter of the layer...
FillerParameter filler
(filler is ignored unless just one bottom is given and the bias is a learned parameter of the layer....
Specifies the filler parameters used to create each Filler.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
BiasParameter bias_param
Returns the parameter set when initialized with LayerType.BIAS
LayerType
Specifies the layer type.
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
BLOB_TYPE
Defines the tpe of data held by a given Blob.
Definition: Interfaces.cs:62
WEIGHT_TARGET
Defines the type of weight to target in re-initializations.
Definition: Interfaces.cs:38
The MyCaffe.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
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