An enhanced version of .NET array which is scalable, easy to use, capable of running on various hardware CPU, GPU, FPGA Implements the System.IDisposable
Inheritance: IDisposable
Example #1
0
        public static double Var(SuperArray arr, bool isbiased = false)
        {
            double r, i;

            Internal.VERIFY(AFStatistics.af_var_all(out r, out i, arr.variable._ptr, isbiased));
            return(r);
        }
Example #2
0
        public static double StdDev(SuperArray arr)
        {
            double r, i;

            Internal.VERIFY(AFStatistics.af_stdev_all(out r, out i, arr.variable._ptr));
            return(r);
        }
Example #3
0
        /// <summary>
        /// Creates new contiguous.
        /// </summary>
        /// <param name="src">The source.</param>
        /// <returns>NDArray.</returns>
        public static SuperArray NewContiguous(SuperArray src)
        {
            var result = new SuperArray((long[])src.Shape.Clone(), src.ElementType);

            Copy(result, src);
            return(result);
        }
Example #4
0
        /// <summary>
        /// Repeats the array along the dimension.
        /// </summary>
        /// <param name="repetitions">The repetitions.</param>
        /// <returns>ArithArray.</returns>
        /// <exception cref="InvalidOperationException">
        /// repetitions must be at least the same length as the number of array dimensions
        /// or
        /// All dimensions must be repeated at least once
        /// </exception>
        public SuperArray RepeatTensor(params long[] repetitions)
        {
            if (repetitions.Length < this.DimensionCount)
            {
                throw new InvalidOperationException("repetitions must be at least the same length as the number of array dimensions");
            }
            if (repetitions.Any(x => x < 1))
            {
                throw new InvalidOperationException("All dimensions must be repeated at least once");
            }

            var paddedSrc  = this.PadToDimCount(repetitions.Length);
            var resultSize = paddedSrc.Shape.Zip(repetitions, (s, r) => s * r).ToArray();

            var result = new SuperArray(resultSize, this.ElementType);

            var urTensor = result.CopyRef();

            for (int i = 0; i < paddedSrc.DimensionCount; ++i)
            {
                var oldUrTensor = urTensor;
                urTensor = urTensor.Unfold(i, paddedSrc.Shape[i], paddedSrc.Shape[i]);
                oldUrTensor.Dispose();
            }

            paddedSrc = paddedSrc.PadToDimCount(urTensor.DimensionCount);
            var expandedSrc = paddedSrc.Expand(urTensor.Shape);

            Helper.Copy(urTensor, expandedSrc);

            return(result);
        }
Example #5
0
        public static double Min(SuperArray arr)
        {
            double r, i;

            Internal.VERIFY(AFAlgorithm.af_min_all(out r, out i, arr.variable._ptr));
            return(r);
        }
Example #6
0
        public static SuperArray Clip <T>(SuperArray arr, T lo, T hi) where T : struct
        {
            IntPtr ptr;

            Internal.VERIFY(AFArith.af_clamp(out ptr, arr.variable._ptr, Data.Constant <T>(lo, arr.variable.Dimensions)._ptr, Data.Constant <T>(hi, arr.variable.Dimensions)._ptr, false));
            return(new SuperArray(new AFArray(ptr)));
        }
Example #7
0
        /// <summary>
        /// Abses the minimum maximum.
        /// </summary>
        /// <param name="storage">The storage.</param>
        /// <param name="array">The array.</param>
        /// <returns>Tuple<System.Double, System.Double&gt;.</returns>
        private static Tuple <double, double> AbsMinMax(Storage storage, SuperArray array)
        {
            if (storage.ElementCount == 0)
            {
                return(Tuple.Create(0.0, 0.0));
            }

            double min = storage.GetElementAsFloat(0);
            double max = storage.GetElementAsFloat(0);

            // HACK this is a hacky way of iterating over the elements of the array.
            // if the array has holes, this will incorrectly include those elements
            // in the iteration.
            var minOffset = array.StorageOffset;
            var maxOffset = minOffset + Helper.GetStorageSize(array.Shape, array.Strides) - 1;

            for (long i = minOffset; i <= maxOffset; ++i)
            {
                var item = storage.GetElementAsFloat(i);
                if (item < min)
                {
                    min = item;
                }
                if (item > max)
                {
                    max = item;
                }
            }

            return(Tuple.Create(Math.Abs(min), Math.Abs(max)));
        }
