コード例 #1
0
ファイル: Tensor.cs プロジェクト: yumingfan/TensorFlowSharp
 static extern unsafe TF_Tensor TF_NewTensor(TFDataType dataType, IntPtr zeroDims, int num_dims, IntPtr data, size_t len, Deallocator deallocator, IntPtr deallocator_arg);
コード例 #2
0
ファイル: Tensor.cs プロジェクト: yumingfan/TensorFlowSharp
        // General purpose constructor, specifies data type and gets pointer to buffer
        // Is the default good, one where we let the user provide their own deallocator, or should we make a copy in that case?
        /// <summary>
        /// Low-level tensor constructor that creates a tensor from a buffer pointed to by an IntPtr.
        /// </summary>
        /// <param name="dataType">Specifies the data type held by the tensor, as well as how to interpret the provided data.</param>
        /// <param name="dims">Describes the tensor shape, an array that indicates .</param>
        /// <param name="data">Pointer to the raw data that will be used to initialize the tensor.</param>
        /// <param name="dataSize">The size of the data being passed in.</param>
        /// <param name="deallocator">Deallocator method, it is invoked when the tensor is destroyed to release the data pointed to by <paramref name="data"/>.</param>
        /// <param name="deallocatorData">An optional argument of data that is passed to the deallocator method when the tensor is destroyed, you can use this to pass context information.</param>
        public TFTensor(TFDataType dataType, long [] dims, IntPtr data, size_t dataSize, Deallocator deallocator, IntPtr deallocatorData) : base(IntPtr.Zero)
        {
            if (dims == null)
            {
                throw new ArgumentNullException("dims");
            }

            handle = TF_NewTensor(dataType, dims, dims.Length, data, dataSize, deallocator, deallocatorData);
        }
コード例 #3
0
        private IntPtr Allocate(NDArray nd)
        {
            IntPtr dotHandle = IntPtr.Zero;
            ulong  size      = 0;

            if (nd.dtype.Name != "String")
            {
                dotHandle = Marshal.AllocHGlobal(nd.dtypesize * nd.size);
                size      = (ulong)(nd.size * nd.dtypesize);
            }

            var dataType = ToTFDataType(nd.dtype);
            // shape
            var dims = nd.shape.Select(x => (long)x).ToArray();

            switch (nd.dtype.Name)
            {
            case "Int16":
                Marshal.Copy(nd.Data <short>(), 0, dotHandle, nd.size);
                break;

            case "Int32":
                Marshal.Copy(nd.Data <int>(), 0, dotHandle, nd.size);
                break;

            case "Single":
                Marshal.Copy(nd.Data <float>(), 0, dotHandle, nd.size);
                break;

            case "Double":
                Marshal.Copy(nd.Data <double>(), 0, dotHandle, nd.size);
                break;

            case "String":
                var   str       = nd.Data <string>()[0];
                ulong dst_len   = c_api.TF_StringEncodedSize((ulong)str.Length);
                var   dataType1 = ToTFDataType(nd.dtype);
                // shape
                var dims1 = nd.shape.Select(x => (long)x).ToArray();

                var tfHandle1 = c_api.TF_AllocateTensor(dataType1,
                                                        dims1,
                                                        nd.ndim,
                                                        dst_len + sizeof(Int64));

                dotHandle = c_api.TF_TensorData(tfHandle1);
                Marshal.WriteInt64(dotHandle, 0);
                c_api.TF_StringEncode(str, (ulong)str.Length, dotHandle + sizeof(Int64), dst_len, status);
                return(tfHandle1);

            default:
                throw new NotImplementedException("Marshal.Copy failed.");
            }

            // Free the original buffer and set flag
            Deallocator deallocator = (IntPtr values, IntPtr len, ref bool closure) =>
            {
                Marshal.FreeHGlobal(dotHandle);
                closure = true;
            };

            var tfHandle = c_api.TF_NewTensor(dataType,
                                              dims,
                                              nd.ndim,
                                              dotHandle,
                                              size,
                                              deallocator,
                                              ref deallocator_called);

            return(tfHandle);
        }
