/// <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, 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))); }
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)); }
// Prepend singleton dimensions until DimensionCount equals newDimCount private Tensor PadToDimCount(int newDimCount) { long[] newSizes = Pad1Prepend(sizes, newDimCount); long[] newStrides = TensorDimensionHelpers.GetContiguousStride(newSizes); Array.Copy(strides, 0, newStrides, newStrides.Length - strides.Length, strides.Length); return(new Tensor(newSizes, newStrides, storage, storageOffset)); }
// Prepend singleton dimensions until DimensionCount equals newDimCount private Tensor PadToDimCount(int newDimCount) { var newSizes = Pad1Prepend(this.sizes, newDimCount); var newStrides = TensorDimensionHelpers.GetContiguousStride(newSizes); Array.Copy(this.strides, 0, newStrides, newStrides.Length - this.strides.Length, this.strides.Length); return(new Tensor(newSizes, newStrides, this.storage, this.storageOffset)); }
public long ElementCount() { if (elementCount.HasValue) { return(elementCount.Value); } elementCount = TensorDimensionHelpers.ElementCount(sizes); return(elementCount.Value); }
public long ElementCount() { if (this.elementCount.HasValue) { return(this.elementCount.Value); } this.elementCount = TensorDimensionHelpers.ElementCount(this.Sizes); return(this.elementCount.Value); }
public Tensor 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 Tensor(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, 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); }
/// <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> public Tensor(IAllocator allocator, DType elementType, params long[] sizes) : this(allocator, elementType, sizes, TensorDimensionHelpers.GetContiguousStride(sizes)) { }
public long GetStorageSize() { return(TensorDimensionHelpers.GetStorageSize(sizes, strides)); }