Example #8
0
        /// <summary>
        /// Fill one hot label
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="labelCount">The label count.</param>
        /// <param name="labels">The labels.</param>
        /// <exception cref="InvalidOperationException">
        /// result must be a 2D array
        /// or
        /// first dimension of result must equal the number of samples
        /// or
        /// second dimension of result must be at least as large as labelCount
        /// or
        /// label at index " + i + " is out of range 0 <= x < labelCount
        /// </exception>
        public void FillOneHot(SuperArray src, int labelCount, int[] labels)
        {
            if (src.DimensionCount != 2)
            {
                throw new InvalidOperationException("result must be a 2D array");
            }
            if (src.Shape[0] != labels.Length)
            {
                throw new InvalidOperationException("first dimension of result must equal the number of samples");
            }
            if (src.Shape[1] > labelCount)
            {
                throw new InvalidOperationException("second dimension of result must be at least as large as labelCount");
            }

            for (int i = 0; i < labels.Length; ++i)
            {
                if (labels[i] < 0 || labels[i] >= labelCount)
                {
                    throw new InvalidOperationException("label at index " + i + " is out of range 0 <= x < labelCount");
                }

                src.SetElementAsFloat(1.0f, i, labels[i]);
            }
        }
Example #9
0
        public static SuperArray Constant(float value, DType dtype, params long[] sizes)
        {
            SuperArray array = new SuperArray(sizes, dtype);

            Global.OP.Fill(array, value);
            return(array);
        }
Example #10
0
        /// <summary>
        /// Formats the size of the array type and.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <returns>System.String.</returns>
        public static string FormatTensorTypeAndSize(SuperArray array)
        {
            var result = new StringBuilder();

            result
            .Append("[Type: ")
            .Append(array.ElementType);

            if (array.DimensionCount == 0)
            {
                result.Append(" 0 Dimension");
            }
            else
            {
                result
                .Append(", Shape: ")
                .Append(array.Shape[0]);

                for (int i = 1; i < array.DimensionCount; ++i)
                {
                    result.Append("x").Append(array.Shape[i]);
                }
            }

            result.Append("]");
            return(result.ToString());
        }
Example #11
0
        /// <summary>Broadcasts the tensor.</summary>
        /// <param name="lhs">The LHS.</param>
        /// <param name="rhs">The RHS.</param>
        /// <returns></returns>
        public Tuple <SuperArray, SuperArray> BroadcastTensor(SuperArray lhs, SuperArray rhs)
        {
            if (!lhs.IsSameSizeAs(rhs))
            {
                if (lhs.Shape[0] == rhs.Shape[0] && (lhs.Shape[1] == 1 || rhs.Shape[1] == 1))
                {
                    if (lhs.Shape[1] == 1)
                    {
                        lhs = lhs.RepeatTensor(1, rhs.Shape[1]);
                    }

                    if (rhs.Shape[1] == 1)
                    {
                        rhs = rhs.RepeatTensor(1, lhs.Shape[1]);
                    }
                }

                if (lhs.Shape[1] == rhs.Shape[1] && (lhs.Shape[0] == 1 || rhs.Shape[0] == 1))
                {
                    if (lhs.Shape[0] == 1)
                    {
                        lhs = lhs.RepeatTensor(rhs.Shape[0], 1);
                    }

                    if (rhs.Shape[0] == 1)
                    {
                        rhs = rhs.RepeatTensor(lhs.Shape[0], 1);
                    }
                }

                if (lhs.Shape[1] == 1 && rhs.Shape[0] == 1)
                {
                    if (lhs.Shape[1] == 1)
                    {
                        lhs = lhs.RepeatTensor(1, rhs.Shape[1]);
                    }

                    if (rhs.Shape[0] == 1)
                    {
                        rhs = rhs.RepeatTensor(lhs.Shape[0], 1);
                    }
                }

                if (lhs.Shape[0] == 1 || rhs.Shape[1] == 1)
                {
                    if (lhs.Shape[0] == 1)
                    {
                        lhs = lhs.RepeatTensor(rhs.Shape[0], 1);
                    }

                    if (rhs.Shape[1] == 1)
                    {
                        rhs = rhs.RepeatTensor(1, lhs.Shape[1]);
                    }
                }
            }

            return(new Tuple <SuperArray, SuperArray>(lhs, rhs));
        }
Example #12
0
        public static SuperArray Var(SuperArray arr, uint dim, bool isbiased = false)
        {
            AFArray y = CreateMajorSupportedArray(arr);
            IntPtr  ptr;

            Internal.VERIFY(AFStatistics.af_var(out ptr, y._ptr, isbiased, dim));
            return(new SuperArray(new AFArray(ptr)));
        }
Example #13
0
        public static SuperArray StdDev(SuperArray arr, uint dim)
        {
            AFArray y = CreateMajorSupportedArray(arr);
            IntPtr  ptr;

            Internal.VERIFY(AFStatistics.af_stdev(out ptr, y._ptr, dim));
            return(new SuperArray(new AFArray(ptr)));
        }
