最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

onnxruntime - previsions with a ONNX model in a C# console application with ML.NET - Stack Overflow

programmeradmin1浏览0评论

I'm trying to make previsions with a ONNX model in a C# console application with ML.NET I'm getting the message: System.ArgumentException: 'Length of memory (10) must match product of dimensions (0).'

This is what I got so far:

Output and input:

public class BertInput
{
    [VectorType(1, 256)]
    [ColumnName("input_ids")]
    public Int64[] InputIds { get; set; }

    [VectorType(1, 256)]
    [ColumnName("attention_mask")]
    public Int64[] AttentionMask { get; set; }

    [VectorType(1, 256)]
    [ColumnName("token_type_ids")]
    public Int64[] TokenTypeIds { get; set; }
}

 public class BertOutput
 {
     [ColumnName("logits")]
     public float[] Logits { get; set; }
 }

My program.cs so far:

[class Program
{
    static string ONNX_MODEL_PATH = "C:\\Users\\t203951\\source\\repos\\AutoMLONNXConsoleApp\\Assets\\Model\\model.onnx";
    static void Main(string\[\] args)
    {
        MLContext mlContext = new MLContext();
        var onnxPredictionPipeline = GetPredictionPipeline(mlContext);
        var onnxPredictionEngine = mlContext.Model.CreatePredictionEngine<BertInput, BertOutput>(onnxPredictionPipeline);

        var testInput = new BertInput
        {
            InputIds = (\[101, 7592, 1010, 2026, 3899, 2003, 10140, 102, 0, 0\]),
            AttentionMask = (\[1, 1, 1, 1, 1, 1, 1, 1, 0, 0\]),
            TokenTypeIds = (\[0, 0, 0, 0, 0, 0, 0, 0, 0, 0\])
        };

        var prediction = onnxPredictionEngine.Predict(testInput);

        Console.WriteLine($"Predicted Fare: {prediction.Logits.First()}");

        static ITransformer GetPredictionPipeline(MLContext mlContext)
        {
            var inputColumns = new string\[\] { "input_ids", "attention_mask", "token_type_ids" };

            var outputColumns = new string\[\] { "logits" };

            var onnxPredictionPipeline =
            mlContext
            .Transforms
            .ApplyOnnxModel(
                outputColumnNames: outputColumns,
                inputColumnNames: inputColumns,
                ONNX_MODEL_PATH);

            var emptyDv = mlContext.Data.LoadFromEnumerable(new BertInput\[\] { });

            return onnxPredictionPipeline.Fit(emptyDv);
        }
    }
}][1]
发布评论

评论列表(0)

  1. 暂无评论