NumSharp icon indicating copy to clipboard operation
NumSharp copied to clipboard

np.meshgrid() has a hidden error returning wrong results

Open LordTrololo opened this issue 4 years ago • 2 comments

There seems to be a bug in the np.meshgrid() function. It has something to do with the way the unmanaged memory is handeld but I havent tried to understand the details.

Why hidden ? Well, lets say we have the following code:

var meshAB = np.meshgrid(np.arange(3), np.arange(3));

meshAB.Item1 seems to be ok, but meshAB.Item2 hides a nasty problem. Strangely enough, when one makes a clone of Item2, the cloned values are OK! This is also my quick fix for now.

So to summary, the code below:

var meshAB = np.meshgrid(np.arange(3), np.arange(3));
Console.WriteLine("meshAB.Item1.flatten(): " + meshAB.Item1.flatten().ToString());
Console.WriteLine("meshAB.Item2.flatten(): " + meshAB.Item2.flatten().ToString());

NDArray a = meshAB.Item1.Clone();
NDArray b = meshAB.Item2.Clone();

Console.WriteLine("a.flatten(): " +a.flatten().ToString());
Console.WriteLine("b.flatten(): " +b.flatten().ToString());

Will output:

meshAB.Item1.flatten(): [0, 1, 2, 0, 1, 2, 0, 1, 2]    
meshAB.Item2.flatten(): [0, 1, 2, 0, 1, 2, 0, 1, 2]     <-------WRONG
a.flatten(): [0, 1, 2, 0, 1, 2, 0, 1, 2]
b.flatten(): [0, 0, 0, 1, 1, 1, 2, 2, 2]      <------ CORRECT

The bug is nasty because in inspector we also see ok values which is not true. Here is an image: image

LordTrololo avatar Apr 23 '20 10:04 LordTrololo

Same problem, but clone didnt resolve the problem

ArthHil avatar Jul 15 '21 10:07 ArthHil

I tested it in tf.net preview. It's working good. image New version implemented the NumPy API in TensorFlow NumPy

Oceania2018 avatar Jul 15 '21 12:07 Oceania2018