Example #14
0
        public static SuperArray Min(SuperArray arr, uint dim)
        {
            AFArray y = CreateMajorSupportedArray(arr);
            IntPtr  ptr;

            Internal.VERIFY(AFAlgorithm.af_min(out ptr, y._ptr, (int)dim));
            return(new SuperArray(new AFArray(ptr)));
        }
Example #15
0
        /// <summary>
        /// Formats the array.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="array">The array.</param>
        private static void FormatTensor(StringBuilder builder, SuperArray array)
        {
            var storageFormat = GetStorageFormat(array.Storage, array);
            var format        = storageFormat.Item1;
            var scale         = storageFormat.Item2;
            var sz            = storageFormat.Item3;

            var  startingLength = builder.Length;
            var  counter        = Enumerable.Repeat((long)0, array.DimensionCount - 2).ToArray();
            bool finished       = false;

            counter[0] = -1;
            while (true)
            {
                for (int i = 0; i < array.DimensionCount - 2; ++i)
                {
                    counter[i]++;
                    if (counter[i] >= array.Shape[i])
                    {
                        if (i == array.DimensionCount - 3)
                        {
                            finished = true;
                            break;
                        }
                        counter[i] = 0;
                    }
                    else
                    {
                        break;
                    }
                }

                if (finished)
                {
                    break;
                }

                if (builder.Length - startingLength > 1)
                {
                    builder.AppendLine();
                }

                builder.Append('(');
                var tensorCopy = array.CopyRef();
                for (int i = 0; i < array.DimensionCount - 2; ++i)
                {
                    var newCopy = tensorCopy.Select(0, counter[i]);
                    tensorCopy.Dispose();
                    tensorCopy = newCopy;
                    builder.Append(counter[i]).Append(',');
                }

                builder.AppendLine(".,.) = ");
                FormatMatrix(builder, tensorCopy, " ");

                tensorCopy.Dispose();
            }
        }
Example #16
0
        public static SuperArray TopK(SuperArray arr, int k, uint dim, int order)
        {
            AFArray y = CreateMajorSupportedArray(arr);
            IntPtr  ptr;
            IntPtr  idx;

            Internal.VERIFY(AFStatistics.af_topk(out ptr, out idx, y._ptr, k, (int)dim, order));
            return(new SuperArray(new AFArray(idx)));
        }
Example #17
0
        public static SuperArray ArgMin(SuperArray arr)
        {
            AFArray y = CreateMajorSupportedArray(arr, false);
            IntPtr  ptr;
            IntPtr  idx;

            Internal.VERIFY(AFStatistics.af_topk(out ptr, out idx, y._ptr, 1, 0, 1));
            return(new SuperArray(new AFArray(idx)));
        }
Example #18
0
        /// <summary>
        /// Matcheses the requirements.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="requireContiguous">if set to <c>true</c> [require contiguous].</param>
        /// <param name="requiredSizes">The required sizes.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        private static bool MatchesRequirements(SuperArray array, bool requireContiguous, params long[] requiredSizes)
        {
            if (requireContiguous && !array.IsContiguous())
            {
                return(false);
            }

            return(ArrayEqual(array.Shape, requiredSizes));
        }
Example #19
0
 /// <summary>
 /// Ases the contiguous.
 /// </summary>
 /// <param name="src">The source.</param>
 /// <returns>NDArray.</returns>
 public static SuperArray AsContiguous(SuperArray src)
 {
     if (src.IsContiguous())
     {
         return(src.CopyRef());
     }
     else
     {
         return(NewContiguous(src));
     }
 }
Example #20
0
        /// <summary>
        /// Tiles the specified tensor.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="repetitions">The repetitions.</param>
        /// <returns></returns>
        public SuperArray Tile(SuperArray x, long repetitions)
        {
            long[] shape = new long[x.DimensionCount];
            for (int i = 0; i < shape.Length; i++)
            {
                shape[i] = 1;
            }

            shape[shape.Length - 1] = repetitions;

            return(x.RepeatTensor(shape));
        }
Example #21
0
        public static SuperArray Max(SuperArray arr, uint[] dims)
        {
            AFArray y = CreateMajorSupportedArray(arr);

            IntPtr ptr = IntPtr.Zero;

            foreach (int dim in dims)
            {
                Internal.VERIFY(AFAlgorithm.af_max(out ptr, y._ptr, dim));
                y._ptr = ptr;
            }

            return(new SuperArray(new AFArray(ptr)));
        }
