machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

Use schema from trained model instead of inferring it again from type when creating prediction engine.

Open zeahmed opened this issue 8 years ago • 6 comments

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 CreateBatchPredictionEngine should 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);
}

zeahmed avatar Jun 11 '18 22:06 zeahmed

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 avatar Jun 15 '18 19:06 TomFinley

@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.

zeahmed avatar Jun 18 '18 17:06 zeahmed

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.

obke avatar Jun 27 '18 16:06 obke

@Zruty0 is this still valid?

shauheen avatar Sep 25 '18 20:09 shauheen

It is still valid.

Zruty0 avatar Sep 25 '18 20:09 Zruty0

Not valid anymore, the new API and the prediction allows you pass in schema that you can get when you do loadModel.

codemzs avatar Jun 30 '19 02:06 codemzs