Example #1
0
        public IntPtr ToTHTensor()
        {
            IntPtr ret = (IntPtr)0;

            if (IntData != null)
            {
                ret = THWrapper.THIntTensor_new();
                CreatedTensors.Add(ret);

                if (Shape.Length == 2)
                {
                    THWrapper.THIntTensor_resize2d(ret, Shape[0], Shape[1]);
                    for (int i = 0; i < Shape[0]; i++)
                    {
                        for (int j = 0; j < Shape[1]; j++)
                        {
                            THWrapper.THIntTensor_set2d(ret, i, j, Get2DInt(i, j));
                        }
                    }
                }
                if (Shape.Length == 3)
                {
                    THWrapper.THIntTensor_resize3d(ret, Shape[0], Shape[1], Shape[2]);
                    for (int k = 0; k < Shape[0]; k++)
                    {
                        for (int i = 0; i < Shape[1]; i++)
                        {
                            for (int j = 0; j < Shape[2]; j++)
                            {
                                THWrapper.THIntTensor_set3d(ret, k, i, j, Get3DInt(k, i, j));
                            }
                        }
                    }
                }
                if (Shape.Length == 4)
                {
                    THWrapper.THIntTensor_resize4d(ret, Shape[0], Shape[1], Shape[2], Shape[3]);
                    for (int k1 = 0; k1 < Shape[0]; k1++)
                    {
                        for (int k = 0; k < Shape[1]; k++)
                        {
                            for (int i = 0; i < Shape[2]; i++)
                            {
                                for (int j = 0; j < Shape[3]; j++)
                                {
                                    THWrapper.THIntTensor_set4d(ret, k1, k, i, j, Get4DInt(k1, k, i, j));
                                }
                            }
                        }
                    }
                }
                return(ret);
            }
            if (Data != null)
            {
                ret = THWrapper.THFloatTensor_new();
                CreatedTensors.Add(ret);

                if (Shape.Length == 2)
                {
                    THWrapper.THFloatTensor_resize2d(ret, Shape[0], Shape[1]);
                    for (int i = 0; i < Shape[0]; i++)
                    {
                        for (int j = 0; j < Shape[1]; j++)
                        {
                            THWrapper.THFloatTensor_set2d(ret, i, j, Get2D(i, j));
                        }
                    }
                }
                if (Shape.Length == 3)
                {
                    THWrapper.THFloatTensor_resize3d(ret, Shape[0], Shape[1], Shape[2]);
                    for (int k = 0; k < Shape[0]; k++)
                    {
                        for (int i = 0; i < Shape[1]; i++)
                        {
                            for (int j = 0; j < Shape[2]; j++)
                            {
                                THWrapper.THFloatTensor_set3d(ret, k, i, j, Get3D(k, i, j));
                            }
                        }
                    }
                }
                if (Shape.Length == 4)
                {
                    THWrapper.THFloatTensor_resize4d(ret, Shape[0], Shape[1], Shape[2], Shape[3]);
                    for (int k1 = 0; k1 < Shape[0]; k1++)
                    {
                        for (int k = 0; k < Shape[1]; k++)
                        {
                            for (int i = 0; i < Shape[2]; i++)
                            {
                                for (int j = 0; j < Shape[3]; j++)
                                {
                                    THWrapper.THFloatTensor_set4d(ret, k1, k, i, j, Get4D(k1, k, i, j));
                                }
                            }
                        }
                    }
                }
            }
            return(ret);
        }