protected TrainStateBase(IChannel ch, int numFeatures, LinearPredictor predictor, OnlineLinearTrainer <TTransformer, TModel> parent) { Contracts.CheckValue(ch, nameof(ch)); ch.Check(numFeatures > 0, "Cannot train with zero features!"); ch.AssertValueOrNull(predictor); ch.AssertValue(parent); ch.Assert(Iteration == 0); ch.Assert(Bias == 0); ParentHost = parent.Host; ch.Trace("{0} Initializing {1} on {2} features", DateTime.UtcNow, parent.Name, numFeatures); // We want a dense vector, to prevent memory creation during training // unless we have a lot of features. if (predictor != null) { predictor.GetFeatureWeights(ref Weights); VBufferUtils.Densify(ref Weights); Bias = predictor.Bias; } else if (!string.IsNullOrWhiteSpace(parent.Args.InitialWeights)) { ch.Info("Initializing weights and bias to " + parent.Args.InitialWeights); string[] weightStr = parent.Args.InitialWeights.Split(','); if (weightStr.Length != numFeatures + 1) { throw ch.Except( "Could not initialize weights from 'initialWeights': expecting {0} values to initialize {1} weights and the intercept", numFeatures + 1, numFeatures); } var weightValues = new float[numFeatures]; for (int i = 0; i < numFeatures; i++) { weightValues[i] = Float.Parse(weightStr[i], CultureInfo.InvariantCulture); } Weights = new VBuffer <float>(numFeatures, weightValues); Bias = Float.Parse(weightStr[numFeatures], CultureInfo.InvariantCulture); } else if (parent.Args.InitWtsDiameter > 0) { var weightValues = new float[numFeatures]; for (int i = 0; i < numFeatures; i++) { weightValues[i] = parent.Args.InitWtsDiameter * (parent.Host.Rand.NextSingle() - (Float)0.5); } Weights = new VBuffer <float>(numFeatures, weightValues); Bias = parent.Args.InitWtsDiameter * (parent.Host.Rand.NextSingle() - (Float)0.5); } else if (numFeatures <= 1000) { Weights = VBufferUtils.CreateDense <Float>(numFeatures); } else { Weights = VBufferUtils.CreateEmpty <Float>(numFeatures); } WeightsScale = 1; }
protected virtual void InitCore(IChannel ch, int numFeatures, LinearPredictor predictor) { Contracts.Check(numFeatures > 0, "Can't train with zero features!"); Contracts.Check(NumFeatures == 0, "Can't re-use trainer!"); Contracts.Assert(Iteration == 0); Contracts.Assert(Bias == 0); ch.Trace("{0} Initializing {1} on {2} features", DateTime.UtcNow, Name, numFeatures); NumFeatures = numFeatures; // We want a dense vector, to prevent memory creation during training // unless we have a lot of features. // REVIEW: make a setting if (predictor != null) { predictor.GetFeatureWeights(ref Weights); VBufferUtils.Densify(ref Weights); Bias = predictor.Bias; } else if (!string.IsNullOrWhiteSpace(Args.InitialWeights)) { ch.Info("Initializing weights and bias to " + Args.InitialWeights); string[] weightStr = Args.InitialWeights.Split(','); if (weightStr.Length != NumFeatures + 1) { throw Contracts.Except( "Could not initialize weights from 'initialWeights': expecting {0} values to initialize {1} weights and the intercept", NumFeatures + 1, NumFeatures); } Weights = VBufferUtils.CreateDense <Float>(NumFeatures); for (int i = 0; i < NumFeatures; i++) { Weights.Values[i] = Float.Parse(weightStr[i], CultureInfo.InvariantCulture); } Bias = Float.Parse(weightStr[NumFeatures], CultureInfo.InvariantCulture); } else if (Args.InitWtsDiameter > 0) { Weights = VBufferUtils.CreateDense <Float>(NumFeatures); for (int i = 0; i < NumFeatures; i++) { Weights.Values[i] = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5); } Bias = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5); } else if (NumFeatures <= 1000) { Weights = VBufferUtils.CreateDense <Float>(NumFeatures); } else { Weights = VBufferUtils.CreateEmpty <Float>(NumFeatures); } WeightsScale = 1; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (targetType == typeof(Float) && value is Float && parameter is string) { Float orbit = (Float)value; Float factor = Float.Parse((string)parameter, CultureInfo.InvariantCulture); return((Float)(Math.Pow(orbit / 40, 0.4) * 770.0 * factor)); } return(default(Float)); }
private static StageClassifier LoadStageClassifier(int id, XmlNode stageNode) { MyFloat threshold = MyFloat.Parse(stageNode.Attributes["Threshold"].Value); int weakNum = int.Parse(stageNode.Attributes["ClassifiersNum"].Value); WeakClassifier[] weaks = new WeakClassifier[weakNum]; for (int i = 0; i < weakNum; i++) { XmlNode weakNode = stageNode.ChildNodes[i]; weaks[i] = LoadWeakClassifier(weakNode); } StageClassifier stage = StageClassifier.Load(id, threshold, weaks); return(stage); }
private static WeakClassifier LoadWeakClassifier(XmlNode weakNode) { MyFloat threshold = MyFloat.Parse(weakNode.Attributes["Threshold"].Value); MyFloat weight = MyFloat.Parse(weakNode.Attributes["Weight"].Value); bool posLargeThanThreshold = bool.Parse(weakNode.Attributes["PosLargeThanThreshold"].Value); ColorType colorType = ColorType.Null; if (weakNode.Attributes["ColorType"] != null) { string colorValue = weakNode.Attributes["ColorType"].Value; if (colorValue.Contains(ColorType.Gray.ToString())) { colorType |= ColorType.Gray; } if (colorValue.Contains(ColorType.Saturation.ToString())) { colorType |= ColorType.Saturation; } if (colorType == ColorType.Null) { colorType = ColorType.Gray; } } else { colorType = ColorType.Gray; } List <Rectangle> rects = new List <Rectangle>(4); List <int> weights = new List <int>(4); foreach (XmlNode node in weakNode.ChildNodes) { string[] nums = node.InnerText.Split(' '); int x = int.Parse(nums[0]); int y = int.Parse(nums[1]); int width = int.Parse(nums[2]); int height = int.Parse(nums[3]); int ww = int.Parse(nums[4]); rects.Add(new Rectangle(x, y, width, height)); weights.Add(ww); } return(WeakClassifier.Load(threshold, posLargeThanThreshold, weight, rects.ToArray(), weights.ToArray(), colorType)); }
/// <param name="value">a string representation of the value to convert</param> public Real(string value) { this._value = Numeric.Parse(value); }
/// <param name="value">a floating point number</param> /// <param name="style"></param> public static Real Parse(string value, System.Globalization.NumberStyles style) { return(new Real(Numeric.Parse(value, style))); }
/// <param name="value"></param> /// <param name="style"></param> /// <param name="provider"></param> public static Real Parse(string value, System.Globalization.NumberStyles style, IFormatProvider provider) { return(new Real(Numeric.Parse(value, style, provider))); }
/// <param name="value"></param> /// <param name="provider"></param> public static Real Parse(string value, IFormatProvider provider) { return(new Real(Numeric.Parse(value, provider))); }
/// <overloads> /// <summary> /// converts a string representation of a number in a specified style and culture-specific format /// to its floating point number equivilent /// </summary> /// <param name="value">a floating point number</param> /// <exception cref="System.ArgumentException" /> /// <exception cref="System.FormatException" /> /// <exception cref="System.ArgumentNullException" /> /// <returns>a Real</returns> /// </overloads> public static Real Parse(string value) { return(new Real(Numeric.Parse(value, englishCulture))); }
public static bool TryParse(string data, DataType flag, out SimpleType value) { value = Null; try { var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } switch (flag) { case DataType.Bool: value = new SimpleType(Boolean.Parse(data)); return(true); case DataType.Char: value = new SimpleType(Char.Parse(data)); return(true); case DataType.Byte: value = new SimpleType(Byte.Parse(data, numStyle)); return(true); case DataType.SByte: value = new SimpleType(SByte.Parse(data, numStyle)); return(true); case DataType.Short: value = new SimpleType(Int16.Parse(data, numStyle)); return(true); case DataType.UShort: value = new SimpleType(UInt16.Parse(data, numStyle)); return(true); case DataType.Int: value = new SimpleType(Int32.Parse(data, numStyle)); return(true); case DataType.UInt: value = new SimpleType(UInt32.Parse(data, numStyle)); return(true); case DataType.Long: value = new SimpleType(Int64.Parse(data, numStyle)); return(true); case DataType.ULong: value = new SimpleType(UInt64.Parse(data, numStyle)); return(true); case DataType.Float: value = new SimpleType(Single.Parse(data, numStyle)); return(true); case DataType.Decimal: value = new SimpleType(Decimal.Parse(data, numStyle)); return(true); case DataType.Double: value = new SimpleType(Double.Parse(data, numStyle)); return(true); case DataType.String: value = new SimpleType(data); return(true); case DataType.DateTime: value = new SimpleType(DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces)); return(true); case DataType.TimeSpan: value = new SimpleType(TimeSpan.Parse(data)); return(true); } } catch { } return(false); }
public static bool TryParse <T>(string data, out T value) { value = default(T); try { object val = null; var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } var f = DataTypes.Lookup(value); switch (f) { case DataType.Bool: val = Boolean.Parse(data); break; case DataType.Char: val = Char.Parse(data); break; case DataType.Byte: val = Byte.Parse(data, numStyle); break; case DataType.SByte: val = SByte.Parse(data, numStyle); break; case DataType.Short: val = Int16.Parse(data, numStyle); break; case DataType.UShort: val = UInt16.Parse(data, numStyle); break; case DataType.Int: val = Int32.Parse(data, numStyle); break; case DataType.UInt: val = UInt32.Parse(data, numStyle); break; case DataType.Long: val = Int64.Parse(data, numStyle); break; case DataType.ULong: val = UInt64.Parse(data, numStyle); break; case DataType.Float: val = Single.Parse(data, numStyle); break; case DataType.Decimal: val = Decimal.Parse(data, numStyle); break; case DataType.Double: val = Double.Parse(data, numStyle); break; case DataType.String: val = data; break; case DataType.DateTime: val = DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces); break; case DataType.TimeSpan: val = TimeSpan.Parse(data); break; } value = (T)val; return(true); } catch { } return(false); }
/// <summary>Converts the string representation of a number to a half-precision floating-point equivalent.</summary> /// <param name="s">String representation of the number to convert.</param> /// <param name="style">Specifies the format of s.</param> /// <param name="provider">Culture-specific formatting information.</param> /// <returns>A new Half instance.</returns> public static Half Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider) { return((Half)Single.Parse(s, style, provider)); }
/// <summary>Converts the string representation of a number to a half-precision floating-point equivalent.</summary> /// <param name="s">String representation of the number to convert.</param> /// <returns>A new Half instance.</returns> public static Half Parse(string s) { return((Half)Single.Parse(s)); }
public static bool TryConvert(string data, DataType flag, out object val) { val = null; if (flag == DataType.Null) { return(false); } try { var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } switch (flag) { case DataType.Bool: val = Boolean.Parse(data); return(true); case DataType.Char: val = Char.Parse(data); return(true); case DataType.Byte: val = Byte.Parse(data, numStyle); return(true); case DataType.SByte: val = SByte.Parse(data, numStyle); return(true); case DataType.Short: val = Int16.Parse(data, numStyle); return(true); case DataType.UShort: val = UInt16.Parse(data, numStyle); return(true); case DataType.Int: val = Int32.Parse(data, numStyle); return(true); case DataType.UInt: val = UInt32.Parse(data, numStyle); return(true); case DataType.Long: val = Int64.Parse(data, numStyle); return(true); case DataType.ULong: val = UInt64.Parse(data, numStyle); return(true); case DataType.Float: val = Single.Parse(data, numStyle); return(true); case DataType.Decimal: val = Decimal.Parse(data, numStyle); return(true); case DataType.Double: val = Double.Parse(data, numStyle); return(true); case DataType.String: val = data; return(true); case DataType.DateTime: val = DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces); return(true); case DataType.TimeSpan: val = TimeSpan.Parse(data); return(true); default: return(false); } } catch { return(false); } }
public static bool TryParse(string data, DataType flag, out SimpleType value) { try { switch (flag) { case DataType.Null: value = new SimpleType(null); break; case DataType.Bool: value = new SimpleType(Boolean.Parse(data)); break; case DataType.Char: value = new SimpleType(Char.Parse(data)); break; case DataType.Byte: value = new SimpleType(Byte.Parse(data)); break; case DataType.SByte: value = new SimpleType(SByte.Parse(data)); break; case DataType.Short: value = new SimpleType(Int16.Parse(data)); break; case DataType.UShort: value = new SimpleType(UInt16.Parse(data)); break; case DataType.Int: value = new SimpleType(Int32.Parse(data)); break; case DataType.UInt: value = new SimpleType(UInt32.Parse(data)); break; case DataType.Long: value = new SimpleType(Int64.Parse(data)); break; case DataType.ULong: value = new SimpleType(UInt64.Parse(data)); break; case DataType.Float: value = new SimpleType(Single.Parse(data)); break; case DataType.Decimal: value = new SimpleType(Decimal.Parse(data)); break; case DataType.Double: value = new SimpleType(Double.Parse(data)); break; case DataType.String: value = new SimpleType(data); break; case DataType.DateTime: value = new SimpleType(DateTime.Parse(data)); break; case DataType.TimeSpan: value = new SimpleType(TimeSpan.Parse(data)); break; default: value = new SimpleType(null); break; } return(true); } catch { value = new SimpleType(null); return(false); } }