ó ùµÈ[c@sydZddlZddlmZmZmZmZmZmZm Z ddlm Z m Z m Z ddl mZddl mZdd l mZdd l mZdd lmZdd lmZddlmZdd lmZyddljZWnek rdZnXd d ddddgZdddd„Zdddd„Zddd„Zd„Zd„Z d„Z!dS(s4Utility functions for NDArray and BaseSparseNDArray.iÿÿÿÿNi(t_LIBt check_calltpy_strtc_strt string_typestmx_uintt NDArrayHandle(tc_arraytc_handle_arrayt c_str_arrayi(tNDArray(tarray(tempty(tzeros(t _ndarray_clsR R R tloadtload_frombuffertsavecKsE|dks|dkr+t||||St|||||SdS(sReturn a new array of given shape and type, filled with zeros. Parameters ---------- shape : int or tuple of int The shape of the empty array ctx : Context, optional An optional device context (default is the current default context) dtype : str or numpy.dtype, optional An optional value type (default is `float32`) stype: string, optional The storage type of the empty array, such as 'row_sparse', 'csr', etc. Returns ------- NDArray, CSRNDArray or RowSparseNDArray A created array Examples -------- >>> mx.nd.zeros((1,2), mx.cpu(), stype='csr') >>> mx.nd.zeros((1,2), mx.cpu(), 'float16', stype='row_sparse').asnumpy() array([[ 0., 0.]], dtype=float16) tdefaultN(tNonet_zeros_ndarrayt_zeros_sparse_ndarray(tshapetctxtdtypetstypetkwargs((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyR (scCs?|dks|dkr(t|||ƒSt||||ƒSdS(sHReturns a new array of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). stype : str, optional An optional storage type (default is `default`). Returns ------- NDArray, CSRNDArray or RowSparseNDArray A created array. Examples -------- >>> mx.nd.empty(1) >>> mx.nd.empty((1,2), mx.gpu(0)) >>> mx.nd.empty((1,2), mx.gpu(0), 'float16') >>> mx.nd.empty((1,2), stype='csr') RN(Rt_empty_ndarrayt_empty_sparse_ndarray(RRRR((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyR HscCs…tdk r7t|tjjƒr7t|d|d|ƒSt|tƒrk|jdkrkt|d|d|ƒSt|d|d|ƒSdS(s@Creates an array from any object exposing the array interface. Parameters ---------- source_array : array_like An object exposing the array interface, an object whose `__array__` method returns an array, or any (nested) sequence. ctx : Context, optional Device context (default is the current default context). dtype : str or numpy.dtype, optional The data type of the output array. The default dtype is ``source_array.dtype`` if `source_array` is an `NDArray`, `float32` otherwise. Returns ------- NDArray, RowSparseNDArray or CSRNDArray An array with the same contents as the `source_array`. Examples -------- >>> import numpy as np >>> mx.nd.array([1, 2, 3]) >>> mx.nd.array([[1, 2], [3, 4]]) >>> mx.nd.array(np.zeros((3, 2))) >>> mx.nd.array(np.zeros((3, 2)), mx.gpu(0)) >>> mx.nd.array(mx.nd.zeros((3, 2), stype='row_sparse')) RRRN( tspspRt isinstancetcsrt csr_matrixt _sparse_arrayR Rt_array(t source_arrayRR((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyR ls !!cs!t|tƒstdƒ‚ntƒ}tƒ}tjtƒƒ‰tjtjƒƒ‰tt j t |ƒtj |ƒtj ˆƒtj |ƒtj ˆƒƒƒ|j dkrßgt|j ƒD]}ttˆ|ƒƒ^q¿S|j |j ks÷t‚t‡‡fd†t|j ƒDƒƒSdS(s'Loads an array from file. See more details in ``save``. Parameters ---------- fname : str The filename. Returns ------- list of NDArray, RowSparseNDArray or CSRNDArray, or dict of str to NDArray, RowSparseNDArray or CSRNDArray Loaded data. sfname required to be a stringic3s5|]+}tˆ|ƒttˆ|ƒƒfVqdS(N(RRR(t.0ti(thandlestnames(sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pys µsN(RRt TypeErrorRtctypestPOINTERRtc_char_pRRt MXNDArrayLoadRtbyreftvaluetrangeRtAssertionErrortdict(tfnametout_sizet out_name_sizeR%((R&R'sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyR•s"     0c s7t|tttgƒƒs+tdƒ‚ntƒ}tƒ}tjtƒƒ‰tjtj ƒƒ‰t t j |tt |ƒƒtj|ƒtjˆƒtj|ƒtjˆƒƒƒ|jdkrõgt|jƒD]}ttˆ|ƒƒ^qÕS|j|jks t‚t‡‡fd†t|jƒDƒƒSdS(shLoads an array dictionary or list from a buffer See more details in ``save``. Parameters ---------- buf : str Buffer containing contents of a file as a string or bytes. Returns ------- list of NDArray, RowSparseNDArray or CSRNDArray, or dict of str to NDArray, RowSparseNDArray or CSRNDArray Loaded data. s$buf required to be a string or bytesic3s5|]+}tˆ|ƒttˆ|ƒƒfVqdS(N(RRR(R$R%(R&R'(sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pys ÚsN(RRttupletbytesR(RR)R*RR+RRtMXNDArrayLoadFromBuffertlenR-R.R/RR0R1(tbufR3R4R%((R&R'sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyR¹s$      0cCs.t|tƒr*|g}ttgƒ}nt|tƒr§|jƒ}|jƒ}td„|Dƒƒs}td„|DƒƒrŒtdƒ‚nt |ƒ}t |ƒ}nUt|t ƒrðtd„|DƒƒrÛtdƒ‚nd}t |ƒ}n t dƒ‚ttjt|ƒtt|ƒƒ||ƒƒdS(sgSaves a list of arrays or a dict of str->array to file. Examples of filenames: - ``/path/to/file`` - ``s3://my-bucket/path/to/file`` (if compiled with AWS S3 supports) - ``hdfs://path/to/file`` (if compiled with HDFS supports) Parameters ---------- fname : str The filename. data : NDArray, RowSparseNDArray or CSRNDArray, or list of NDArray, RowSparseNDArray or CSRNDArray, or dict of str to NDArray, RowSparseNDArray or CSRNDArray The data to save. Examples -------- >>> x = mx.nd.zeros((2,3)) >>> y = mx.nd.ones((1,4)) >>> mx.nd.save('my_list', [x,y]) >>> mx.nd.save('my_dict', {'x':x, 'y':y}) >>> mx.nd.load('my_list') [, ] >>> mx.nd.load('my_dict') {'y': , 'x': } css|]}t|tƒ VqdS(N(RR(R$tk((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pys scss|]}t|tƒ VqdS(N(RR (R$tv((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pys ss5save only accept dict str->NDArray or list of NDArraycss|]}t|tƒ VqdS(N(RR (R$R;((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pys ssTdata needs to either be a NDArray, dict of str, NDArray pairs or a list of NDarrays.N(RR RRR1tkeystvaluestanyR(R RtlistRt ValueErrorRRt MXNDArraySaveRRR8(R2tdataR&tstr_keystnd_valsR<((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyRÞs*     ("t__doc__R)tbaseRRRRRRRRRR tndarrayR R R"R RR RtsparseRRR!Rt scipy.sparseRt ImportErrorRt__all__RRR(((sS/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/utils.pyts, 4   $) $ %