-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[Clojure] Helper function for n-dim vector to ndarray #14305
[Clojure] Helper function for n-dim vector to ndarray #14305
Conversation
Thanks. This looks super 👍 - I'll check it out in closer detail shortly, (and find out why the CI seems stuck) |
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.
do we want to also think about a ->nd-vec
function that would do the opposite operation? Taking an NDArray
and returning a Clojure nd-vec
?
Here is a proposal: Chouffe@282d863
(defn ->ndarray | ||
"Creates a new NDArray based on the given n-dimensional | ||
float/double vector. | ||
`nd-vec`: n-dimensional vector with floats or doubles. |
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.
Do we want to allow also integers here? or other numerical types that can be understood by MXNet?
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.
Maybe the Scala functions will not allow it but we can cast to float/double from clojure?
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.
currently, scala doesn't allow numerical types other than float/double.
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.
usually i've been using ndarray/as-type
for type conversion instead of doing conversion within clojure. my hunch is as-type
is faster but i've not verified this. however, one disadvantage is that it returns a copy of the array in the new type.
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.
From a user perspective, I think it would be nice to be able to handle ((ndarray/->ndarray [5 -4 3])
and not throw an exception. What do you think about adding a check in the util function to see if any element is an integer and if so, convert it to double. Ex:
(if (some int? s)
(to-array (mapv double s))
(to-array s))
`ctx`: Context of the output ndarray, will use default context if unspecified. | ||
} | ||
returns: `ndarray` with the given values and matching the shape of the input vector. | ||
Ex: |
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.
Love your docstring!
[s] | ||
(validate! ::non-empty-seq s "Invalid N-D sequence") | ||
(if (sequential? (first s)) | ||
(to-array (map to-array-nd s)) |
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.
Should we call mapv
? So that thunks for the lazy sequence are not built up?
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.
done
@mxnet-label-bot add [pr-awaiting-review, Clojure] |
@kedarbellare here is the PR - WIP I need to add the unit tests |
taking a look 👀 |
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.
Thanks so much for your work on this @kedarbellare. Overall looks great, just a comment on how we can support the user friendly use case of a user putting in int values.
"Converts any N-D sequential structure to an array | ||
with the same dimensions." | ||
[s] | ||
(validate! ::non-empty-seq s "Invalid N-D sequence") |
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.
Thanks for putting this validation in
(defn ->ndarray | ||
"Creates a new NDArray based on the given n-dimensional | ||
float/double vector. | ||
`nd-vec`: n-dimensional vector with floats or doubles. |
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.
From a user perspective, I think it would be nice to be able to handle ((ndarray/->ndarray [5 -4 3])
and not throw an exception. What do you think about adding a check in the util function to see if any element is an integer and if so, convert it to double. Ex:
(if (some int? s)
(to-array (mapv double s))
(to-array s))
[s] | ||
(validate! ::non-empty-seq s "Invalid N-D sequence") | ||
(if (sequential? (first s)) | ||
(to-array (mapv to-array-nd s)) |
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.
recursion for the win! 🥇
@gigasquid @Chouffe found a better way! PTAL 😃 |
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.
Looks great. Will be good to go after CI turns green
(to-array (mapv to-array-nd nd-seq)) | ||
(to-array nd-seq))) | ||
|
||
(defn nd-seq-shape |
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.
Nice!
* [Clojure] Helper function for n-dim vector to ndarray * More tests, specs and rename method * Address comments * Allow every number type
* [Clojure] Helper function for n-dim vector to ndarray * More tests, specs and rename method * Address comments * Allow every number type
* [Clojure] Helper function for n-dim vector to ndarray * More tests, specs and rename method * Address comments * Allow every number type
Description
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.
Changes
->ndarray
with tests and docs