// 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> /// 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> /// 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)) { }