/// <summary> /// Initializes a new instance of the <see cref="VowpalWabbit{TExample}"/> class. /// </summary> /// <param name="vw">The native instance to wrap.</param> /// <remarks>This instance takes ownership of <paramref name="vw"/> instance and disposes it.</remarks> public VowpalWabbit(VowpalWabbit vw) { if (vw == null) { throw new ArgumentNullException("vw"); } Contract.Ensures(this.serializer != null); Contract.EndContractBlock(); this.vw = vw; this.compiledSerializer = VowpalWabbitSerializerFactory.CreateSerializer <TExample>(vw.Settings); if (this.compiledSerializer == null) { throw new ArgumentException("No features found for " + typeof(TExample)); } this.serializer = this.compiledSerializer.Create(vw); // have a 2nd member to throw NullReferenceException in release instead of silently producing wrong results. this.learnSerializer = this.serializer.CachesExamples ? null : this.serializer; // have a 3rd member to avoid cast everytime... this.singleLineSerializer = this.serializer as VowpalWabbitSingleExampleSerializer <TExample>; }
private void Dispose(bool disposing) { if (disposing) { if (this.serializer != null) { this.serializer.Dispose(); this.serializer = null; } if (this.vw != null) { this.vw.Dispose(); this.vw = null; } } }
internal VowpalWabbitExampleValidator(VowpalWabbitSettings settings) { this.vw = new VowpalWabbit <TExample>(settings.ShallowCopy(enableStringExampleGeneration: true)); var compiler = this.vw.Serializer as VowpalWabbitSingleExampleSerializerCompiler <TExample>; if (compiler != null) { this.serializer = compiler.Func(this.vw.Native); } this.vwNative = new VowpalWabbit <TExample>(settings); compiler = this.vwNative.Serializer as VowpalWabbitSingleExampleSerializerCompiler <TExample>; if (compiler != null) { this.serializerNative = compiler.Func(this.vwNative.Native); } this.factorySerializer = VowpalWabbitSerializerFactory.CreateSerializer <TExample>(settings.ShallowCopy(enableStringExampleGeneration: true)).Create(this.vw.Native); }
private void Dispose(bool disposing) { if (disposing) { if (this.vw != null) { this.vw.Dispose(); this.vw = null; } if (this.vwNative != null) { this.vwNative.Dispose(); this.vwNative = null; } if (this.factorySerializer != null) { this.factorySerializer.Dispose(); this.factorySerializer = null; } } }
/// <summary> /// Initializes a new instance of the <see cref="VowpalWabbit{TExample}"/> class. /// </summary> /// <param name="vw">The native instance to wrap.</param> /// <param name="compiledSerializer">The per-compiled serializer.</param> /// <remarks>This instance takes ownership of <paramref name="vw"/> instance and disposes it.</remarks> public VowpalWabbit(VowpalWabbit vw, IVowpalWabbitSerializerCompiler <TExample> compiledSerializer) { if (vw == null) { throw new ArgumentNullException(nameof(vw)); } if (compiledSerializer == null) { throw new ArgumentNullException(nameof(compiledSerializer)); } Contract.Ensures(this.serializer != null); Contract.EndContractBlock(); this.vw = vw; this.compiledSerializer = compiledSerializer; this.serializer = this.compiledSerializer.Create(vw); // have a 2nd member to throw NullReferenceException in release instead of silently producing wrong results. this.learnSerializer = this.serializer.CachesExamples ? null : this.serializer; // have a 3rd member to avoid cast everytime... this.singleLineSerializer = this.serializer as VowpalWabbitSingleExampleSerializer <TExample>; }
/// <summary> /// Serializes the specifed example to VW native string format. /// </summary> /// <typeparam name="TExample">The user example type.</typeparam> /// <typeparam name="TActionDependentFeature">The user action dependent feature type.</typeparam> /// <param name="vw">The VW instance.</param> /// <param name="example">The shared example.</param> /// <param name="actionDependentFeatures">The action dependent features.</param> /// <param name="index">The optional index of the label example.</param> /// <param name="label">The optional label.</param> /// <param name="serializer">The example serializer.</param> /// <param name="actionDependentFeatureSerializer">The action dependent feature serializer.</param> /// <param name="dictionary">Dictionary used for dictify operation.</param> /// <param name="fastDictionary">Dictionary used for dictify operation.</param> /// <returns>The string serialized example.</returns> public static string SerializeToString <TExample, TActionDependentFeature>( VowpalWabbit vw, TExample example, IReadOnlyCollection <TActionDependentFeature> actionDependentFeatures, int?index = null, ILabel label = null, IVowpalWabbitSerializer <TExample> serializer = null, IVowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer = null, Dictionary <string, string> dictionary = null, Dictionary <object, string> fastDictionary = null) { if (vw == null) { throw new ArgumentNullException("vw"); } if (serializer == null) { serializer = VowpalWabbitSerializerFactory.CreateSerializer <TExample>(new VowpalWabbitSettings { EnableStringExampleGeneration = true }).Create(vw); } else if (!serializer.EnableStringExampleGeneration) { throw new ArgumentException("Serializer must be compiled using EnableStringExampleGeneration = true"); } if (actionDependentFeatureSerializer == null) { actionDependentFeatureSerializer = VowpalWabbitSerializerFactory.CreateSerializer <TActionDependentFeature>(new VowpalWabbitSettings { EnableStringExampleGeneration = true }).Create(vw); } else if (!actionDependentFeatureSerializer.EnableStringExampleGeneration) { throw new ArgumentException("Action dependent serializer must be compiled using EnableStringExampleGeneration = true"); } var stringExample = new StringBuilder(); var sharedExample = serializer.SerializeToString(example, SharedLabel.Instance, null, dictionary, fastDictionary); // check if we have shared features if (!string.IsNullOrWhiteSpace(sharedExample)) { stringExample.AppendLine(sharedExample); } var i = 0; foreach (var actionDependentFeature in actionDependentFeatures) { var adfExample = actionDependentFeatureSerializer.SerializeToString(actionDependentFeature, index != null && i == index ? label : null, null, dictionary, fastDictionary); if (!string.IsNullOrWhiteSpace(adfExample)) { stringExample.AppendLine(adfExample); } i++; } return(stringExample.ToString()); }