This repository was archived by the owner on Jul 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
NdArray Initializers API #14
Draft
karllessard
wants to merge
3
commits into
tensorflow:main
Choose a base branch
from
karllessard:initializers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Temp work for hydrators
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 151 additions & 9 deletions
160
ndarray/src/main/java/org/tensorflow/ndarray/hydrator/DoubleNdArrayHydrator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,172 @@ | ||
| package org.tensorflow.ndarray.hydrator; | ||
|
|
||
| import org.tensorflow.ndarray.DoubleNdArray; | ||
| import org.tensorflow.ndarray.NdArray; | ||
| import org.tensorflow.ndarray.Shape; | ||
|
|
||
| public interface DoubleNdArrayHydrator extends NdArrayHydrator<Double> { | ||
| /** | ||
| * Specialization of the {@link NdArrayHydrator} API for hydrating arrays of doubles. | ||
| * | ||
| * @see NdArrayHydrator | ||
| */ | ||
| public interface DoubleNdArrayHydrator { | ||
|
|
||
| interface Scalars extends NdArrayHydrator.Scalars<Double> { | ||
| /** | ||
| * An API for hydrate an {@link DoubleNdArray} using scalar values | ||
| */ | ||
| interface Scalars { | ||
|
|
||
| @Override | ||
| /** | ||
| * Position the hydrator to the given {@code coordinates} to write the next scalars. | ||
| * | ||
| * @param coordinates position in the hydrated array | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code coordinates} are empty or are not one of a scalar | ||
| */ | ||
| Scalars at(long... coordinates); | ||
|
|
||
| /** | ||
| * Set a double value as the next scalar value in the hydrated array. | ||
| * | ||
| * @param scalar next scalar value | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code scalar} is null | ||
| */ | ||
| Scalars put(double scalar); | ||
| } | ||
|
|
||
| interface Vectors extends NdArrayHydrator.Vectors<Double> { | ||
| /** | ||
| * An API for hydrate an {@link DoubleNdArray} using vectors, i.e. a list of scalars | ||
| */ | ||
| interface Vectors { | ||
|
|
||
| @Override | ||
| /** | ||
| * Position the hydrator to the given {@code coordinates} to write the next vectors. | ||
| * | ||
| * @param coordinates position in the hydrated array | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code coordinates} are empty or are not one of a vector | ||
| */ | ||
| Vectors at(long... coordinates); | ||
|
|
||
| /** | ||
| * Set a list of doubles as the next vector in the hydrated array. | ||
| * | ||
| * @param vector next vector values | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code vector} is empty or its length is greater than the size of the dimension | ||
| * {@code n-1}, given {@code n} the rank of the hydrated array | ||
| */ | ||
| Vectors put(double... vector); | ||
| } | ||
|
|
||
| @Override | ||
| /** | ||
| * An API for hydrate an {@link DoubleNdArray} using n-dimensional elements (sub-arrays). | ||
| */ | ||
| interface Elements { | ||
|
|
||
| /** | ||
| * Position the hydrator to the given {@code coordinates} to write the next elements. | ||
| * | ||
| * @param coordinates position in the hydrated array | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code coordinates} are empty or are not one of an element of the hydrated array | ||
| */ | ||
| Elements at(long... coordinates); | ||
|
|
||
| /** | ||
| * Set a n-dimensional array of doubles as the next element in the hydrated array. | ||
| * | ||
| * @param element array containing the next element values | ||
| * @return this API | ||
| * @throws IllegalArgumentException if {@code element} is null or its shape is incompatible with the current hydrator position | ||
| */ | ||
| Elements put(DoubleNdArray element); | ||
| } | ||
|
|
||
| /** | ||
| * Start to hydrate the targeted array with scalars. | ||
| * | ||
| * If no {@code coordinates} are provided, the start position is the current one relatively to any previous hydration that occured or if none, | ||
| * defaults to the first scalar of this array. | ||
| * | ||
| * Example of usage: | ||
| * <pre>{@code | ||
| * DoubleNdArray array = NdArrays.ofDoubles(Shape.of(3, 2), hydrator -> { | ||
| * hydrator.byScalars() | ||
| * .put(10.0) | ||
| * .put(20.0) | ||
| * .put(30.0) | ||
| * .at(2, 1) | ||
| * .put(40.0); | ||
| * }); | ||
| * // -> [[10.0, 20.0], [30.0, 0.0], [0.0, 40.0]] | ||
| * }</pre> | ||
| * | ||
| * @param coordinates position in the hydrated array to start from | ||
| * @return a {@link Scalars} instance | ||
| * @throws IllegalArgumentException if {@code coordinates} are set but are not one of a scalar | ||
| */ | ||
| Scalars byScalars(long... coordinates); | ||
|
|
||
| @Override | ||
| /** | ||
| * Start to hydrate the targeted array with vectors. | ||
| * | ||
| * If no {@code coordinates} are provided, the start position is the current one relatively to any previous hydration that occured or if none, | ||
| * defaults to the first scalar of the first vector of this array. | ||
| * | ||
| * Example of usage: | ||
| * <pre>{@code | ||
| * DoubleNdArray array = NdArrays.ofDoubles(Shape.of(3, 2), hydrator -> { | ||
| * hydrator.byVectors() | ||
| * .put(10.0, 20.0) | ||
| * .put(30.0) | ||
| * .at(2) | ||
| * .put(40.0, 50.0); | ||
| * }); | ||
| * // -> [[10.0, 20.0], [30.0, null], [40.0, 50.0]] | ||
| * }</pre> | ||
| * | ||
| * @param coordinates position in the hydrated array to start from | ||
| * @return a {@link Vectors} instance | ||
| * @throws IllegalArgumentException if hydrated array is of rank-0 or if {@code coordinates} are set but are not one of a vector | ||
| */ | ||
| Vectors byVectors(long... coordinates); | ||
|
|
||
| /** | ||
| * Start to hydrate the targeted array with n-dimensional elements. | ||
| * | ||
| * If no {@code coordinates} are provided, the start position is the current one relatively to any previous hydration that occured or if none, | ||
| * defaults to the first element in the first (0) dimension of the hydrated array. | ||
| * | ||
| * Example of usage: | ||
| * <pre>{@code | ||
| * DoubleNdArray vector = NdArrays.vectorOf(10.0, 20.0); | ||
| * DoubleNdArray scalar = NdArrays.scalarOf(30.0); | ||
| * | ||
| * DoubleNdArray array = NdArrays.ofDoubles(Shape.of(4, 2), hydrator -> { | ||
| * hydrator.byElements() | ||
| * .put(vector) | ||
| * .put(vector) | ||
| * .at(2, 1) | ||
| * .put(scalar) | ||
| * .at(3) | ||
| * .put(vector); | ||
| * }); | ||
| * // -> [[10.0, 20.0], [10.0, 20.0], [0.0, 30.0], [10.0, 20.0]] | ||
| * }</pre> | ||
| * | ||
| * @param coordinates position in the hydrated array to start from | ||
| * @return a {@link Elements} instance | ||
| * @throws IllegalArgumentException if {@code coordinates} are set but are not one of an element of the hydrated array | ||
| */ | ||
| Elements byElements(long... coordinates); | ||
|
|
||
| /** | ||
| * Creates an API to hydrate the targeted array with {@code Double} boxed type. | ||
| * | ||
| * Note that sticking to primitive types improve I/O performances overall, so only rely boxed types if the data is already | ||
| * available in that format. | ||
| * | ||
| * @return a hydrator supporting {@code Double} boxed type | ||
| */ | ||
| NdArrayHydrator<Double> boxed(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This says "hydrating" and the one above says "initializing". Similarly for the rest of the javadoc in this class, and also the parameter names.