0

Ive got a linear regression model in ML.NET and the predictions are working fine:

  MLContext mlContext = new MLContext(seed: 0);
            List<TwoInputRegressionModel> inputs = new List<TwoInputRegressionModel>();
            foreach (var JahrMitCO in ListWithCO)
            {
                float tempyear = JahrMitCO.Year;
                foreach (var JahrMitPopulation in Population)
                {
                    if (JahrMitPopulation.Year == tempyear)
                    {
                        inputs.Add(new TwoInputRegressionModel() { Year = tempyear, Population = JahrMitPopulation.Value, Co2 = JahrMitCO.Value });
                    }
                }
            }
            var model = Train(mlContext, inputs);
            TestSinglePrediction(mlContext, model); //works

But I would like to know how to gain access to the parameters (weights + bias) of the trained model? I do know that the ITransformer class (here called model)does contain a Model property, but trying to convert it to the LinearRegressionModelParameters class like stated on the documentation doesnt work:

 LinearRegressionModelParameters originalModelParameters = ((ISingleFeaturePredictionTransformer<object>)model).Model as LinearRegressionModelParameters; //Exception:System.InvalidCastException

The object of the type Microsoft.ML.Data.TransformerChain1[Microsoft.ML.Data.RegressionPredictionTransformer1[Microsoft.ML.Trainers.FastTree.FastTreeRegressionModelParameters]] can not be converted to Microsoft.ML.ISingleFeaturePredictionTransformer`1[System.Object]

How to access the model parameters?

actopozipc
  • 93
  • 12

1 Answers1

1

The problem in this case is that your model object isn't a ISingleFeaturePredictionTransformer, but instead it is a TransformerChain object (i.e. a chain of transformers), where the LastTransformer is the "prediction transformer".

So to fix this, first cast your model to TransformerChain<RegressionPredictionTransformer<FastTreeRegressionModelParameters>>, then you can get LastTransformer, which will return the RegressionPredictionTransformer<FastTreeRegressionModelParameters>, from there you can get the Model property.

If you don't happen to know at compile time which concrete type of transformer the TransformerChain will contain, you can cast model to IEnumerable<ITransformer> and get the .Last() transformer in the chain. That you can cast to ISingleFeaturePredictionTransformer<object> in order to get the Model property.

    ITransformer model = ...;
    IEnumerable<ITransformer> chain = model as IEnumerable<ITransformer>;

    ISingleFeaturePredictionTransformer<object> predictionTransformer =
        chain.Last() as ISingleFeaturePredictionTransformer<object>;

    object modelParameters = predictionTransformer.Model;

From there you can cast modelParameters to whatever specific ModelParameters class it happens to be.

Note: from your exception message, it doesn't appear you are training a linear regression model, but instead a fast tree model. Tree-based models won't be able to be casted as LinearRegressionModelParameters as so you won't see bias and weights, but instead you will see tree information.

Eric Erhardt
  • 2,286
  • 15
  • 19
  • Thank you very much for your help, I appreciate it so much! The modelParameters does contain 2 weights and a bias, shouldnt one of the weights be the bias? – actopozipc Feb 23 '20 at 20:05
  • I do realize that actopozipc most probably have already found the answer for his question, but for future generations here is a good link about what "bias" is: https://stackoverflow.com/a/2499936/2731639 – Alex Yagur Aug 21 '21 at 11:08