Example #22
0
        public static SuperArray Mean(SuperArray arr, uint[] dims)
        {
            AFArray y = CreateMajorSupportedArray(arr);

            IntPtr ptr = IntPtr.Zero;

            foreach (var dim in dims)
            {
                Internal.VERIFY(AFStatistics.af_mean(out ptr, y._ptr, dim));
                y._ptr = ptr;
            }

            return(new SuperArray(new AFArray(ptr)));
        }
Example #23
0
        /// <summary>
        /// Gets the storage format.
        /// </summary>
        /// <param name="storage">The storage.</param>
        /// <param name="array">The array.</param>
        /// <returns>Tuple<System.String, System.Double, System.Int32&gt;.</returns>
        private static Tuple <string, double, int> GetStorageFormat(Storage storage, SuperArray array)
        {
            if (storage.ElementCount == 0)
            {
                return(Tuple.Create("", 1.0, 0));
            }

            bool intMode = IsIntOnly(storage, array);
            var  minMax  = AbsMinMax(storage, array);

            var formatSize   = GetFormatSize(minMax, intMode);
            var formatString = BuildFormatString(formatSize.Item1, formatSize.Item3);

            return(Tuple.Create("{0:" + formatString + "}", formatSize.Item2, formatSize.Item3));
        }
Example #24
0
        public static SuperArray RandomGeometric <T>(Shape shape, float p, int?seed = null) where T : struct
        {
            if (seed.HasValue)
            {
                Data.RandSeed = (ulong)seed.Value;
            }
            else
            {
                Data.RandSeed = (ulong)new Random().Next(1, 1000);
            }

            SuperArray result = Data.RandUniform <T>(shape.Dims);

            return(Ops.Log(1 - result) / (float)Math.Log(p) + 1);
        }
Example #25
0
        public static SuperArray RandomCauchy <T>(Shape shape, float median, float sigma, int?seed = null) where T : struct
        {
            if (seed.HasValue)
            {
                Data.RandSeed = (ulong)seed.Value;
            }
            else
            {
                Data.RandSeed = (ulong)new Random().Next(1, 1000);
            }

            SuperArray result = Data.RandUniform <T>(shape.Dims);

            return((median + sigma) * Ops.Tan((float)Math.PI * (result - 0.5f)));
        }
Example #26
0
        public static SuperArray RandomExponential <T>(Shape shape, float lambda, int?seed = null) where T : struct
        {
            if (seed.HasValue)
            {
                Data.RandSeed = (ulong)seed.Value;
            }
            else
            {
                Data.RandSeed = (ulong)new Random().Next(1, 1000);
            }

            SuperArray result = Data.RandUniform <T>(shape.Dims);

            return((-1 / lambda) * Ops.Log(1 - result));
        }
Example #27
0
        public static SuperArray RandomBernoulli <T>(Shape shape, float p, int?seed = null) where T : struct
        {
            if (seed.HasValue)
            {
                Data.RandSeed = (ulong)seed.Value;
            }
            else
            {
                Data.RandSeed = (ulong)new Random().Next(1, 1000);
            }

            SuperArray result = Data.RandUniform <T>(shape.Dims);

            return(result <= p);
        }
Example #28
0
        public static SuperArray Arange(float start, float stop, int step = 1)
        {
            SuperArray   result = null;
            List <float> data   = new List <float>();

            while (start < stop)
            {
                data.Add(start);
                start += step;
            }

            result = new SuperArray(1, data.Count);
            result.LoadFrom(data.ToArray());
            return(result);
        }
Example #29
0
        public static SuperArray Tile(SuperArray x, int nTimes, int axis = 0)
        {
            uint[] dims = new uint[x.Shape.Length];

            for (int i = 0; i < dims.Length; i++)
            {
                if (i == axis)
                {
                    dims[i] = (uint)nTimes;
                    continue;
                }

                dims[i] = 1;
            }

            return(Data.Tile(x.variable, dims));
        }
Example #30
0
        /// <summary>
        /// Determines whether [is int only] [the specified storage].
        /// </summary>
        /// <param name="storage">The storage.</param>
        /// <param name="array">The array.</param>
        /// <returns><c>true</c> if [is int only] [the specified storage]; otherwise, <c>false</c>.</returns>
        private static bool IsIntOnly(Storage storage, SuperArray array)
        {
            // HACK this is a hacky way of iterating over the elements of the array.
            // if the array has holes, this will incorrectly include those elements
            // in the iteration.
            var minOffset = array.StorageOffset;
            var maxOffset = minOffset + Helper.GetStorageSize(array.Shape, array.Strides) - 1;

            for (long i = minOffset; i <= maxOffset; ++i)
            {
                var value = Convert.ToDouble((object)storage.GetElementAsFloat(i));
                if (value != Math.Ceiling(value))
                {
                    return(false);
                }
            }

            return(true);
        }