Пример #1
0
        /// <summary>
        /// Abses the minimum maximum.
        /// </summary>
        /// <param name="storage">The storage.</param>
        /// <param name="tensor">The tensor.</param>
        /// <returns>Tuple&lt;System.Double, System.Double&gt;.</returns>
        private static Tuple <double, double> AbsMinMax(Storage storage, NDArray tensor)
        {
            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 tensor.
            // if the tensor has holes, this will incorrectly include those elements
            // in the iteration.
            var minOffset = tensor.StorageOffset;
            var maxOffset = minOffset + TensorDimensionHelpers.GetStorageSize(tensor.Shape, tensor.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)));
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NDArray"/> class.
 /// </summary>
 /// <param name="allocator">The allocator.</param>
 /// <param name="elementType">Type of the element.</param>
 /// <param name="sizes">The sizes.</param>
 /// <param name="strides">The strides.</param>
 public NDArray(IAllocator allocator, DType elementType, long[] sizes, long[] strides)
 {
     this.shape         = sizes;
     this.strides       = strides;
     this.storageOffset = 0;
     this.storage       = allocator.Allocate(elementType, TensorDimensionHelpers.GetStorageSize(sizes, strides));
 }
Пример #3
0
        /// <summary>
        /// Determines whether [is int only] [the specified storage].
        /// </summary>
        /// <param name="storage">The storage.</param>
        /// <param name="tensor">The tensor.</param>
        /// <returns><c>true</c> if [is int only] [the specified storage]; otherwise, <c>false</c>.</returns>
        private static bool IsIntOnly(Storage storage, NDArray tensor)
        {
            // HACK this is a hacky way of iterating over the elements of the tensor.
            // if the tensor has holes, this will incorrectly include those elements
            // in the iteration.
            var minOffset = tensor.StorageOffset;
            var maxOffset = minOffset + TensorDimensionHelpers.GetStorageSize(tensor.Shape, tensor.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);
        }