簡體   English   中英

二維 Numpy 數組(NDarray)到 C#:Linq 查詢返回 Marshal.Copy 錯誤的限制

[英]2Dimentional Numpy array(NDarray) to C#: Linq Query returns limitations of Marshal.Copy error

我在 c# 中使用 keras 實現了 ML model。 output 是一個 4dim 向量。

我有以下預測 function 來預測一個條目。

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

但我現在想返回整個 IEnumerable 條目。 所以我也定義了以下function:

        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]

下面是預測變量結構的截圖。 在此處輸入圖像描述

此外,我認為這是問題的根源,我可以說以下 2 行返回相同的異常。

 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

看來您可以只調用.GetData<float>()它將返回二維數組展平為一維

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM