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

1

I have implemented a ML model using keras in c#. The output is a 4dim vector.

I have the following predict function to predict one entry.

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

But I want to now return a whole IEnumerable of entries. So I also define the following 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
       }

The problem sits with the last Linq Query(shown in the code) where I want to return an IEnumerable with all the right parameters. The error I get is a runtime error that reads:

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]

Below is a screenshot of the structure of the prediction variable. enter image description here

Additionally, which I think the base of the problem, I can say that the following 2 lines returns the same exception.

 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
c#
.net
linq
keras
asked on Stack Overflow Aug 17, 2020 by Ian • edited Aug 19, 2020 by Ian

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0