MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ParamSpec.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
7using MyCaffe.basecode;
8using MyCaffe.common;
9
10namespace MyCaffe.param
11{
16 [Serializable]
17 [TypeConverter(typeof(ExpandableObjectConverter))]
18 public class ParamSpec : BaseParameter, ICloneable, IBinaryPersist
19 {
25 string m_strName = "";
26
31 DimCheckMode m_shareMode = DimCheckMode.STRICT;
32
36 public enum DimCheckMode
37 {
41 STRICT = 0,
42
46 PERMISSIVE = 1
47 }
48
52 double m_dfLrMult = 1.0;
53
57 double m_dfDecayMult = 1.0;
58
62 public ParamSpec()
63 {
64 }
65
70 public ParamSpec(string strName)
71 {
72 m_strName = strName;
73 }
74
81 public ParamSpec(double dfLrMult, double dfDecayMult, string strName = null)
82 {
83 m_dfLrMult = dfLrMult;
84 m_dfDecayMult = dfDecayMult;
85
86 if (strName != null)
87 m_strName = strName;
88 }
89
94 public void Save(BinaryWriter bw)
95 {
96 bw.Write(m_strName);
97 bw.Write((int)m_shareMode);
98 bw.Write(m_dfLrMult);
99 bw.Write(m_dfDecayMult);
100 }
101
108 public object Load(BinaryReader br, bool bNewInstance)
109 {
110 ParamSpec p = this;
111
112 if (bNewInstance)
113 p = new ParamSpec();
114
115 p.m_strName = br.ReadString();
116 p.m_shareMode = (DimCheckMode)br.ReadInt32();
117 p.m_dfLrMult = br.ReadDouble();
118 p.m_dfDecayMult = br.ReadDouble();
119
120 return p;
121 }
122
123#pragma warning disable 1591
124
125 [Browsable(false)]
126 public string Name
127 {
128 get { return m_strName; }
129 }
130
131#pragma warning restore 1591
132
136 [Description("Specifies the name of this parameter.")]
137 public string name
138 {
139 get { return m_strName; }
140 set { m_strName = value; }
141 }
142
146 [Description("Specifies whether to require shared weights to have the same shape, or just the same count - defaults to STICT (same shape).")]
148 {
149 get { return m_shareMode; }
150 set { m_shareMode = value; }
151 }
152
156 [Description("Specifies the multiplier used on the global learning rate for this parameter.")]
157 public double lr_mult
158 {
159 get { return m_dfLrMult; }
160 set { m_dfLrMult = value; }
161 }
162
166 [Description("Specifies the multiplier used on the global weight decay for this parameter.")]
167 public double decay_mult
168 {
169 get { return m_dfDecayMult; }
170 set { m_dfDecayMult = value; }
171 }
172
177 public object Clone()
178 {
179 ParamSpec p = new ParamSpec();
180
181 p.m_strName = m_strName;
182 p.m_shareMode = m_shareMode;
183 p.m_dfDecayMult = m_dfDecayMult;
184 p.m_dfLrMult = m_dfLrMult;
185
186 return p;
187 }
188
194 public override RawProto ToProto(string strName)
195 {
196 RawProtoCollection rgChildren = new RawProtoCollection();
197
198 if (name.Length > 0)
199 rgChildren.Add("name", "\"" + name + "\"");
200
201 if (share_mode != DimCheckMode.STRICT)
202 rgChildren.Add("share_mode", share_mode.ToString());
203
204 rgChildren.Add("lr_mult", lr_mult.ToString());
205
206 if (decay_mult != 1)
207 rgChildren.Add("decay_mult", decay_mult.ToString());
208
209 return new RawProto(strName, "", rgChildren);
210 }
211
217 public static ParamSpec FromProto(RawProto rp)
218 {
219 string strVal;
220 ParamSpec p = new ParamSpec();
221
222 if ((strVal = rp.FindValue("name")) != null)
223 p.name = strVal;
224
225 if ((strVal = rp.FindValue("share_mode")) != null)
226 {
227 switch (strVal)
228 {
229 case "STRICT":
230 p.share_mode = DimCheckMode.STRICT;
231 break;
232
233 case "PERMISSIVE":
234 p.share_mode = DimCheckMode.PERMISSIVE;
235 break;
236
237 default:
238 throw new Exception("Unknown 'share_mode' value: " + strVal);
239 }
240 }
241
242 if ((strVal = rp.FindValue("lr_mult")) != null)
243 p.lr_mult = ParseDouble(strVal);
244
245 if ((strVal = rp.FindValue("decay_mult")) != null)
246 p.decay_mult = ParseDouble(strVal);
247
248 return p;
249 }
250 }
251}
The BaseParameter class is the base class for all other parameter classes.
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
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
Specifies training parameters (multipliers on global learning constants, and the name of other settin...
Definition: ParamSpec.cs:19
double decay_mult
Specifies the multiplier used on the global weight decay for this parameter.
Definition: ParamSpec.cs:168
ParamSpec(double dfLrMult, double dfDecayMult, string strName=null)
The ParamSpec constructor.
Definition: ParamSpec.cs:81
override RawProto ToProto(string strName)
Converts the ParamSpec into a RawProto.
Definition: ParamSpec.cs:194
ParamSpec()
The ParamSpec constructor.
Definition: ParamSpec.cs:62
DimCheckMode
Defines the dimension check mode.
Definition: ParamSpec.cs:37
string name
Specifies the name of this parameter.
Definition: ParamSpec.cs:138
DimCheckMode share_mode
Specifies whether to require shared weights to have the same shape, or just the same count - defaults...
Definition: ParamSpec.cs:148
object Load(BinaryReader br, bool bNewInstance)
Loads a ParamSpec from a binary reader.
Definition: ParamSpec.cs:108
static ParamSpec FromProto(RawProto rp)
Parses a new ParamSpec from a RawProto.
Definition: ParamSpec.cs:217
void Save(BinaryWriter bw)
Saves the ParamSpec to a binary writer.
Definition: ParamSpec.cs:94
double lr_mult
Specifies the multiplier used on the global learning rate for this parameter.
Definition: ParamSpec.cs:158
object Clone()
Creates a new copy of the ParamSpec.
Definition: ParamSpec.cs:177
ParamSpec(string strName)
The ParamSpec constructor.
Definition: ParamSpec.cs:70
The IBinaryPersist interface provides generic save and load functionality.
Definition: Utility.cs:16
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.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