Exemple #1
0
        public NDArray(Shape shape, bool delayAlloc = true, Context ctx = null, DType dtype = null)
        {
            if (ctx == null)
            {
                ctx = Context.CurrentContext;
            }

            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (dtype == null)
            {
                dtype = DType.Float32;
            }

            Logging.CHECK_EQ(NativeMethods.MXNDArrayCreateEx(shape.Data.ToArray(),
                                                             shape.Dimension,
                                                             ctx.GetDeviceType(),
                                                             ctx.GetDeviceId(),
                                                             delayAlloc.ToInt32(),
                                                             dtype.Index,
                                                             out var @out), NativeMethods.OK);
            NativePtr = @out;
            _Blob     = new NDBlob(@out);
        }
Exemple #2
0
        public NDArray()
        {
            Logging.CHECK_EQ(NativeMethods.MXNDArrayCreateNone(out var @out), NativeMethods.OK);

            NativePtr = @out;
            _Blob     = new NDBlob(@out);
        }
Exemple #3
0
        internal NDArray(NDArrayHandle handle)
        {
            if (handle == NDArrayHandle.Zero)
            {
                throw new ArgumentException("Can not pass IntPtr.Zero", nameof(handle));
            }

            NativePtr = handle;
            _Blob     = new NDBlob(handle);
        }
Exemple #4
0
        public NDArray(Array data, Context ctx = null, DType dtype = null)
        {
            if (ctx == null)
            {
                ctx = Context.CurrentContext;
            }

            var shapeData = new List <int>();

            for (int i = 0; i < data.Rank; i++)
            {
                shapeData.Add(data.GetLength(i));
            }

            var shape = new Shape(shapeData);

            if (dtype == null)
            {
                dtype = DType.InferDtype(data);
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            NDArrayHandle @out = new NDArrayHandle();

            Logging.CHECK_EQ(NativeMethods.MXNDArrayCreateEx(shape.Data.ToArray(),
                                                             shape.Dimension,
                                                             ctx.GetDeviceType(),
                                                             ctx.GetDeviceId(),
                                                             false.ToInt32(),
                                                             dtype.Index,
                                                             out @out), NativeMethods.OK);

            var datagch = GCHandle.Alloc(data, GCHandleType.Pinned);

            NativeMethods.MXNDArraySyncCopyFromCPU(@out, datagch.AddrOfPinnedObject(), (uint)shape.Size);
            datagch.Free();

            NativePtr = @out;
            _Blob     = new NDBlob(@out);
        }
Exemple #5
0
        public NDArray(Array data, Shape shape, Context ctx = null, DType dtype = null)
        {
            if (ctx == null)
            {
                ctx = Context.CurrentContext;
            }

            if (dtype == null)
            {
                dtype = DType.Float32;
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }


            Logging.CHECK_EQ(NativeMethods.MXNDArrayCreateEx(shape.Data,
                                                             shape.Dimension,
                                                             ctx.GetDeviceType(),
                                                             ctx.GetDeviceId(),
                                                             false.ToInt32(),
                                                             dtype.Index,
                                                             out var @out), NativeMethods.OK);
            var datagch = GCHandle.Alloc(data, GCHandleType.Pinned);

            NativeMethods.MXNDArraySyncCopyFromCPU(@out, datagch.AddrOfPinnedObject(), (uint)shape.Size);

            NativePtr = @out;
            _Blob     = new NDBlob(@out);
        }