コード例 #4
0
        private IntPtr Allocate(NDArray nd)
        {
            IntPtr dotHandle = IntPtr.Zero;
            ulong  size      = 0;

            if (nd.dtype.Name != "String")
            {
                dotHandle = Marshal.AllocHGlobal(nd.dtypesize * nd.size);
                size      = (ulong)(nd.size * nd.dtypesize);
            }

            var dataType = ToTFDataType(nd.dtype);
            // shape
            var dims = nd.shape.Select(x => (long)x).ToArray();
            var nd1  = nd.ravel();

            switch (nd.dtype.Name)
            {
            case "Int16":
                Marshal.Copy(nd1.Data <short>(), 0, dotHandle, nd.size);
                break;

            case "Int32":
                Marshal.Copy(nd1.Data <int>(), 0, dotHandle, nd.size);
                break;

            case "Single":
                Marshal.Copy(nd1.Data <float>(), 0, dotHandle, nd.size);
                break;

            case "Double":
                Marshal.Copy(nd1.Data <double>(), 0, dotHandle, nd.size);
                break;

            case "Byte":
                Marshal.Copy(nd1.Data <byte>(), 0, dotHandle, nd.size);

                /*var bb = nd.Data<byte>();
                 * var bytes = Marshal.AllocHGlobal(bb.Length);
                 * Marshal.Copy(bb, 0, bytes, bb.Length);
                 * ulong bytes_len = c_api.TF_StringEncodedSize((ulong)bb.Length);
                 * var dataTypeByte = ToTFDataType(nd.dtype);
                 * // shape
                 * var dims2 = nd.shape.Select(x => (long)x).ToArray();
                 *
                 * var tfHandle2 = c_api.TF_AllocateTensor(dataTypeByte,
                 *  dims2,
                 *  nd.ndim,
                 *  bytes_len + sizeof(Int64));
                 *
                 * dotHandle = c_api.TF_TensorData(tfHandle2);
                 * Marshal.WriteInt64(dotHandle, 0);
                 * c_api.TF_StringEncode(bytes, (ulong)bb.Length, dotHandle + sizeof(Int64), bytes_len, status);
                 * return tfHandle2;*/
                break;
                //case "String":

                /*string ss = nd.Data<string>()[0];
                 * var str = Marshal.StringToHGlobalAnsi(ss);
                 * ulong dst_len = c_api.TF_StringEncodedSize((ulong)ss.Length);
                 * var dataType1 = ToTFDataType(nd.dtype);
                 * // shape
                 * var dims1 = nd.shape.Select(x => (long)x).ToArray();
                 *
                 * var tfHandle1 = c_api.TF_AllocateTensor(dataType1,
                 *  dims1,
                 *  nd.ndim,
                 *  dst_len + sizeof(Int64));
                 *
                 * dotHandle = c_api.TF_TensorData(tfHandle1);
                 * Marshal.WriteInt64(dotHandle, 0);
                 * c_api.TF_StringEncode(str, (ulong)ss.Length, dotHandle + sizeof(Int64), dst_len, status);
                 * return tfHandle1;*/
                break;

            default:
                throw new NotImplementedException("Marshal.Copy failed.");
            }

            // Free the original buffer and set flag
            Deallocator deallocator = (IntPtr values, IntPtr len, ref bool closure) =>
            {
                Marshal.FreeHGlobal(values);
                closure = true;
            };

            var tfHandle = c_api.TF_NewTensor(dataType,
                                              dims,
                                              dims.Length,
                                              dotHandle,
                                              (UIntPtr)size,
                                              deallocator,
                                              ref deallocator_called);

            return(tfHandle);
        }
コード例 #5
0
 public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, ref bool deallocator_arg);
コード例 #6
0
        private IntPtr Allocate(NDArray nd, TF_DataType?tensorDType = null)
        {
            if (tensorDType == TF_DataType.TF_STRING &&
                nd.dtype.Name == "Byte")
            {
                return(new Tensor(nd.Data <byte>()));
            }

            IntPtr dotHandle = IntPtr.Zero;
            ulong  size      = 0;

            if (nd.dtype.Name != "String")
            {
                dotHandle = Marshal.AllocHGlobal(nd.dtypesize * nd.size);
                size      = (ulong)(nd.size * nd.dtypesize);
            }

            var dataType = ToTFDataType(nd.dtype);
            // shape
            var dims = nd.shape.Select(x => (long)x).ToArray();
            var nd1  = nd.ravel();

            switch (nd.dtype.Name)
            {
            case "Boolean":
                var boolVals = Array.ConvertAll(nd1.Data <bool>(), x => Convert.ToByte(x));
                Marshal.Copy(boolVals, 0, dotHandle, nd.size);
                break;

            case "Int16":
                Marshal.Copy(nd1.Data <short>(), 0, dotHandle, nd.size);
                break;

            case "Int32":
                Marshal.Copy(nd1.Data <int>(), 0, dotHandle, nd.size);
                break;

            case "Int64":
                Marshal.Copy(nd1.Data <long>(), 0, dotHandle, nd.size);
                break;

            case "Single":
                Marshal.Copy(nd1.Data <float>(), 0, dotHandle, nd.size);
                break;

            case "Double":
                Marshal.Copy(nd1.Data <double>(), 0, dotHandle, nd.size);
                break;

            case "Byte":
                Marshal.Copy(nd1.Data <byte>(), 0, dotHandle, nd.size);
                break;

            case "String":
                return(new Tensor(UTF8Encoding.UTF8.GetBytes(nd.Data <string>(0))));

            default:
                throw new NotImplementedException($"Marshal.Copy failed for {nd.dtype.Name}.");
            }

            // Free the original buffer and set flag
            Deallocator deallocator = (IntPtr values, IntPtr len, ref bool closure) =>
            {
                Marshal.FreeHGlobal(values);
                closure = true;
            };

            var tfHandle = c_api.TF_NewTensor(dataType,
                                              dims,
                                              dims.Length,
                                              dotHandle,
                                              (UIntPtr)size,
                                              deallocator,
                                              ref deallocator_called);

            return(tfHandle);
        }
コード例 #7
0
 public static extern SafeTensorHandle TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg);