Class SingleElementSequence<T, U extends NdArray<T>>
- Type Parameters:
T- Type of the elementU- Type of theNdArraywith this sequence
- All Implemented Interfaces:
Iterable<U>, NdArraySequence<U>
-
Constructor Summary
ConstructorsConstructorDescriptionSingleElementSequence(org.tensorflow.ndarray.impl.AbstractNdArray<T, U> ndArray) -
Method Summary
Modifier and TypeMethodDescriptionasSlices()Returns each element as a new slice.voidforEachIndexed(BiConsumer<long[], U> consumer) Visit each elements of this iteration and their respective coordinates.iterator()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
SingleElementSequence
-
-
Method Details
-
iterator
-
asSlices
Description copied from interface:NdArraySequenceReturns each element as a new slice.Unlike conventional Java collections, elements of a
NdArraySequenceare transient, i.e. newNdArrayinstances are allocated for each iteration. To improve performance, the same instance can be recycled to view all elements of this sequence, using aDataBufferWindow.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:
asSlicesin interfaceNdArraySequence<T>- Returns:
- a sequence that returns each elements iterated as a new slice
- See Also:
-
forEachIndexed
Description copied from interface:NdArraySequenceVisit 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:
forEachIndexedin interfaceNdArraySequence<T>- Parameters:
consumer- method to invoke for each elements
-