/// <summary> /// Abses the minimum maximum. /// </summary> /// <param name="storage">The storage.</param> /// <param name="tensor">The tensor.</param> /// <returns>Tuple<System.Double, System.Double>.</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))); }
/// <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)); }
// Prepend singleton dimensions until DimensionCount equals newDimCount /// <summary> /// Pads to dim count. /// </summary> /// <param name="newDimCount">The new dim count.</param> /// <returns>Tensor.</returns> private NDArray PadToDimCount(int newDimCount) { var newSizes = Pad1Prepend(this.shape, newDimCount); var newStrides = TensorDimensionHelpers.GetContiguousStride(newSizes); Array.Copy(this.strides, 0, newStrides, newStrides.Length - this.strides.Length, this.strides.Length); return(new NDArray(newSizes, newStrides, this.storage, this.storageOffset)); }
/// <summary> /// Elements the count. /// </summary> /// <returns>System.Int64.</returns> public long ElementCount() { if (elementCount.HasValue) { return(elementCount.Value); } elementCount = TensorDimensionHelpers.ElementCount(shape); return(elementCount.Value); }
/// <summary> /// Views the specified sizes. /// </summary> /// <param name="sizes">The sizes.</param> /// <returns>Tensor.</returns> /// <exception cref="InvalidOperationException"> /// Cannot use View on a non-contiguous tensor000 /// or /// Output tensor must have the same number of elements as the input /// </exception> public NDArray View(params long[] sizes) { if (!this.IsContiguous()) { throw new InvalidOperationException("Cannot use View on a non-contiguous tensor"); } if (this.ElementCount() != TensorDimensionHelpers.ElementCount(sizes)) { throw new InvalidOperationException("Output tensor must have the same number of elements as the input"); } return(new NDArray(sizes, TensorDimensionHelpers.GetContiguousStride(sizes), this.storage, this.storageOffset)); }
/// <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); }
/// <summary> /// Construct a new tensor, using the given allocator to construct a storage. The new tensor /// will be contiguous in memory. The tensor's elements will not be initialized. /// </summary> /// <param name="allocator"></param> /// <param name="elementType"></param> /// <param name="sizes"></param> /// <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> public NDArray(IAllocator allocator, DType elementType, params long[] sizes) : this(allocator, elementType, sizes, TensorDimensionHelpers.GetContiguousStride(sizes)) { }