예제 #1
0
 public Tensor(IAllocator allocator, DType elementType, long[] sizes, long[] strides)
 {
     this.sizes         = sizes;
     this.strides       = strides;
     this.storageOffset = 0;
     this.storage       = allocator.Allocate(elementType, TensorDimensionHelpers.GetStorageSize(sizes, strides));
 }
예제 #2
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, Tensor 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)));
        }
예제 #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, Tensor 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);
        }
예제 #4
0
 public long GetStorageSize()
 {
     return(TensorDimensionHelpers.GetStorageSize(sizes, strides));
 }