MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
RecurrentParameter.cs
1using System;
2using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using MyCaffe.basecode;
9
10namespace MyCaffe.param
11{
15 [Serializable]
16 [TypeConverter(typeof(ExpandableObjectConverter))]
18 {
19 uint m_nNumOutput = 0;
20 FillerParameter m_weight_filler = new FillerParameter("xavier");
21 FillerParameter m_bias_filler = new FillerParameter("constant", 0.1);
22 bool m_bDebugInfo = false;
23 bool m_bExposeHiddenInput = false;
24 bool m_bExposeHiddenOutput = false;
25 uint m_nNumLayers = 1; // cuDnn only
26 double m_dfDropoutRatio = 0.0; // cuDnn only
27 long m_lDropoutSeed = 0; // cuDnn only
28 bool m_bBidirectional = false; // cuDnn only
29 bool m_bCudnnEnableTensorCores = false; // cuDnn only
30 bool m_bBatchFirst = false; // input and output have the batch in the first dim.
31 bool m_bAutoRepeatHiddenStatesAcrossLayers = false;
32 bool m_bUseCudnnRnn8IfSupported = false; // cuDnn only
33
36 {
37 }
38
43 public string useCaffeReason()
44 {
45 if (engine == Engine.CAFFE || engine == Engine.DEFAULT)
46 return "The engine setting is set on CAFFE or DEFAULT.";
47
48 return "";
49 }
50
55 {
56 get { return m_bUseCudnnRnn8IfSupported; }
57 set { m_bUseCudnnRnn8IfSupported = value; }
58 }
59
63 [Description("Auto repeat the hidden and cell states so that a separate state is fed to each layer.")]
65 {
66 get { return m_bAutoRepeatHiddenStatesAcrossLayers; }
67 set { m_bAutoRepeatHiddenStatesAcrossLayers = value; }
68 }
69
73 [Description("The input and outputs are shaped with the batch in the first dimension.")]
74 public bool batch_first
75 {
76 get { return m_bBatchFirst; }
77 set { m_bBatchFirst = value; }
78 }
79
87 public bool useCudnn()
88 {
89 if (engine == Engine.CAFFE || engine == Engine.DEFAULT)
90 return false;
91
92 return true;
93 }
94
98 [Description("Specifies whether the network is bidirectional (<i>true</i>) or unidirectional (<i>false</i> - default).")]
99 public bool bidirectional
100 {
101 get { return m_bBidirectional; }
102 set { m_bBidirectional = value; }
103 }
104
109 [Description("Specifies the dimension of the output (and usually hidden state) representation -- must be explicitly set to non-zero.")]
110 public uint num_output
111 {
112 get { return m_nNumOutput; }
113 set { m_nNumOutput = value; }
114 }
115
119 [Description("Specifies the filler for the weights.")]
121 {
122 get { return m_weight_filler; }
123 set { m_weight_filler = value; }
124 }
125
129 [Description("Specifies the filler for the bias.")]
131 {
132 get { return m_bias_filler; }
133 set { m_bias_filler = value; }
134 }
135
139 [Description("Specifies whether to enable displaying debug info in the unrolled recurrent net.")]
140 public bool debug_info
141 {
142 get { return m_bDebugInfo; }
143 set { m_bDebugInfo = value; }
144 }
145
151 [Description("Specifies whether to add as additional inputs (bottoms) the initial hidden state blobs. The number of additional bottom/top blobs required depends on the recurrent architecture -- e.g., 1 for RNN's, 2 for LSTM's.")]
153 {
154 get { return m_bExposeHiddenInput; }
155 set { m_bExposeHiddenInput = value; }
156 }
157
163 [Description("Specifies whether to add as additional outputs (tops) the final timestep hidden state blobs. The number of additional bottom/top blobs required depends on the recurrent architecture -- e.g., 1 for RNN's, 2 for LSTM's.")]
165 {
166 get { return m_bExposeHiddenOutput; }
167 set { m_bExposeHiddenOutput = value; }
168 }
169
174 [Description("Specifies the number of LSTM layers to implement (cuDnn only).")]
175 public uint num_layers
176 {
177 get { return m_nNumLayers; }
178 set { m_nNumLayers = value; }
179 }
180
185 [Description("Specifies the dropout ratio (cuDnn only). (e.g. the probability that values will be dropped out and set to zero. A value of 0.25 = 25% chance that a value is set to 0, and dropped out.)")]
186 public double dropout_ratio
187 {
188 get { return m_dfDropoutRatio; }
189 set { m_dfDropoutRatio = value; }
190 }
191
196 [Description("Specifies the random number generator seed used with the cuDnn dropout - the default value of '0' uses a random seed (cuDnn only).")]
197 public long dropout_seed
198 {
199 get { return m_lDropoutSeed; }
200 set { m_lDropoutSeed = value; }
201 }
202
209 [Description("Specifies to enable CUDA tensor cores when performing the rnn operations which is faster but not supported by all GPU's. When not supported, the default math is used.")]
211 {
212 get { return m_bCudnnEnableTensorCores; }
213 set { m_bCudnnEnableTensorCores = value; }
214 }
215
217 public override object Load(BinaryReader br, bool bNewInstance = true)
218 {
219 RawProto proto = RawProto.Parse(br.ReadString());
220 RecurrentParameter p = FromProto(proto);
221
222 if (!bNewInstance)
223 Copy(p);
224
225 return p;
226 }
227
229 public override LayerParameterBase Clone()
230 {
232 p.Copy(this);
233 return p;
234 }
235
237 public override void Copy(LayerParameterBase src)
238 {
239 base.Copy(src);
240
241 if (src is RecurrentParameter)
242 {
244 m_nNumOutput = p.num_output;
245 m_weight_filler = p.weight_filler.Clone();
246 m_bias_filler = p.bias_filler.Clone();
247 m_bDebugInfo = p.debug_info;
248 m_bExposeHiddenInput = p.expose_hidden_input;
249 m_bExposeHiddenOutput = p.expose_hidden_output;
250 m_dfDropoutRatio = p.dropout_ratio;
251 m_lDropoutSeed = p.dropout_seed;
252 m_nNumLayers = p.num_layers;
253 m_bBidirectional = p.bidirectional;
254 m_bCudnnEnableTensorCores = p.m_bCudnnEnableTensorCores;
255 m_bBatchFirst = p.batch_first;
256 m_bAutoRepeatHiddenStatesAcrossLayers = p.auto_repeat_hidden_states_across_layers;
257 m_bUseCudnnRnn8IfSupported = p.use_cudnn_rnn8_if_supported;
258 }
259 }
260
266 public override RawProto ToProto(string strName)
267 {
268 RawProto rpBase = base.ToProto("engine");
269 RawProtoCollection rgChildren = new RawProtoCollection();
270
271 rgChildren.Add(rpBase.Children);
272 rgChildren.Add("num_output", num_output.ToString());
273
274 if (weight_filler != null)
275 rgChildren.Add(weight_filler.ToProto("weight_filler"));
276
277 if (bias_filler != null)
278 rgChildren.Add(bias_filler.ToProto("bias_filler"));
279
280 rgChildren.Add("debug_info", debug_info.ToString());
281 rgChildren.Add("expose_hidden", (expose_hidden_input && expose_hidden_output).ToString());
282 rgChildren.Add("expose_hidden_input", expose_hidden_input.ToString());
283 rgChildren.Add("expose_hidden_output", expose_hidden_output.ToString());
284 rgChildren.Add("batch_first", batch_first.ToString());
285 rgChildren.Add("auto_repeat_hidden_states_across_layers", auto_repeat_hidden_states_across_layers.ToString());
286 rgChildren.Add("use_cudnn_rnn8_if_supported", use_cudnn_rnn8_if_supported.ToString());
287
288 if (engine != Engine.CAFFE)
289 {
290 rgChildren.Add("dropout_ratio", dropout_ratio.ToString());
291 rgChildren.Add("dropout_seed", dropout_seed.ToString());
292 rgChildren.Add("num_layers", num_layers.ToString());
293 rgChildren.Add("bidirectional", bidirectional.ToString());
294 }
295
296 rgChildren.Add("cudnn_enable_tensor_cores", cudnn_enable_tensor_cores.ToString());
297
298 return new RawProto(strName, "", rgChildren);
299 }
300
307 {
308 string strVal;
310
312
313 if ((strVal = rp.FindValue("num_output")) != null)
314 p.num_output = uint.Parse(strVal);
315
316 RawProto rpWeightFiller = rp.FindChild("weight_filler");
317 if (rpWeightFiller != null)
318 p.weight_filler = FillerParameter.FromProto(rpWeightFiller);
319
320 RawProto rpBiasFiller = rp.FindChild("bias_filler");
321 if (rpBiasFiller != null)
322 p.bias_filler = FillerParameter.FromProto(rpBiasFiller);
323
324 if ((strVal = rp.FindValue("debug_info")) != null)
325 p.debug_info = bool.Parse(strVal);
326
327 if ((strVal = rp.FindValue("expose_hidden")) != null)
328 {
329 p.expose_hidden_input = bool.Parse(strVal);
330 p.expose_hidden_output = bool.Parse(strVal);
331 }
332
333 if ((strVal = rp.FindValue("expose_hidden_input")) != null)
334 p.expose_hidden_input = bool.Parse(strVal);
335
336 if ((strVal = rp.FindValue("expose_hidden_output")) != null)
337 p.expose_hidden_output = bool.Parse(strVal);
338
339 if ((strVal = rp.FindValue("dropout_ratio")) != null)
340 p.dropout_ratio = ParseDouble(strVal);
341
342 if ((strVal = rp.FindValue("dropout_seed")) != null)
343 p.dropout_seed = long.Parse(strVal);
344
345 if ((strVal = rp.FindValue("num_layers")) != null)
346 p.num_layers = uint.Parse(strVal);
347
348 if ((strVal = rp.FindValue("bidirectional")) != null)
349 p.bidirectional = bool.Parse(strVal);
350
351 if ((strVal = rp.FindValue("cudnn_enable_tensor_cores")) != null)
352 p.cudnn_enable_tensor_cores = bool.Parse(strVal);
353
354 if ((strVal= rp.FindValue("batch_first")) != null)
355 p.batch_first = bool.Parse(strVal);
356
357 if ((strVal = rp.FindValue("auto_repeat_hidden_states_across_layers")) != null)
358 p.auto_repeat_hidden_states_across_layers = bool.Parse(strVal);
359
360 if ((strVal = rp.FindValue("use_cudnn_rnn8_if_supported")) != null)
361 p.use_cudnn_rnn8_if_supported = bool.Parse(strVal);
362
363 return p;
364 }
365 }
366}
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
Definition: RawProto.cs:17
RawProtoCollection Children
Returns a collection of this nodes child nodes.
Definition: RawProto.cs:96
RawProto FindChild(string strName)
Searches for a given node.
Definition: RawProto.cs:231
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
Specifies whether to use the NVIDIA cuDnn version or Caffe version of a given forward/backward operat...
Engine engine
Specifies the Engine in use.
EngineParameter()
Constructor for the parameter.
static EngineParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
Engine
Defines the type of engine to use.
Specifies the filler parameters used to create each Filler.
static FillerParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
FillerParameter Clone()
Creates a new copy of this instance of the parameter.
The LayerParameterBase is the base class for all other layer specific parameters.
Specifies the parameters used by the RecurrentLayer.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
static new RecurrentParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
bool use_cudnn_rnn8_if_supported
Specifies to use cuDnn RNN8 if supported (requires cuDnn 8.0 or higher), (default = false).
bool debug_info
Whether to enable displaying debug info in the unrolled recurrent net.
uint num_layers
The number of LSTM layers to implement.
uint num_output
The dimension of the output (and usually hidden state) representation – must be explicitly set to non...
FillerParameter weight_filler
The filler for the weights.
bool expose_hidden_output
Whether to add as additional outputs (tops) the final timestep hidden state blobs....
bool bidirectional
Specifies whether the network is bidirectional (true) or unidirectional (false - default).
bool batch_first
The input and outputs are shaped with the batch in the first dimension.
long dropout_seed
Specifies the seed used by cuDnn for random number generation.
bool useCudnn()
Queries whether or not to use NVIDIA's cuDnn.
RecurrentParameter()
Constructor for the parameter.
double dropout_ratio
Specifies the dropout ratio. (e.g. the probability that values will be dropped out and set to zero....
FillerParameter bias_filler
The filler for the bias.
override object Load(BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
bool cudnn_enable_tensor_cores
Specifies to enable the CUDA tensor cores when performing the rnn operations which is faster but not ...
bool auto_repeat_hidden_states_across_layers
Auto repeat the hidden and cell states so that a separate state is fed to each layer.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
string useCaffeReason()
Returns the reason that Caffe version was used instead of NVIDIA's cuDnn.
bool expose_hidden_input
Whether to add as additional inputs (bottoms) the initial hidden state blobss. The number of addition...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
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