0

I was trying to use ML.net since a Web API, I followed all the steps of the tutorial of ML.net in https://learn.microsoft.com/es-es/dotnet/machine-learning/tutorials/taxi-fare

But, when I try to use the service I have the message : "Transforms.CategoricalHashOneHotVectorizer" not found int the line

model = pipeline.Train<Trazability, TrazabilityPrediction>();

I don't know how can obtain a correct response in the tutorial but when I tried to put the same code in a Web Api proyect obtain this wrong.

Here the code that I did:

Class Program.cs

public class Program
{
    const string _datapath = @".\Datos\Train.csv";
    const string _testdatapath = @".\Datos\Test.csv";
    const string _modelpath = @".\Datos\Model.zip";

    public float predicion()
    {
        var prediccion = DuracionDias();
        return float.Parse(prediccion.ToString());
    }

    public async Task<TrazabilityPrediction> DuracionDias() {

        PredictionModel<Trazability, TrazabilityPrediction> model = await Train();
        Evaluate(model);
        TrazabilityPrediction prediction = model.Predict(TrazabilityTest.Prueba1);

        return prediction;
    }

    public static async Task<PredictionModel<Trazability, TrazabilityPrediction>> Train()
    {
        PredictionModel<Trazability, TrazabilityPrediction> model = null;

        try
        {

            var pipeline = new LearningPipeline();
            pipeline.Add(new TextLoader(_datapath).CreateFrom<Trazability>(useHeader: true, separator: ';'));
            pipeline.Add(new ColumnCopier(("duracionDias", "Label")));
            pipeline.Add(new CategoricalHashOneHotVectorizer("producto", "proveedor"));
            pipeline.Add(new ColumnConcatenator("Features", "producto", "proveedor", "peso"));
            pipeline.Add(new FastTreeRegressor());

            model = pipeline.Train<Trazability, TrazabilityPrediction>();
            await model.WriteAsync(path: _modelpath);

        }
        catch(Exception e)
        {
            throw new Exception(e.Message);
        }

        return model;
    }

    private static void Evaluate(PredictionModel<Trazability, TrazabilityPrediction> model)
    {
        var testData = new TextLoader(_testdatapath).CreateFrom<Trazability>(useHeader: true, separator: ';');

        var evaluator = new RegressionEvaluator();

        RegressionMetrics metrics = evaluator.Evaluate(model, testData);

        Console.WriteLine($"Rms = {metrics.Rms}");
        Console.WriteLine($"RSquared = {metrics.RSquared}");

    }


}

and here are the models

 public class Trazability
{
    [Column("0")]
    public string producto;

    [Column("1")]
    public string proveedor;

    [Column("2")]
    public float peso;

    [Column("3")]
    public float duracionDias;
}

public class TrazabilityPrediction
{
    [ColumnName("Score")]
    public float duracionDias;
}

Also here have the .csv (train.csv and test.csv)

producto;proveedor;peso;duracionDias
Azucar;Sol;10;12
Azucar;Sol;10;12
Azucar;Sol;10;12
Azucar;Sol;10;12
Azucar;Sol;10;12
Azucar;Sol;20;24
Azucar;Sol;20;24
Azucar;Sol;20;24
Azucar;Sol;20;24
Azucar;Sol;20;24
Colorante;Sol;10;12
Colorante;Sol;10;12
Colorante;Sol;10;12
Colorante;Sol;10;12
Colorante;Sol;10;12
Colorante;Sol;20;24
Colorante;Sol;20;24
Colorante;Sol;20;24
Colorante;Sol;20;24
Colorante;Sol;20;24

please help me

4
  • Is there any additional information in the error you can share? It is strange that ColumnCopier works but CategoricalHashOneHotVectorizer does not, given that they are in the same assembly. Were you able to run the same pipeline in a console app and get results? Commented Sep 26, 2018 at 16:43
  • Yest @GalOshri when I run the same pipeline in a console app it works. I don't know what to do Commented Sep 27, 2018 at 23:29
  • Historically it's been caused by the fact that the necessary DLLs were not copied into the bin folder of the webside. Commented Sep 28, 2018 at 17:10
  • Actually, if you upgrade to 0.6 you might have this problem resolve itself, since we changed the way dependency injection works in that version Commented Sep 28, 2018 at 17:12

1 Answer 1

1

I have created a POC using the code and sample data you have provided.I am able to get the following output when I run the application.Please note I am using the ML.NET version 5.0

enter image description here

I think that your application is good but it is different version of ML.NET which is the problem.There have been couple of changes in the recent version.

If you need to use the features in the previous versions you can use the Microsoft.ML.Legacy namespace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.