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

[MXNET-145] Remove the dependences of mx.io and mx.initializer on the numpy's global random number generator #10260

Merged
merged 2 commits into from Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/mxnet/initializer.py
Expand Up @@ -530,9 +530,9 @@ def _init_weight(self, _, arr):
nout = arr.shape[0]
nin = np.prod(arr.shape[1:])
if self.rand_type == "uniform":
tmp = np.random.uniform(-1.0, 1.0, (nout, nin))
tmp = random.uniform(-1.0, 1.0, shape=(nout, nin)).asnumpy()
Copy link
Contributor

Choose a reason for hiding this comment

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

are these asnumpys necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Numpy's SVD is applied to tmp array, so the conversions are needed. Currently mxnet seems not have SVD. There is a simple workaround to perform SVD using eigen decomposition (so using syevd), however it is not so efficient.

elif self.rand_type == "normal":
tmp = np.random.normal(0.0, 1.0, (nout, nin))
tmp = random.normal(0.0, 1.0, shape=(nout, nin)).asnumpy()
u, _, v = np.linalg.svd(tmp, full_matrices=False) # pylint: disable=invalid-name
if u.shape == tmp.shape:
res = u
Expand Down
8 changes: 6 additions & 2 deletions python/mxnet/io.py
Expand Up @@ -39,6 +39,8 @@
from .ndarray import _ndarray_cls
from .ndarray import array
from .ndarray import concatenate
from .ndarray import arange
from .ndarray.random import shuffle as random_shuffle

class DataDesc(namedtuple('DataDesc', ['name', 'shape'])):
"""DataDesc is used to store name, shape, type and layout
Expand Down Expand Up @@ -651,12 +653,14 @@ def __init__(self, data, label=None, batch_size=1, shuffle=False,
raise NotImplementedError("`NDArrayIter` only supports ``CSRNDArray``" \
" with `last_batch_handle` set to `discard`.")

self.idx = np.arange(self.data[0][1].shape[0])
# shuffle data
if shuffle:
np.random.shuffle(self.idx)
tmp_idx = arange(self.data[0][1].shape[0], dtype=np.int32)
self.idx = random_shuffle(tmp_idx, out=tmp_idx).asnumpy()
self.data = _shuffle(self.data, self.idx)
self.label = _shuffle(self.label, self.idx)
else:
self.idx = np.arange(self.data[0][1].shape[0])

# batching
if last_batch_handle == 'discard':
Expand Down