private protected override OvaPredictor CreatePredictor() { Host.Check(TrainedEnsemble != null, "The predictor cannot be created before training is complete."); Host.Assert(_numClass > 1, "Must know the number of classes before creating a predictor."); Host.Assert(TrainedEnsemble.NumTrees % _numClass == 0, "Number of trees should be a multiple of number of classes."); var innerArgs = LightGbmInterfaceUtils.JoinParameters(Options); IPredictorProducing <float>[] predictors = new IPredictorProducing <float> [_tlcNumClass]; for (int i = 0; i < _tlcNumClass; ++i) { var pred = CreateBinaryPredictor(i, innerArgs); var cali = new PlattCalibrator(Host, -0.5, 0); predictors[i] = new FeatureWeightsCalibratedPredictor(Host, pred, cali); } return(OvaPredictor.Create(Host, predictors)); }
public FeatureSubsetModel(IPredictorProducing <TOutput> predictor, BitArray features = null, KeyValuePair <string, double>[] metrics = null) { if (!(predictor is IPredictorProducing <TOutput> predictorProducing)) { throw Contracts.ExceptParam(nameof(predictor), $"Input predictor did not have the expected output type {typeof(TOutput).Name}."); } Predictor = predictorProducing; int card; if (features != null && (card = Utils.GetCardinality(features)) < features.Count) { SelectedFeatures = features; Cardinality = card; } Metrics = metrics; }
internal static object Create(IHostEnvironment env, ModelLoadContext ctx, IHost host, IPredictorProducing <VBuffer <float> > model, Type predictionTransformerType) { // Create generic type of the prediction transformer using the correct TModel. // Return an instance of that type, passing the previously loaded model to the constructor var genericCtor = CreateConstructor(model.GetType(), predictionTransformerType); var genericInstance = genericCtor.Invoke(new object[] { env, ctx, host, model }); return(genericInstance); }
protected override BinaryPredictionTransformer <IPredictorProducing <float> > MakeTransformer(IPredictorProducing <float> model, Schema trainSchema) => new BinaryPredictionTransformer <IPredictorProducing <float> >(Host, model, trainSchema, FeatureColumn.Name);
public void Train(List <FeatureSubsetModel <IPredictorProducing <TOutput> > > models, RoleMappedData data, IHostEnvironment env) { Contracts.CheckValue(env, nameof(env)); var host = env.Register(Stacking.LoadName); host.CheckValue(models, nameof(models)); host.CheckValue(data, nameof(data)); using (var ch = host.Start("Training stacked model")) { ch.Check(Meta == null, "Train called multiple times"); ch.Check(BasePredictorType != null); var maps = new ValueMapper <VBuffer <Single>, TOutput> [models.Count]; for (int i = 0; i < maps.Length; i++) { Contracts.Assert(models[i].Predictor is IValueMapper); var m = (IValueMapper)models[i].Predictor; maps[i] = m.GetMapper <VBuffer <Single>, TOutput>(); } // REVIEW: Should implement this better.... var labels = new Single[100]; var features = new VBuffer <Single> [100]; int count = 0; // REVIEW: Should this include bad values or filter them? using (var cursor = new FloatLabelCursor(data, CursOpt.AllFeatures | CursOpt.AllLabels)) { TOutput[] predictions = new TOutput[maps.Length]; var vBuffers = new VBuffer <Single> [maps.Length]; while (cursor.MoveNext()) { Parallel.For(0, maps.Length, i => { var model = models[i]; if (model.SelectedFeatures != null) { EnsembleUtils.SelectFeatures(ref cursor.Features, model.SelectedFeatures, model.Cardinality, ref vBuffers[i]); maps[i](ref vBuffers[i], ref predictions[i]); } else { maps[i](ref cursor.Features, ref predictions[i]); } }); Utils.EnsureSize(ref labels, count + 1); Utils.EnsureSize(ref features, count + 1); labels[count] = cursor.Label; FillFeatureBuffer(predictions, ref features[count]); count++; } } ch.Info("The number of instances used for stacking trainer is {0}", count); var bldr = new ArrayDataViewBuilder(host); Array.Resize(ref labels, count); Array.Resize(ref features, count); bldr.AddColumn(DefaultColumnNames.Label, NumberType.Float, labels); bldr.AddColumn(DefaultColumnNames.Features, NumberType.Float, features); var view = bldr.GetDataView(); var rmd = new RoleMappedData(view, DefaultColumnNames.Label, DefaultColumnNames.Features); var trainer = BasePredictorType.CreateComponent(host); if (trainer.Info.NeedNormalization) { ch.Warning("The trainer specified for stacking wants normalization, but we do not currently allow this."); } Meta = trainer.Train(rmd); CheckMeta(); } }
internal static object Create(IHostEnvironment env, ModelLoadContext ctx, IHost host, IPredictorProducing <VBuffer <float> > model, Type generic) { // Create generic type of the prediction transformer using the correct TModel. // Return an instance of that type, passing the previously loaded model to the constructor Type[] genericTypeArgs = { model.GetType() }; Type constructed = generic.MakeGenericType(genericTypeArgs); Type[] constructorArgs = { typeof(IHostEnvironment), typeof(ModelLoadContext), typeof(IHost), model.GetType() }; var genericCtor = constructed.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, constructorArgs, null); var genericInstance = genericCtor.Invoke(new object[] { env, ctx, host, model }); return(genericInstance); }