haojin2 commented on a change in pull request #14758: [WIP] Infra for supporting numpy ops in imperative mode and Gluon APIs URL: https://github.com/apache/incubator-mxnet/pull/14758#discussion_r277447351
########## File path: python/mxnet/numpy/multiarray.py ########## @@ -0,0 +1,165 @@ +#!/usr/bin/env python + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +from __future__ import absolute_import +import numpy as _np +import ctypes +from ..ndarray import NDArray, _new_alloc_handle +from ..ndarray._internal import _set_np_ndarray_class +from . import _op +from ..base import use_np_compat, check_call, _LIB, NDArrayHandle, _sanity_check_params +from ..context import current_context +from ..ndarray import numpy as _mx_nd_np + + +__all__ = ['ndarray', 'empty', 'array', 'zeros'] + + +def _np_ndarray_cls(handle, writable=True): + return ndarray(handle, writable=writable) + + +_set_np_ndarray_class(_np_ndarray_cls) + + +class ndarray(NDArray): + def as_legacy_ndarray(self): + """Convert mxnet.numpy.ndarray to mxnet.ndarray.NDArray to use its fluent methods.""" + hdl = NDArrayHandle() + check_call(_LIB.MXShallowCopyNDArray(self.handle, ctypes.byref(hdl))) + return NDArray(handle=hdl, writable=self.writable) + + @use_np_compat + def __repr__(self): + """Returns a string representation of the array.""" + return '%s\n<%s shape=%s ctx=%s>' % (str(self.asnumpy()), self.__class__.__name__, + self.shape, self.context) + + @use_np_compat + def sin(self, *args, **kwargs): + """Convenience fluent method for :py:func:`sin`. + + The arguments are the same as for :py:func:`sin`, with + this array as data. + """ + raise NotImplementedError('mxnet.numpy.ndarray.sin is not implemented. Please ' + 'convert the mxnet.numpy.ndarray to mxnet.ndarray.NDArray ' + 'and call the sin function as follows: ' + 'self.as_legacy_ndarray().sin(*args, **kwargs).') Review comment: Maybe refine the error message to be more clear that numpy actually does not have convenient method for basic mathematical calculations? Same for the other convenient ones that already exists. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
