Skip to content

Knowing what to cast the model to is hard #2933

@sfilipi

Description

@sfilipi

I have been writing a few tests about saving the model and reloading the models, and casting the model members to the right types, so I can get to something nested.

Example

`(IEstimator<ITransformer> pipe, IDataView dataView) = GetBinaryClassificationPipeline();

 pipe = pipe.Append(ML.BinaryClassification.Trainers.LogisticRegression(
 new LogisticRegression.Options
 {
     ShowTrainingStatistics = true,
     ComputeStandardDeviation = new ComputeLRTrainingStdThroughMkl(),
 }));

 // SEE THE CASTS
  var transformer = pipe.Fit(dataView) as TransformerChain<BinaryPredictionTransformer<CalibratedModelParametersBase<LinearBinaryModelParameters, PlattCalibrator>>>;

  var linearModel = transformer.LastTransformer.Model.SubModel as LinearBinaryModelParameters;
  var stats = linearModel.Statistics;

 var modelPath = GetOutputPath("temp.zip");
 // Save model. 
 using (var file = File.Create(modelPath))
 transformer.SaveTo(ML, file);

 // Load model.
 TransformerChain<ITransformer> transformerChain;
 using (var file = File.OpenRead(modelPath))
 transformerChain = TransformerChain.LoadFrom(ML, file);

 // SEE THE CASTS
 var lastTransformer = transformerChain.LastTransformer as BinaryPredictionTransformer<IPredictorProducing<float>>;
  var model = lastTransformer.Model as ParameterMixingCalibratedModelParameters<IPredictorWithFeatureWeights<float>, ICalibrator>;
  linearModel = model.SubModel as LinearBinaryModelParameters;
  var stats = linearModel.Statistics;

`

Notice the casts
The only way to get to the Statistics (or weights, bias etc) is by casting to the right type.
It takes living in the Visual Studio debugger to figure out the right types..

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Priority of the issue for triage purpose: Needs to be fixed at some point.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions