java-ndarray icon indicating copy to clipboard operation
java-ndarray copied to clipboard

NdArray Initializers API

Open karllessard opened this issue 3 years ago • 1 comments

This draft shows how we could hydrate a just-allocated array using a new API that focuses on a stateful builder approach rather than the positional endpoints setXXX(value, coords) exposed by the NdArray itself.

Not only it simplifies the task of initializing an array with explicit values but also it allows us to allocate an empty sparse array and then let the user initialize its data (right now, the user have to allocate and initialize the data of the indices and values subarrays first before being able to allocate a sparse array).

Example of usage:

NdArray<String> array = NdArrays.ofObjects(String.class, Shape.of(3, 2), initializer -> {
     initializer.byVectors()
         .put("Cat", "Dog")
         .put("House") // partial initialization
         .to(2)
         .put("Orange", "Apple");
});
// -> [["Cat", "Dog"], ["House", null], ["Orange", "Apple"]]

Note: this is a newer version of https://github.com/tensorflow/java-ndarray/pull/4

karllessard avatar Dec 05 '22 13:12 karllessard

@Craigacp : This is still in draft mode as it is missing most of the datatypes, right now only arrays of Double and Objects (like String) are being demonstrated. Once we are satisfied, I'll update it for all other missing types and turn it to a real PR.

I couldn't unfortunately reopen the previous version of the PR as I forced-push it while it was closed and GitHub does not like that. Still, I replied and addressed your comments you put in there and we can continue the discussion here for the few remaining points.

karllessard avatar Dec 05 '22 13:12 karllessard