Kotlin / multik

Multidimensional array library for Kotlin
https://kotlin.github.io/multik/
Apache License 2.0
646 stars 39 forks source link

Add `pad` function #149

Open AlexandreBrown opened 1 year ago

AlexandreBrown commented 1 year ago

I would like to know how to pad using createAlignedNDArray.
From my understanding this function takes a list of lists and makes every list the max length and pads the remaining with zeros or whatever value you want (for each dim).
image

Example:
Let P be the size of each dims we want our final matrix to be with padding.
Let W and H be 2 sizes (different) that are less than P.
For the sake of this example let's say we have W< H < P.
Let a be an mk array of shape (W, H). Then applying createAlignedNDArray(a.toListD3(), 0.0) would give us a matrix of shape (H, H) padded with zeros.

While the name of the function seems correct, I'm interested in knowing if it's possible to use this function to transoform a into a matrix of size (P, P) instead.

Something like, we provide an mk array and can specify the desired padding for each dimensions.
See https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html

If it's possible with createAlignedNDArray then feel free to let me know.

devcrocod commented 1 year ago

In your example where a is shaped (W, H), using createAlignedNDArray(a.toListD3(), 0.0) will give the same shape (W, H). ndarray in multik is aligned, i.e. all sizes at dimension 0 will be the same. Unfortunately, it will not work to get the shape (P, P). This can be done by converting the output lists to a mutableList and expanding one of them to size P.

I had a request for this function, having figured it out, I called it createAlignedNDArray, since this is not a full-fledged pad.

In that case, I'll try to add the classic pad.