dotnet / dotnet/machinelearning

Knowing what to cast the model to is hard

Open
#2,933 4 comments 0 reactions 0 assignees View on GitHub
area-Core Priority:2
Dominant language
C#
Stars
9.4k
Forks
2k
Avg merge
2d 20h
Merged PRs (30d)
11

Description

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 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>>;

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 transformerChain;
using (var file = File.OpenRead(modelPath))
transformerChain = TransformerChain.LoadFrom(ML, file);

// SEE THE CASTS
var lastTransformer = transformerChain.LastTransformer as BinaryPredictionTransformer>;
var model = lastTransformer.Model as ParameterMixingCalibratedModelParameters, 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..

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.