ó ùµÈ[c@sÆdZddlZddlmZddlmZddlmZddlm Z ydd l TWne k rsnXd d d d gZ dd„Zd„Zd„Zd„Zdd„Zd„ZdS(sContrib NDArray API of MXNet.iÿÿÿÿNi(tcurrent_context(tuniform(t_as_listi(tndarray(t*t rand_zipfiantforeacht while_looptcondc Csæ|d krtƒ}ntj|dƒ}td|d|fddd|ƒ}|jƒdjdƒ|}|j|ƒjdƒ}|d|d jƒ||}|jdƒ} | d| d jƒ|} | |} ||| fS( sGDraw random samples from an approximately log-uniform or Zipfian distribution. This operation randomly samples *num_sampled* candidates the range of integers [0, range_max). The elements of sampled_candidates are drawn with replacement from the base distribution. The base distribution for this operator is an approximately log-uniform or Zipfian distribution: P(class) = (log(class + 2) - log(class + 1)) / log(range_max + 1) This sampler is useful when the true classes approximately follow such a distribution. For example, if the classes represent words in a lexicon sorted in decreasing order of frequency. If your classes are not ordered by decreasing frequency, do not use this op. Additionaly, it also returns the number of times each of the true classes and the sampled classes is expected to occur. Parameters ---------- true_classes : NDArray A 1-D NDArray of the target classes. num_sampled: int The number of classes to randomly sample. range_max: int The number of possible classes. ctx : Context Device context of output. Default is current context. Returns ------- samples: NDArray The sampled candidate classes in 1-D `int64` dtype. expected_count_true: NDArray The expected count for true classes in 1-D `float64` dtype. expected_count_sample: NDArray The expected count for sampled candidates in 1-D `float64` dtype. Examples -------- >>> true_cls = mx.nd.array([3]) >>> samples, exp_count_true, exp_count_sample = mx.nd.contrib.rand_zipfian(true_cls, 4, 5) >>> samples [1 3 3 3] >>> exp_count_true [ 0.12453879] >>> exp_count_sample [ 0.22629439 0.12453879 0.12453879 0.12453879] iitshapetdtypetfloat64tctxtint64g@gð?N(tNoneRtmathtlogRtexptastypet as_in_context( t true_classest num_sampledt range_maxR t log_rangetrandtsampled_classesttrue_clstexpected_count_truetsampled_cls_fp64texpected_prob_sampledtexpected_count_sampled((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyR"s3  $  cCs»t|tjƒr%|gtdƒfSt|ttfƒsetd|t|ƒtt|ƒƒfƒ‚g}g}x=|D]5}t ||ƒ\}}|j |ƒ|j |ƒqxW||fS(Nis:%s must be (nested) list of NDArray, but got %s of type %s( t isinstanceRtNDArraytinttlistttupletAssertionErrortstrttypet_flattentextendtappend(targst inout_strtflattfmtstitargtfmt((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyR'fs%  cCs¿t|tƒr?|dkr-|d|dfS|| ||fSt|ttfƒs|tdt|ƒtt|ƒƒfƒ‚g}x0|D](}t||ƒ\}}|j|ƒq‰W||fS(Niis>output must be (nested) list of NDArray, but got %s of type %s( RR!R"R#R$R%R&t_regroupR)(R*R0tretR.tres((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyR1vs " cCszd„}t|dƒ\}}||tjdƒt|dƒ\}}||tjdƒt|tjƒ}|r~|jdn|djd}|}g} x{t|ƒD]m} |rÁ|| } ng|D]} | | ^qÈ} || |ƒ\} }t| dƒ\} }| j| ƒq¨Wt| Œ} g}x'| D]}|jtjj |Œƒq2W|} t | |ƒ\} }| |fS(sÉRun a for loop with user-defined computation over NDArrays on dimension 0. This operator simulates a for loop and body has the computation for an iteration of the for loop. It runs the computation in body on each slice from the input NDArrays. body takes two arguments as input and outputs a tuple of two elements, as illustrated below: out, states = body(data1, states) data1 can be either an NDArray or a list of NDArrays. If data is an NDArray, data1 is an NDArray. Otherwise, data1 is a list of NDArrays and has the same size as data. states is a list of NDArrays and have the same size as init_states. Similarly, out can be either an NDArray or a list of NDArrays, which are concatenated as the first output of foreach; states from the last execution of body are the second output of foreach. The computation done by this operator is equivalent to the pseudo code below when the input data is NDArray: states = init_states outs = [] for i in data.shape[0]: s = data[i] out, states = body(s, states) outs.append(out) outs = stack(*outs) Parameters ---------- body : a Python function. Define computation in an iteration. data: an NDArray or a list of NDArrays. The input data. init_states: an NDArray or nested lists of NDArrays. The initial values of the loop states. name: string. The name of the operator. Returns ------- outputs: an NDArray or nested lists of NDArrays. The output data concatenated from the output of all iterations. states: an NDArray or nested lists of NDArrays. The loop states in the last iteration. Examples -------- >>> step = lambda data, states: (data + states[0], [states[0] * 2]) >>> data = mx.nd.random.uniform(shape=(2, 10)) >>> states = [mx.nd.random.uniform(shape=(10))] >>> outs, states = mx.nd.contrib.foreach(step, data, states) cSsgt}t|tƒrBx9|D]}t||ƒst}PqqWnt||ƒ}|sct|ƒ‚dS(N(tTrueRR"tFalseR$(tinputstin_typetmsgtis_NDArray_or_listR.((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyt check_input¿s  s foreach inputs6data should be an NDArray or a nested list of NDArrayssforeach statess=init_states should be an NDArray or a nested list of NDArraysisforeach output( R'RR RR trangeR)tziptoptstackR1(tbodytdatat init_statesR:tflattent_t not_data_listt num_iterststatestoutputsR.telestdtoutstout_fmtt tmp_outputstout((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyR†s29   $   c sÊd„}‡fd†}|dkr3tdƒ‚n||tdƒ}t|ƒdkrftdƒ‚nd}g}d}t|tjƒ} t|ƒ}x£||kr;|||Œtdƒr;||ƒ\} }t | dƒ\} }|j | ƒ|d 7}t|ƒ|ks)t| ƒt|dƒkr™td ƒ‚q™q™Wg} xDt t |Œd ƒD]-\} } g| D]}|j dƒ^qk} ||krü| rü||gt| djd ƒ}tjd |d | djd | djƒ}t| ƒ|g} ny#| j tjjdd| ŒƒWqXtk r„tdjd| ggt | ƒD]%\}}d|t|jƒf^qOƒƒ‚qXXqXW|dk r­t| |ƒ\} }n| rÀ|d}n| |fS(sÒ Run a while loop with user-defined computation and loop condition. This operator simulates a while loop which iterately does customized computation as long as the condition is satisfied. `loop_vars` is a list of NDArrays on which the computation uses. `cond` is a user-defined function, used as the loop condition. It consumes `loop_vars`, and produces a scalar MXNet NDArray, indicating the termination of the loop. The loop ends when `cond` returns false (zero). The `cond` is variadic, and its signature should be `cond(*loop_vars) => NDArray`. `func` is a user-defined function, used as the loop body. It also consumes `loop_vars`, and produces `step_output` and `new_loop_vars` at each step. In each step, `step_output` should contain the same number elements. Through all steps, the i-th element of `step_output` should have the same shape and dtype. Also, `new_loop_vars` should contain the same number of elements as `loop_vars`, and the corresponding element should have the same shape and dtype. The `func` is variadic, and its signature should be `func(*loop_vars) => (NDArray or nested List[NDArray] step_output, NDArray or nested List[NDArray] new_loop_vars)`. `max_iterations` is a scalar that defines the maximum number of iterations allowed. This function returns two lists. The first list has the length of `|step_output|`, in which the i-th element are all i-th elements of `step_output` from all steps, stacked along axis 0. The second list has the length of `|loop_vars|`, which represents final states of loop variables. .. warning:: For now, the axis 0 of all NDArrays in the first list are `max_iterations`, due to lack of dynamic shape inference. .. warning:: When `cond` is never satisfied, we assume `step_output` is empty, because it cannot be inferred. This is different from the symbolic version. Parameters ---------- cond: a Python function. The loop condition. func: a Python function. The loop body. loop_vars: an NDArray or nested lists of NDArrays. The initial values of the loop variables. max_iterations: a python int. Maximum number of iterations. Returns ------ outputs: an NDArray or nested lists of NDArrays stacked output from each step states: an NDArray or nested lists of NDArrays final state Examples -------- >>> cond = lambda i, s: i <= 5 >>> func = lambda i, s: ([i + s], [i + 1, s + i]) >>> loop_vars = (mx.nd.array([0], dtype="int64"), mx.nd.array([1], dtype="int64")) >>> outputs, states = mx.nd.contrib.while_loop(cond, func, loop_vars, max_iterations=10) >>> outputs [ [[ 1] [ 2] [ 4] [ 7] [11] [16] [...] # undefined value [...] [...] [...]] ] >>> states [ [6] , [16] ] cSsXt|tjƒr!|jƒ}ny||ƒ}Wn td||jfƒ‚nX|S(sxConverts "inputs", possibly typed mxnet NDArray, a numpy ndarray, other python types, to the given type sCannot convert %s to python %s(RRR tasscalart ValueErrort__name__(R6ttype_tname((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyt_to_python_scalar>scsµˆ|Œ\}}|dkr'g}n|dkr<g}nt|tƒrZt|ƒ}nt|tƒrxt|ƒ}nt|ƒ}t|ƒt|ƒkr«tdƒ‚n||fS(såThis wrapper unifies "func: loop_vars -> new_loop_vars" and "func: loop_vars -> (step_output, new_loop_vars)" into "func: loop_vars -> (None or tuple of step_outputs, tuple of new_loop_vars) s<The length of loop_vars should be consistent during the loopN(RRR#R"RtlenRO(t loop_varst step_outputt new_loop_vars(tfunc(sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyt _func_wrapperJs     s"max_iterations should be specifiedt max_iterationis-loop_vars should contain at least one elementsReturn value of conds while outputisANumber of elements in step_output should be the same in each stepR R R tdims sEShapes of %d-th elements in step_outputs are inconsistent, which are:s Step %d, shape is %sN(RROR!RTRRR RtboolR'R)t enumerateR<t expand_dimsR"R temptytcontextR R=tconcattjoinR%R1(RRXRUtmax_iterationsRSRYtstepsRGRKtnot_loop_var_listRVtstacked_outputsti_thtitemstxt pad_shapetpadR.RC((RXsU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyRæsRX     ."""  #   G  cCs3d„}||tdƒ}|r(|ƒS|ƒSdS(s÷Run an if-then-else using user-defined condition and computation This operator simulates a if-like branch which chooses to do one of the two customized computations according to the specified condition. `pred` is a scalar MXNet NDArray, indicating which branch of computation should be used. `then_func` is a user-defined function, used as computation of the then branch. It produces `outputs`, which is a list of NDArrays. The signature of `then_func` should be `then_func() => NDArray or nested List[NDArray]`. `else_func` is a user-defined function, used as computation of the else branch. It produces `outputs`, which is a list of NDArrays. The signature of `else_func` should be `else_func() => NDArray or nested List[NDArray]`. The `outputs` produces by `then_func` and `else_func` should have the same number of elements, all of which should be in the same shape, of the same dtype and stype. This function returns a list of symbols, representing the computation result. Parameters ---------- pred: a MXNet NDArray representing a scalar. The branch condition. then_func: a Python function. The computation to be executed if `pred` is true. else_func: a Python function. The computation to be executed if `pred` is false. Returns ------- outputs: an NDArray or nested lists of NDArrays, representing the result of computation. Examples -------- >>> a, b = mx.nd.array([1]), mx.nd.array([2]) >>> pred = a * b < 5 >>> then_func = lambda a, b: (a + 5) * (b + 5) >>> else_func = lambda a, b: (a - 5) * (b - 5) >>> outputs = mx.nd.contrib.cond(pred, then_func, else_func) >>> outputs[0] [42.] cSsUt|dƒr|jƒ}ny||ƒ}Wn td||jfƒ‚nX|S(sxConverts "inputs", possibly typed mxnet NDArray, a numpy ndarray, other python types, to the given type RNsCannot convert %s to python %s(thasattrRNRORP(R6RQRR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyRS¾stpredN(R\(Rmt then_funct else_funcRStbranch((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyRŽs 0 (t__doc__RR`RtrandomRtbaseRtRt gen_contribt ImportErrort__all__RRR'R1RRR(((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/ndarray/contrib.pyts    D   ` ¨