Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Clojure] Helper function for n-dim vector to ndarray #14305

Merged
merged 4 commits into from
Mar 11, 2019

Conversation

kedarbellare
Copy link
Contributor

Description

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • ->ndarray with tests and docs

@gigasquid
Copy link
Member

Thanks. This looks super 👍 - I'll check it out in closer detail shortly, (and find out why the CI seems stuck)

Copy link
Contributor

@Chouffe Chouffe left a 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.
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@kedarbellare kedarbellare Mar 4, 2019

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.

Copy link
Member

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:
Copy link
Contributor

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))
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@anirudhacharya
Copy link
Member

@mxnet-label-bot add [pr-awaiting-review, Clojure]

@marcoabreu marcoabreu added Clojure pr-awaiting-review PR is waiting for code review labels Mar 4, 2019
@kedarbellare
Copy link
Contributor Author

@Chouffe : having an ->nd-vec could be potentially useful although the recent (str ndarray) is much clearer for debugging (#14272). do you want to submit a PR for ->nd-vec?

@Chouffe
Copy link
Contributor

Chouffe commented Mar 4, 2019

@kedarbellare here is the PR - WIP I need to add the unit tests
#14308
I'd love your feedback

@gigasquid
Copy link
Member

taking a look 👀

Copy link
Member

@gigasquid gigasquid left a 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")
Copy link
Member

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.
Copy link
Member

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recursion for the win! 🥇

@kedarbellare
Copy link
Contributor Author

@gigasquid @Chouffe found a better way! PTAL 😃

Copy link
Member

@gigasquid gigasquid left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@gigasquid gigasquid removed the pr-awaiting-review PR is waiting for code review label Mar 10, 2019
@gigasquid gigasquid merged commit a4b9802 into apache:master Mar 11, 2019
vdantu pushed a commit to vdantu/incubator-mxnet that referenced this pull request Mar 31, 2019
* [Clojure] Helper function for n-dim vector to ndarray

* More tests, specs and rename method

* Address comments

* Allow every number type
nswamy pushed a commit that referenced this pull request Apr 5, 2019
* [Clojure] Helper function for n-dim vector to ndarray

* More tests, specs and rename method

* Address comments

* Allow every number type
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* [Clojure] Helper function for n-dim vector to ndarray

* More tests, specs and rename method

* Address comments

* Allow every number type
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants