Class SlicingElementSequence<T,​U extends NdArray<T>>

  • Type Parameters:
    T - Type of the element
    U - Type of the NdArray with this sequence
    All Implemented Interfaces:
    Iterable<U>, NdArraySequence<U>

    public final class SlicingElementSequence<T,​U extends NdArray<T>>
    extends Object
    implements NdArraySequence<U>
    A sequence creating a new NdArray instance (slice) for each element of an iteration
    • Constructor Detail

      • SlicingElementSequence

        public SlicingElementSequence​(org.tensorflow.ndarray.impl.AbstractNdArray<T,​U> ndArray,
                                      int dimensionIdx)
      • SlicingElementSequence

        public SlicingElementSequence​(org.tensorflow.ndarray.impl.AbstractNdArray<T,​U> ndArray,
                                      int dimensionIdx,
                                      DimensionalSpace elementDimensions)
    • Method Detail

      • forEachIndexed

        public void forEachIndexed​(BiConsumer<long[],​U> consumer)
        Description copied from interface: NdArraySequence
        Visit each elements of this iteration and their respective coordinates.

        Important: the consumer method should not keep a reference to the coordinates as they might be mutable and reused during the iteration to improve performance.

        Specified by:
        forEachIndexed in interface NdArraySequence<T>
        Parameters:
        consumer - method to invoke for each elements
      • asSlices

        public NdArraySequence<U> asSlices()
        Description copied from interface: NdArraySequence
        Returns each element as a new slice.

        Unlike conventional Java collections, elements of a NdArraySequence are transient, i.e. new NdArray instances are allocated for each iteration. To improve performance, the same instance can be recycled to view all elements of this sequence, using a DataBufferWindow.

        In some cases though, it might be preferable to disable such optimizations to ensure that each element returned is a new slice of the original array. For example, if one or more elements visited must live beyond the scope of the sequence iteration, asSlices() makes sure that all elements returned by the sequence are unique instances.

        
             final List<IntNdArray> vectors = new ArrayList<>();
             IntNdArray matrix = NdArrays.ofInts(Shape.of(6, 6));
             ndArray.elements(0).forEach(e -> vectors::add);  // Not safe, as `e` might always be the same recycled instance
             ndArray.elements(0).asSlices().forEach(e -> vectors::add);  // Safe, each `e` is a distinct NdArray instance
         
        Specified by:
        asSlices in interface NdArraySequence<T>
        Returns:
        a sequence that returns each elements iterated as a new slice
        See Also:
        DataBufferWindow