/// <summary> /// Predicts for the given example. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="reader">The example to predict for.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <param name="label"> /// Optional label, taking precedence over "_label" property found in <paramref name="reader"/>. /// If null, <paramref name="reader"/> will be inspected and the "_label" property used as label. /// </param> /// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param> public TPrediction Predict <TPrediction>(JsonReader reader, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, ILabel label = null, int?index = null) { using (var serializer = new VowpalWabbitJsonSerializer(vw)) using (var result = serializer.ParseAndCreate(reader, label, index)) { return(result.Predict(predictionFactory)); } }
/// <summary> /// Learn from the given example and return the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="json">The example to learn.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <param name="label"> /// Optional label, taking precedence over "_label" property found in <paramref name="json"/>. /// If null, <paramref name="json"/> will be inspected and the "_label" property used as label. /// </param> /// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param> /// <returns>The prediction for the given <paramref name="json"/>.</returns> public TPrediction Learn <TPrediction>(string json, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, ILabel label = null, int?index = null) { using (var serializer = new VowpalWabbitJsonSerializer(vw)) using (var result = serializer.ParseAndCreate(json, label, index)) { return(result.Learn(predictionFactory)); } }
/// <summary> /// Predicts for the given example. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="example">The example to predict for.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <param name="label">This label can be used to weight the example.</param> public TPrediction Predict <TPrediction>(TExample example, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, ILabel label = null) { Contract.Requires(example != null); Contract.Requires(predictionFactory != null); using (var ex = this.serializer.Serialize(example, label)) { return(this.vw.Predict(ex, predictionFactory)); } }
public TPrediction Predict <TPrediction>(object example, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, ILabel label = null, int?index = null) { Contract.Requires(example != null); Contract.Requires(predictionFactory != null); using (var ex = this.Serialize(example, label, index)) { return(ex.Predict(predictionFactory)); } }
/// <summary> /// Predicts for the given example. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="json">The example to predict for.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <param name="label"> /// Optional label, taking precedence over "_label" property found in <paramref name="json"/>. /// If null, <paramref name="json"/> will be inspected and the "_label" property used as label. /// </param> /// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param> public TPrediction Predict <TPrediction>(string json, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, ILabel label = null, int?index = null) { Contract.Requires(json != null); using (var serializer = new VowpalWabbitJsonSerializer(vw)) using (var result = serializer.ParseAndCreate(json, label, index)) { return(result.Predict(predictionFactory)); } }
/// <summary> /// Predicts for the given example. /// </summary> /// <param name="example">The example to predict for.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the given <paramref name="example"/>.</returns> /// <remarks> /// The method only enqueues the example for learning and returns immediately. /// Await the returned task to receive the prediction result. /// </remarks> public Task <TPrediction> Predict <TPrediction>(TExample example, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory) { Contract.Requires(example != null); Contract.Requires(predictionFactory != null); return(manager.Post(vw => { using (var ex = this.serializers[vw.Settings.Node].Serialize(example)) { return vw.Predict(ex, predictionFactory); } })); }
/// <summary> /// Learn from the given example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="example">The example to learn.</param> /// <param name="label">The label for this <paramref name="example"/>.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the given <paramref name="example"/>.</returns> public TPrediction Learn <TPrediction>(TExample example, ILabel label, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory) { Contract.Requires(example != null); Contract.Requires(label != null); Contract.Requires(predictionFactory != null); #if DEBUG // only in debug, since it's a hot path if (this.serializer.CachesExamples) { throw new NotSupportedException("Cached examples cannot be used for learning"); } #endif using (var ex = this.learnSerializer.Serialize(example, label)) { return(this.vw.Learn(ex, predictionFactory)); } }
/// <summary> /// Learn from this example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> /// <param name="vw">Use this VW instance for learning instead of the one the example was created from.</param> protected abstract TPrediction LearnInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw);
/// <summary> /// Predicts for this example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> /// <param name="vw">Use this VW instance for prediction instead of the one the example was created from.</param> public TPrediction Predict <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw = null) { return(this.PredictInternal(predictionFactory, vw ?? this.vw)); }
/// <summary> /// Calls learn or predict for the set of examples. Does required filtering of potential new line examples. /// </summary> private TPrediction Execute <TPrediction>(VowpalWabbit vw, Action <VowpalWabbitExample> predictOrLearn, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory = null) { Contract.Requires(predictOrLearn != null); // firstExample will contain prediction result VowpalWabbitExample firstExample = null; VowpalWabbitExample empty = null; try { if (this.SharedExample != null && !this.SharedExample.IsNewLine) { predictOrLearn(this.SharedExample); firstExample = this.SharedExample; } foreach (var ex in this.Examples) { if (!ex.IsNewLine) { predictOrLearn(ex); if (firstExample == null) { firstExample = ex; } } } // signal end-of-block empty = vw.GetOrCreateNativeExample(); empty.MakeEmpty(vw); predictOrLearn(empty); return(predictionFactory != null?firstExample.GetPrediction(vw, predictionFactory) : default(TPrediction)); } finally { if (empty != null) { empty.Dispose(); } } }
/// <summary> /// Predicts for these examples and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> protected override TPrediction PredictInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw) { return(this.Execute(vw, ex => vw.Predict(ex), predictionFactory)); }
/// <summary> /// Learns from the given example. /// </summary> /// <param name="example">The example to learn.</param> /// <param name="label">The label for this <paramref name="example"/>.</param> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the given <paramref name="example"/>.</returns> /// <remarks> /// The method only enqueues the example for learning and returns immediately. /// Await the returned task to receive the prediction result. /// </remarks> public Task <TPrediction> Learn <TPrediction>(TExample example, ILabel label, IVowpalWabbitPredictionFactory <TPrediction> predictionFactory) { Contract.Requires(example != null); Contract.Requires(label != null); Contract.Requires(predictionFactory != null); return(manager.Post(vw => { using (var ex = this.serializers[vw.Settings.Node].Serialize(example, label)) { return ex.Learn(predictionFactory); } })); }
/// <summary> /// Predicts for these examples and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> public override TPrediction Predict <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory) { return(this.Execute(ex => this.vw.Predict(ex), predictionFactory)); }
/// <summary> /// Predicts for this example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> public abstract TPrediction Predict <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory);
/// <summary> /// Predicts for this example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <param name="vw">The VW instance that should be used for prediction.</param> /// <returns>The prediction for the this example.</returns> protected override TPrediction PredictInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw) { return(vw.Predict <TPrediction>(this.Example, predictionFactory)); }
/// <summary> /// Predicts for this example and returns the current prediction for it. /// </summary> /// <typeparam name="TPrediction">The prediction type.</typeparam> /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param> /// <returns>The prediction for the this example.</returns> public override TPrediction Predict <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory) { return(this.vw.Predict <TPrediction>(this.Example, predictionFactory)); }