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

make array.reshape compatible with numpy #9790

Merged
merged 4 commits into from
Feb 19, 2018
Merged

Conversation

szha
Copy link
Member

@szha szha commented Feb 13, 2018

Description

Allow n ints as input to array.reshape.

Checklist

Essentials

  • Passed code style checking (make lint)
  • 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
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Allow n ints as input to array.reshape.

@szha szha requested a review from piiswrong February 13, 2018 23:19
@@ -926,12 +926,12 @@ def _at(self, idx):
self.handle, mx_uint(idx), ctypes.byref(handle)))
return NDArray(handle=handle, writable=self.writable)

def reshape(self, shape):
def reshape(self, *shape, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

(self, *args, shape=None)?

Copy link
Member Author

Choose a reason for hiding this comment

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

This doesn't work in py2

@@ -968,6 +973,14 @@ def reshape(self, shape):
array([[-1., -1., -1.],
[-1., -1., -1.]], dtype=float32)
"""
if len(shape) == 1 and isinstance(shape[0], (list, tuple)):
shape = shape[0]
elif not len(shape):
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens for reshape(1, 2, shape=1)?

Copy link
Member Author

Choose a reason for hiding this comment

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

keyword argument is ignored.

Copy link
Member Author

Choose a reason for hiding this comment

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

would that be a problem?

Copy link
Contributor

Choose a reason for hiding this comment

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

an error should be raised

Copy link
Member Author

Choose a reason for hiding this comment

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

OK

Copy link
Member Author

Choose a reason for hiding this comment

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

Numpy throws an error too, though the error is confusing.

In [1]: import numpy as np

In [2]: a = np.ones((3,5))

In [3]: a.reshape(3,5,shape=(7,))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-cac6c36bb3d2> in <module>()
----> 1 a.reshape(3,5,shape=(7,))

TypeError: 'shape' is an invalid keyword argument for this function

if len(shape) == 1 and isinstance(shape[0], (list, tuple)):
shape = shape[0]
elif not len(shape):
for key, value in kwargs.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to iterate. test for len(kwargs) == 1 and kwargs.get('shape', None) directly

Copy link
Member Author

@szha szha Feb 15, 2018

Choose a reason for hiding this comment

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

The loop also helps throw exception with the invalid argument name, which is consistent with numpy's behavior. The check would be lost in your proposal.

if len(shape) == 1 and isinstance(shape[0], (list, tuple)):
shape = shape[0]
elif not shape:
for key, value in kwargs.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

This is slow. Use test for len(kwargs) == 1, then kwargs.get('shape', None)

@piiswrong piiswrong merged commit 9348a3a into apache:master Feb 19, 2018
@szha szha deleted the reshape_nint branch April 26, 2018 18:21
rahul003 pushed a commit to rahul003/mxnet that referenced this pull request Jun 4, 2018
* make array.reshape compatible with numpy

* update

* add exception when both *args and **kwargs are specified

* update
zheng-da pushed a commit to zheng-da/incubator-mxnet that referenced this pull request Jun 28, 2018
* make array.reshape compatible with numpy

* update

* add exception when both *args and **kwargs are specified

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

Successfully merging this pull request may close these issues.

None yet

2 participants