SciSharp/Numpy.NET

Tensorflow.Numpy.NDArray how to Numpy.NDarray

cross-hello opened this issue · 10 comments

I am using Tensorflow package in C# project. I decided use Numpy.NET package instead of Tensorflow.Numpy.
Sometime the return value type from Tensorflow library is Tensorflow.Numpy.NDArray, and = - = How could I transform between?

henon commented
  1. find out how to get a c# array from tensorflow (I don't know)
  2. Read the readme on how to create an ndarray from a c# array.

@cross-hello You can get the tensor data handle referenced by SafeTensorHandle

Ok
Now for future reference, The function could be like below:

        public static NDarray Tensorflow_Numpy_NDArray2Numpy_NDarray(Tensorflow.NumPy.NDArray a)
        {
            SafeTensorHandle b = a.Handle;
            IntPtr d = c_api.TF_TensorData(b);
            NDarray e = new NDarray(d, (long)a.size, d.GetDtype());
            return e;
        }

(Edit: Sorry I am not native C# developer)

Sorry, I find the method fail at the third argument in NDarray constructor.
public NDarray(IntPtr dataPtr, long dataLength, Dtype dtype) //copy from Numpy.NET
Could I get DType from Tensorflow.Numpy.NDArray variable?

henon commented

If it doesn't work directly I guess you need to use a switch to convert from the Tensorflow DType to the Numpy.Net DType

When I get .dtype.GetDtype() from Tensorflow.Numpy.NDArray variable, the exception throws:

Can not convert type of given object to dtype: Tensorflow.TF_DataType

The only thing I could get from Tensorflow.Numpy.NDArray is this:

Tensorflow.Numpy.NDArray x = Tensorflow.NumPy.np.array(1, 2, 3);
Console.WriteLine(x.dtype);// show TF_INT32
Console.WriteLine(x.dtype.GetDtype());// throw exception

Now the problem I want to know is how to construct a Numpy.Dtype variable via this = - =

henon commented

yes, I guess you need to do some kind of switch:

switch(x.dtype.ToString()) {
    case "TF_INT32":
        // ... use Numpy.NET's  Dtype.int32 or whatever it is called, you'll find it if you look in the code
       break;
   case " ... other TF type":   
        // ... use appropriate Numpy.NET Dtype
       break;
}

Thank you, this time it pass my test in int:

       public static NDarray Tensorflow_Numpy_NDArray2Numpy_NDarray(Tensorflow.NumPy.NDArray a)
        {
            SafeTensorHandle b = a.Handle;
            IntPtr d = c_api.TF_TensorData(b);
            NDarray e;
            switch(a.dtype.ToString())
            {
                //case "TF_INT32":  e= new NDarray(d, (long)a.shape.size, np.int32);
                //case "TF_INT32":  e= new NDarray(d, (long)a.size, np.int32);
                //case "TF_INT32":  e= new NDarray(d, (long)a.bytesize, np.int32); break;
                case "TF_FLOAT":  e= new NDarray(d, (long)a.bytesize, np.float_); break;
                case "TF_DOUBLE":  e= new NDarray(d, (long)a.bytesize, np.float64); break;
                case "TF_INT32":  e= new NDarray(d, (long)a.bytesize, np.int32); break;
                case "TF_UINT8":  e= new NDarray(d, (long)a.bytesize, np.uint8); break;
                case "TF_INT16":  e= new NDarray(d, (long)a.bytesize, np.int16); break;
                case "TF_INT8":  e= new NDarray(d, (long)a.bytesize, np.int8); break;
                case "TF_COMPLEX64":  e= new NDarray(d, (long)a.bytesize, np.complex64); break;
                case "TF_COMPLEX":  e= new NDarray(d, (long)a.bytesize, np.complex_); break;
                case "TF_INT64":  e= new NDarray(d, (long)a.bytesize, np.int64  ); break;
                case "TF_BOOL":  e= new NDarray(d, (long)a.bytesize, np.bool_); break;
                case "TF_QINT8":  e= new NDarray(d, (long)a.bytesize, np.int64 ); break;
                case "TF_QUINT8":  e= new NDarray(d, (long)a.bytesize, np.uint8); break;
                case "TF_QINT32":  e= new NDarray(d, (long)a.bytesize, np.int32  ); break;
                case "TF_BFLOAT16":  e= new NDarray(d, (long)a.bytesize, np.float16 ); break;
                case "TF_QINT16":  e= new NDarray(d, (long)a.bytesize, np.int16  ); break;
                case "TF_QUINT16":  e= new NDarray(d, (long)a.bytesize, np.uint16  ); break;
                case "TF_UINT16":  e= new NDarray(d, (long)a.bytesize, np.uint16  ); break;
                case "TF_COMPLEX128":  e= new NDarray(d, (long)a.bytesize, np.complex128  ); break;
                case "TF_HALF":  e= new NDarray(d, (long)a.bytesize, np.half); break;
                case "TF_UINT64":  e= new NDarray(d, (long)a.bytesize, np.uint64  ); break;
                case "DtFloatRef":  e= new NDarray(d, (long)a.bytesize, np.float32  ); break;
                case "DtDoubleRef":  e= new NDarray(d, (long)a.bytesize, np.float64  ); break;
                case "DtInt32Ref":  e= new NDarray(d, (long)a.bytesize, np.int32  ); break;
                case "DtUint8Ref":  e= new NDarray(d, (long)a.bytesize, np.uint8  ); break;
                case "DtInt16Ref":  e= new NDarray(d, (long)a.bytesize, np.int16  ); break;
                case "DtInt8Ref":  e= new NDarray(d, (long)a.bytesize, np.int8  ); break;
                case "DtComplex64Ref":  e= new NDarray(d, (long)a.bytesize, np.complex64 ); break;
                case "DtInt64Ref":  e= new NDarray(d, (long)a.bytesize, np.int64  ); break;
                case "DtBoolRef":  e= new NDarray(d, (long)a.bytesize, np.bool_  ); break;
                case "DtQint8Ref":  e= new NDarray(d, (long)a.bytesize, np.int8  ); break;
                case "DtQuint8Ref":  e= new NDarray(d, (long)a.bytesize, np.int8  ); break;
                case "DtQint32Ref":  e= new NDarray(d, (long)a.bytesize, np.int32  ); break;
                case "DtBfloat16Ref":  e= new NDarray(d, (long)a.bytesize, np.float16  ); break;
                case "DtQint16Ref":  e= new NDarray(d, (long)a.bytesize, np.int16  ); break;
                case "DtQuint16Ref":  e= new NDarray(d, (long)a.bytesize, np.int16  ); break;
                case "DtUint16Ref":  e= new NDarray(d, (long)a.bytesize, np.uint16  ); break;
                case "DtComplex128Ref":  e= new NDarray(d, (long)a.bytesize, np.complex128  ); break;
                case "DtHalfRef":  e= new NDarray(d, (long)a.bytesize, np.half  ); break;
                case "DtUint32Ref":  e= new NDarray(d, (long)a.bytesize, np.uint32  ); break;
                case "DtUint64Ref":  e= new NDarray(d, (long)a.bytesize, np.uint64  ); break;
                default: e= new NDarray(d,long)a.bytesize, np.int32); break;
            }
            return e;
        }