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

[MXNET-995] Constant Initializer for ND Array #12677

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python/mxnet/ndarray/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def _sync_copyfrom(self, source_array):
source_array = np.ascontiguousarray(source_array, dtype=self.dtype)
if source_array.shape != self.shape:
raise ValueError('Shape inconsistent: expected %s vs got %s'%(
str(self.shape), str(source_array.shape)))
str(source_array.shape), str(self.shape)))
Copy link
Member

Choose a reason for hiding this comment

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

why switch the order of expected vs got?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have a look at #12676
I found it to be odd. Hence created an issue and fixed it in this PR
Don't you think so?

Copy link
Contributor Author

@ChaiBapchya ChaiBapchya Sep 28, 2018

Choose a reason for hiding this comment

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

@zhreshold could you confirm if the above issue (#12676 ) is right or not? coz the value error it gave sounded confusing due to mismatched shapes. Thanks

check_call(_LIB.MXNDArraySyncCopyFromCPU(
self.handle,
source_array.ctypes.data_as(ctypes.c_void_p),
Expand Down Expand Up @@ -2479,6 +2479,8 @@ def array(source_array, ctx=None, dtype=None):
if isinstance(source_array, NDArray):
dtype = source_array.dtype if dtype is None else dtype
else:
if isinstance(source_array, (float, int)):
Copy link
Member

Choose a reason for hiding this comment

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

mx.base.numeric_types is better

Copy link
Member

Choose a reason for hiding this comment

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

my concern here is that you implicitly converted 0-d array(constant) to 1-d array here, which may not be very appropriate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my logic was since MXNet NDArray needs a 1d array minimum, I should convert constant. Else, is there a way I can do constant initialization support? Would be great to get some help in that direction.

source_array = [float(source_array)]
dtype = mx_real_t if dtype is None else dtype
if not isinstance(source_array, np.ndarray):
try:
Expand Down
18 changes: 18 additions & 0 deletions tests/python/unittest/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,24 @@ def test_dlpack():
mx.test_utils.assert_almost_equal(a_np, d_np)
mx.test_utils.assert_almost_equal(a_np, e_np)

@with_seed()
def test_ndarray_constant_init():
# a=mx.nd.array([1])
a=mx.nd.array(9)
assert(isinstance(a,mx.nd.NDArray))

@with_seed()
def test_symbol_constant_init():
# a=mx.nd.array([1])
a = mx.sym.Variable('a')
b = mx.sym.Variable('b')
c = a * b
d = c.eval(a=mx.nd.array(2),b=mx.nd.array(3))
assert(isinstance(d[0],mx.nd.NDArray))
e = c.bind(ctx=mx.cpu(),args=[mx.nd.array(2),mx.nd.array(3)])
f = e.forward()
assert(isinstance(f[0],mx.nd.NDArray))

if __name__ == '__main__':
import nose
nose.runmodule()