Use schema from trained model instead of inferring it again from type when creating prediction engine.
System information
- Windows/Linux/MacOS:
- .NET Version: 2.1.200:
Issue
- Defining custom schema using TextLoaderArguments in TextLoader?
- Training failed when creating prediction engine?
- Expect that
CreateBatchPredictionEngineshould use schema from trained model instead of inferring schema again from input type?
Source code / logs
public class IrisData
{
public float Label;
public float[] Features;
}
public class IrisPrediction
{
[ColumnName("Score")]
public float[] PredictedLabels;
}
public void SampleTest()
{
string dataPath = GetDataPath("iris.txt");
var pipeline = new LearningPipeline(seed: 1, conc: 1);
pipeline.Add(new TextLoader(dataPath)
{
Arguments = new TextLoaderArguments
{
Separator = new[] { '\t' },
HasHeader = true,
Column = new[]
{
new TextLoaderColumn()
{
Name = "Label",
Source = new [] { new TextLoaderRange(0) },
Type = Runtime.Data.DataKind.Num
},
new TextLoaderColumn()
{
Name = "Features",
Source = new [] { new TextLoaderRange(1,4) },
Type = Runtime.Data.DataKind.Num
}
}
}
});
pipeline.Add(new StochasticDualCoordinateAscentClassifier()
{
NormalizeFeatures = NormalizeOption.No
});
PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>();
IrisPrediction prediction = model.Predict(new IrisData()
{
Features = new[] { 3.3f, 1.6f, 0.2f, 5.1f }
});
Assert.Equal(1, prediction.PredictedLabels[0], 2);
Assert.Equal(0, prediction.PredictedLabels[1], 2);
Assert.Equal(0, prediction.PredictedLabels[2], 2);
}
Hi @zeahmed , is related to the issue in #216 , and other issues opened about not being able to properly save the loader models in entry-point land?
@TomFinley, not directly related to #216 in a sense that any changes made to #216 will not directly contribute to solve it.
The problem here is that during training in this particular example, the schema is explicitly provided in TextLoader. However, the BatchPredictionEngine uses input type (IrisData) to infer schema and it fails there.
I have the same issue, I have written a custom loader with a custom schema and it fails in the CreateBatchPredictionEngine. CreateBatchPredictionEngine infers the schema from the type using SchemaDefinition.Create(userType). My schema is generated using different logic.
@Zruty0 is this still valid?
It is still valid.
Not valid anymore, the new API and the prediction allows you pass in schema that you can get when you do loadModel.