首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ML.net -在ML.net中使用ONNX时出错

ML.net -在ML.net中使用ONNX时出错
EN

Stack Overflow用户
提问于 2020-05-04 13:01:29
回答 1查看 778关注 0票数 0

在我的ML.net核心应用程序中使用.NET,我正在尝试使用一个KERAS模型,并将其导出到ONNX文件中。这是我的代码:

代码语言:javascript
复制
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Onnx;

public void getmodel(mydata[] data1)
{
    string modelPath = "C:\\MyStuff\\ONNXtest.onnx";
    MLContext mlContext = new MLContext();

    IDataView data = mlContext.Data.LoadFromEnumerable<mydata>(data1);

    OnnxScoringEstimator pipeline = mlContext.Transforms.ApplyOnnxModel(new[] { "output" }, new[] { "input" }, modelPath);

    IEnumerable<mydata> testdata = mlContext.Data.CreateEnumerable<mydata>(data, reuseRowObject: true);
    foreach (mydata row in testdata)
    {
        System.Diagnostics.Debug.WriteLine(row.myval[0]);
        System.Diagnostics.Debug.WriteLine(row.myval[1]);
        System.Diagnostics.Debug.WriteLine(row.myval[2]);
        System.Diagnostics.Debug.WriteLine(row.myval[3]);
        System.Diagnostics.Debug.WriteLine(row.myval[4]);
    }

    OnnxTransformer test = pipeline.Fit(data);

    IDataView transformedValues = test.Transform(data);

    IEnumerable<float[]> results = transformedValues.GetColumn<float[]>("output");

    double result = Convert.ToDouble(results.ElementAtOrDefault(0).GetValue(0));

}

mydata类如下所示:

代码语言:javascript
复制
public class mydata
{
     [VectorType(1,5,1)]
     [ColumnName("input")]   
     public float[] myval { get; set; }
}

我希望在模型中输入5个值,并查看"System.Diagnostics.Debug.WriteLine“输出,看起来一切都正常,IDataView数据包含5个用于输入模型的值。但是,"System.ArgumentOutOfRangeException“行在Microsoft.ML.OnnxTransformer.dll错误中会导致pipeline.Fit(data)

下面还有在python中训练和导出LSTM的代码:

代码语言:javascript
复制
regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1),name ='input'))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

regressor.add(Dense(units = 1,name ='output'))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, epochs = 100, batch_size = 32)

from winmltools import convert_keras
model_onnx = convert_keras(regressor, 7, name='sequential_7')
from winmltools.utils import save_model
save_model(model_onnx, 'C:\\MyStuff\\ONNXtest.onnx')

这是导出的网络的摘要:

代码语言:javascript
复制
Model: "sequential_7"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (LSTM)                 (None, 5, 50)             10400     
_________________________________________________________________
dropout_25 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_21 (LSTM)               (None, 5, 50)             20200     
_________________________________________________________________
dropout_26 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_22 (LSTM)               (None, 5, 50)             20200     
_________________________________________________________________
dropout_27 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_23 (LSTM)               (None, 50)                20200     
_________________________________________________________________
dropout_28 (Dropout)         (None, 50)                0         
_________________________________________________________________
output (Dense)               (None, 1)                 51        
=================================================================
Total params: 71,051
Trainable params: 71,051
Non-trainable params: 0

如果我只使用Python中的模型,它就能很好地工作,一种非常类似的设置(实际上,我认为它是同一段代码)在ML.net中很好地工作。但是现在我甚至不确定我的错误是在python上还是在c#端。有人能帮我找出如何处理这个错误吗?

EN

回答 1

Stack Overflow用户

发布于 2020-07-29 22:39:58

据我所知,这是C#方面的一个错误,因为ML.NET不允许多维输入数组。VectorTypeAttribute(dims)的文档在这一点上还不清楚,但似乎是一个很快就会解决的问题。请参阅:https://github.com/dotnet/machinelearning/issues/5273

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61592806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档