【问题标题】:2Dimentional Numpy array(NDarray) to C#: Linq Query returns limitations of Marshal.Copy error2维Numpy数组(NDarray)到C#:Linq Query返回Marshal.Copy错误的限制
【发布时间】:2020-08-17 14:41:47
【问题描述】:

我已经在 c# 中使用 keras 实现了一个 ML 模型。输出是一个 4dim 向量。

我有以下预测函数来预测一个条目。

public (DateTime TimeStamp, double dim1, double dim2, double dim3, double dim4) Predict(ForecastControllPower input)
            => Predict(new List<ForecastControllPower>() { input }).First();

但我现在想返回整个 IEnumerable 条目。所以我也定义了如下函数:

        public IEnumerable<(DateTime TimeStamp, double dim1, double dim2, double dim3, double dim4)> Predict(IEnumerable<ForecastControllPower> input)
        {
           var model = Sequential.LoadModel(MODEL_FILEPATH, compile: false);
           var data = TransformData(input);
           var prediction = model.Predict(data.x);
           return input.Zip(prediction.GetData<float[]>().ToList(), (inp, pred) => (inp.TimeStamp, (double)pred[0], (double)pred[1], (double)pred[2], (double)pred[3]));//this line gets the error
       }

问题在于最后一个 Linq 查询(显示在代码中),我想返回一个带有所有正确参数的 IEnumerable。我得到的错误是运行时错误,内容如下:

System.InvalidOperationException
 HResult=0x80131509
 Message=Can not copy the data with data type due to limitations of Marshal.Copy: Single[]
 Source=Numpy.Bare
 StackTrace:
  at Numpy.NDarray.GetData[T]()[![enter image description here][1]][1]

下面是预测变量结构的截图。

另外,我认为问题的根源,我可以说以下两行返回相同的异常。

 var m = np.array(new double[,] {{1, 2}, {3, 4}});//making a numpy array, this works
 var t = m.GetData<double[,]>();//trying to get it back into C#, this gives the exception

【问题讨论】:

  • 根据链接我应该使用roots.GetData())。因为我返回了一个 4 维点数组,所以我假设 GetData 的数据类型应该没问题?
  • 那么你要么有一个二维点 (x,y) 或三个维度点 (x,y,x) 的数组。如果您有一个复杂类型的数组,则它是一个指针数组。你有 Point(整数)还是 PointF(浮点数)?
  • 你指的是ML模型输出的数据类型吗?那将是一个浮动。
  • 您说“我有一个 4 维点数组”。您指的是哪些要点?你有一维数组还是二维数组?

标签: c# .net linq keras


【解决方案1】:

看来你可以直接调用.GetData&lt;float&gt;(),它将返回二维数组展平为一维

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2016-11-03
    • 2021-06-22
    相关资源
    最近更新 更多