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.TransformerChain
1[Microsoft.ML.Data.RegressionPredictionTransformer
1[Microsoft.ML.Trainers.FastTree.FastTreeRegressionModelParameters]] can not be converted to Microsoft.ML.ISingleFeaturePredictionTransformer`1[System.Object]
How to access the model parameters?