[c@sldZdddgZddlZddlZddlZddlZddlmZddlm Z dd l m Z m Z m Z dd l mZdd l mZdd l mZd dlmZmZmZd dlmZmZmZdefdYZdZdZdefdYZdefdYZdZ defdYZ!e dZ"dS(s3Base container class for all neural network models.tBlockt HybridBlockt SymbolBlockiN(t OrderedDicti(t mx_real_t(tsymboltndarrayt initializer(tSymbol(tNDArray(tnamei(t Parametert ParameterDicttDeferredInitializationError(t_indentt_brief_print_listt HookHandlet _BlockScopecBsDeZdZejZdZedZdZ dZ RS(s%Scope for collecting child `Block` s.cCs(||_i|_d|_d|_dS(N(t_blockt_countertNonet _old_scopet _name_scope(tselftblock((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt__init__*s   cCsRttjdd}|dkr|dkr|ttjjdsZtjtjj_ntjjjjd|d}n|dkrt |}nt |j |}||fS|dkr|j j|d}d||f}|d|j |t_regroupRA(RBRItretRGtres((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRJos " cBs7eZdZdddZdZdZdZdZe dZ e dZ dZ e d Z dd Zd d Zd ZdZdeedZdeedZddZdZdZdZejdeedZedZdZdZdZ dZ!RS(s Base class for all neural network layers and models. Your models should subclass this class. :py:class:`Block` can be nested recursively in a tree structure. You can create and assign child :py:class:`Block` as regular attributes:: from mxnet.gluon import Block, nn from mxnet import ndarray as F class Model(Block): def __init__(self, **kwargs): super(Model, self).__init__(**kwargs) # use name_scope to give child Blocks appropriate names. with self.name_scope(): self.dense0 = nn.Dense(20) self.dense1 = nn.Dense(20) def forward(self, x): x = F.relu(self.dense0(x)) return F.relu(self.dense1(x)) model = Model() model.initialize(ctx=mx.cpu(0)) model(F.zeros((10, 10), ctx=mx.cpu(0))) Child :py:class:`Block` assigned this way will be registered and :py:meth:`collect_params` will collect their Parameters recursively. You can also manually register child blocks with :py:meth:`register_child`. Parameters ---------- prefix : str Prefix acts like a name space. All children blocks created in parent block's :py:meth:`name_scope` will have parent block's prefix in their name. Please refer to `naming tutorial `_ for more info on prefix and naming. params : ParameterDict or None :py:class:`ParameterDict` for sharing weights with the new :py:class:`Block`. For example, if you want ``dense1`` to share ``dense0``'s weights, you can do:: dense0 = nn.Dense(20) dense1 = nn.Dense(20, params=dense0.collect_params()) cCs|dk|_tj|||j\|_|_|jjdrU|jd n|j|_t||_t |_ i|_ t |_ t |_ dS(NtRi(R*RR)t_aliast_prefixt_paramstendswithRt_scopeRt _childrent _reg_paramst_forward_hookst_forward_pre_hooks(RR"R#((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs'+   c Csd}djg|jjD]B\}}t|trdjd|dt|jd^q}|jd|jj d|S( Ns{name}( {modstr} )s s ({key}): {block}tkeyRiR tmodstr( tjoint__dict__titemsR6RtformatRt__repr__t __class__R0(RtsRWRRX((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR]s Xc Cst||rt||}t|ttfrt|t| rtdjd|dt|dt|qnt|tr|j||n:t|tr||j kst d||j |t TypeErrorR\tregister_childRTR<tsupert __setattr__(RR Rtexisting((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRes+ * cst|jjfdx|jjD]\}}t|tttfr7|j dps|dk r7|rt j dj d|j jd|ddqq7q7WdS( Ncst|ttfr:x|D]}|rtSqWtSt|trzx*|jD]\}}|rVtSqVWtSt|tr|kStSdS(N(R6R:R;tTruetFalsetdictR[R(tdatateleRtv(t%_find_unregistered_block_in_containertchildren(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRms    t__RSs"{name}" is an unregistered container with Blocks. Note that Blocks inside the list, tuple or dict will not be registered automatically. Make sure to register them using register_child() or switching to nn.Sequential/nn.HybridSequential instead. R t.t stackleveli(tsetRStvaluesRZR[R6R:R;Rit startswithtwarningstwarnR\R^R0(RtkRl((RmRnsQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt_check_container_with_blocks4 cCs|jjjS(N(R^R0tlower(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRNscCs|jS(s!Prefix of this :py:class:`Block`.(RO(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR"scCs|jS(s7Name of this :py:class:`Block`, without '_' in the end.(R(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR scCs|jS(sReturns a name space object managing a child :py:class:`Block` and parameter names. Should be used within a ``with`` statement:: with self.name_scope(): self.dense = nn.Dense(20) Please refer to `naming tutorial `_ for more info on prefix and naming. (RR(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt name_scopes cCs|jS(skReturns this :py:class:`Block`'s parameter dictionary (does not include its children's parameters).(RP(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR# scs|jt|jj}|s5|j|jn5tj||jfd|jjDx0|j j D]}|j|j d|qzW|S(sReturns a :py:class:`ParameterDict` containing this :py:class:`Block` and all of its children's Parameters(default), also can returns the select :py:class:`ParameterDict` which match some given regular expressions. For example, collect the specified parameters in ['conv1_weight', 'conv1_bias', 'fc_weight', 'fc_bias']:: model.collect_params('conv1_weight|conv1_bias|fc_weight|fc_bias') or collect all parameters whose names end with 'weight' or 'bias', this can be done using regular expressions:: model.collect_params('.*weight|.*bias') Parameters ---------- select : str regular expressions Returns ------- The selected :py:class:`ParameterDict` cs.i|]$\}}j|r||qS((tmatch(t.0R R(tpattern(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys .s tselect( RxR RPR"tupdateR#tretcompileR[RSRstcollect_params(RR~RKtcld((R}sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs &RMcsprd7nfd|jjD}x7|jjD]&\}}|j|j|qBW|S(NRpcs#i|]\}}||qS(((R|RWtval(R"(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys 6s (RTR[RSRt_collect_params_with_prefix(RR"RKR tchild((R"sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR3s  cCs6|j}d|jD}tj||dS(sSave parameters to file. Saved parameters can only be loaded with `load_parameters`. Note that this method only saves parameters, not model structure. If you want to save model structures, please use :py:meth:`HybridBlock.export`. Parameters ---------- filename : str Path to file. References ---------- `Saving and Loading Gluon Models `_ cSs%i|]\}}|j|qS((t_reduce(R|RWR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys Ns N(RR[Rtsave(RtfilenameR#targ_dict((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pytsave_parameters;s cCsZtjdy |jj|d|jWn&tk rU}td|jnXdS(s[Deprecated] Please use save_parameters. Note that if you want load from SymbolBlock later, please use export instead. Save parameters to file. filename : str Path to file. ssave_params is deprecated. Please use save_parameters. Note that if you want load from SymbolBlock later, please use export instead. For details, see https://mxnet.incubator.apache.org/tutorials/gluon/save_load_params.htmlt strip_prefixsK%s save_params is deprecated. Using save_parameters may resolve this error.N(RuRvRRR"t ValueErrortmessage(RRte((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt save_paramsQs  cCs=tj|}|j}| r-| r-dStd|jDsr~|jj|||||jdS|sxH|jD]7}||kstd||t|jfqWnxs|D]k}| r||krt d||t|j jfn||kr||j |||qqWdS(sLoad parameters from file previously saved by `save_parameters`. Parameters ---------- filename : str Path to parameter file. ctx : Context or list of Context, default cpu() Context(s) to initialize loaded parameters on. allow_missing : bool, default False Whether to silently skip loading parameters not represents in the file. ignore_extra : bool, default False Whether to silently ignore parameters from the file that are not present in this Block. References ---------- `Saving and Loading Gluon Models `_ Ncss|]}d|kVqdS(RpN((R|RG((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys ss{Parameter '%s' is missing in file '%s', which contains parameters: %s. Set allow_missing=True to ignore missing parameters.sParameter '%s' loaded from file '%s' is not present in ParameterDict, which contains parameters %s. Set ignore_extra=True to ignore. ( RtloadRtanytkeysRR"R<RRRPt _load_init(RRtctxt allow_missingt ignore_extratloadedR#R ((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pytload_parameterses*  & % cCs'tjd|j||||dS(s[Deprecated] Please use load_parameters. Load parameters from file. filename : str Path to parameter file. ctx : Context or list of Context, default cpu() Context(s) to initialize loaded parameters on. allow_missing : bool, default False Whether to silently skip loading parameters not represents in the file. ignore_extra : bool, default False Whether to silently ignore parameters from the file that are not present in this Block. s6load_params is deprecated. Please use load_parameters.N(RuRvR(RRRRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt load_paramss cCs5|dkr$tt|j}n||j| None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` (RtattachRV(Rthookthandle((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pytregister_forward_pre_hooks cCs t}|j|j||S(sRegisters a forward hook on the block. The hook function is called immediately after :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input, output) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` (RRRU(RRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pytregister_forward_hooks cCs5x$|jjD]}|j|qW|||S(sApplies ``fn`` recursively to every child block as well as self. Parameters ---------- fn : callable Function to be applied to each submodule, of form `fn(block)`. Returns ------- this block (RSRstapply(RtfnR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs  cCs |jj||||dS(sInitializes :py:class:`Parameter` s of this :py:class:`Block` and its children. Equivalent to ``block.collect_params().initialize(...)`` Parameters ---------- init : Initializer Global default Initializer to be used when :py:meth:`Parameter.init` is ``None``. Otherwise, :py:meth:`Parameter.init` takes precedence. ctx : Context or list of Context Keeps a copy of Parameters on one or many context(s). verbose : bool, default False Whether to verbosely print out details on initialization. force_reinit : bool, default False Whether to force re-initialization if parameter is already initialized. N(Rt initialize(RtinitRtverboset force_reinit((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRscKs.x'|jjD]}|j||qWdS(sMActivates or deactivates :py:class:`HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. static_alloc : bool, default False Statically allocate memory to improve speed. Memory usage may increase. static_shape : bool, default False Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower. N(RSRst hybridize(RtactivetkwargsR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRscCsXx$|jjD]}|j|qWx*|jjD]\}}|j|q7WdS(sCast this Block to use another data type. Parameters ---------- dtype : str or numpy.dtype The new data type. N(RSRstcastR#R[(RtdtypeRRtparam((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR scGsdx$|jjD]}|||qW|j|}x'|jjD]}||||qFW|S(s1Calls forward. Only accepts positional arguments.(RVRstforwardRU(RRBRtout((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt__call__s cGs tdS(sOverrides to implement forward computation using :py:class:`NDArray`. Only accepts positional arguments. Parameters ---------- *args : list of NDArray Input tensors. N(tNotImplementedError(RRB((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR%s c sttgdfd}td<|dd20} {:>42} {:>15}t-iPs Layer (type)s Output ShapesParam #t=s;Parameters in forward computation graph, duplicate includeds Total params: s Trainable params: s Non-trainable params: s,Shared params in forward computation graph: sUnique parameters in model: N(RRrRR\R=tdetach( RtinputsRt line_formatt total_paramsttrainable_paramst shared_paramstlayerth((RRRRsQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR1sJ   !          N("R0R1R2RRR]ReRxRNtpropertyR"R RzR#RRRRRhRRRcRRRRtUniformRRgRRRRR(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs:-      $    0        cBseZdZdddZdZdZdZdZdZ dZ ddZ e d Z d Zd Zd Zd ZddZdZdZRS(s`HybridBlock` supports forwarding with both Symbol and NDArray. `HybridBlock` is similar to `Block`, with a few differences:: import mxnet as mx from mxnet.gluon import HybridBlock, nn class Model(HybridBlock): def __init__(self, **kwargs): super(Model, self).__init__(**kwargs) # use name_scope to give child Blocks appropriate names. with self.name_scope(): self.dense0 = nn.Dense(20) self.dense1 = nn.Dense(20) def hybrid_forward(self, F, x): x = F.relu(self.dense0(x)) return F.relu(self.dense1(x)) model = Model() model.initialize(ctx=mx.cpu(0)) model.hybridize() model(mx.nd.zeros((10, 10), ctx=mx.cpu(0))) Forward computation in :py:class:`HybridBlock` must be static to work with :py:class:`Symbol` s, i.e. you cannot call :py:meth:`NDArray.asnumpy`, :py:attr:`NDArray.shape`, :py:attr:`NDArray.dtype`, `NDArray` indexing (`x[i]`) etc on tensors. Also, you cannot use branching or loop logic that bases on non-constant expressions like random numbers or intermediate results, since they change the graph structure for each iteration. Before activating with :py:meth:`hybridize()`, :py:class:`HybridBlock` works just like normal :py:class:`Block`. After activation, :py:class:`HybridBlock` will create a symbolic graph representing the forward computation and cache it. On subsequent forwards, the cached graph will be used instead of :py:meth:`hybrid_forward`. Please see references for detailed tutorial. References ---------- `Hybrid - Faster training and easy deployment `_ cCsYtt|jd|d|d|_d|_d|_d|_t|_ g|_ dS(NR"R#(( RdRRt _cached_graphRt _cached_opt _out_formatt _in_formatRhRt_flags(RR"R#((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs     cCs9tt|j||t|tr5|jndS(sRegisters parameters.N(RdRReR6t_clear_cached_op(RR R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRescGs |jst|d\}|_t|dkrhgtt|D]}tjd|^qF}ntjdg}t||jd}d|jj D}|j |j t||}WdQXt|d\}|_ |tj |f|_n|jS(Ntinputisdata%dRjicSs%i|]\}}|j|qS((tvar(R|RGtj((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys s toutput(RR?RR8trangeRRRJRTR[Rzthybrid_forwardRtGroup(RRBRGRtgrouped_inputsR#R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt _get_graphs 5 cGsl|j|\}}dt|D}|j}|j}t|j}t|}x6|D].} | |ksh| |kshtd| qhWg|D]} | |kr| ^q} t| t|kr/djg|j D]"\} } | |krd| ^q} t j d| ddng|D]} | |kr6| ^q6} t| t|krdjt |t| } t j d| ddng}g}g|_ xwt|D]i\} } | |kr |j| |j jt|| fq|j| |j jt|| fqWd |fd |fg|j}tj|||_dS( NcSs"i|]\}}||jqS((R (R|RGRj((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys s s Unknown input to HybridBlock: %ss, s%d-thsMThe %s input to HybridBlock is not used by any computation. Is this intended?Rqis>Parameter %s is not used by any computation. Is this intended?t data_indicest param_indices(Rt enumerateRt list_inputsRrRR<R8RYR[RuRvR:t_cached_op_argsRARgRhRRtCachedOpR(RRBRjRt data_namesR#t input_namest param_namestexpected_namesR RGtused_data_namestunusedtused_param_namesRRtflags((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt _build_caches@    % %      cGsFy|j|Wn.tk rA}dj|}t|nXdS(NsCDeferred initialization failed because shape cannot be inferred. {}(t infer_shapet ExceptionR\R(RRBRt error_msg((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt_deferred_infer_shapes  cGsA|jdkr|j|nt|d\}}||jksOtdy<g|jD](\}}|rx||n |j^q\}Wnutk r|j |g}xR|jD]C\}}|r|j ||q|j |j |jqWnX|j|}t |t r-|g}nt||jdS(NRsInvalid input formati(RRRR?RR<RRjR RRAt_finish_deferred_initR6R RJR(RRBRItis_argRGtcargsR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt_call_cached_ops&9    cCsd|_d|_dS(N((RRR(R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR4s cCsat|ts:tdt|tt|fntt|j|||jdS(NsChildren of HybridBlock must also be HybridBlock, but %s has type %s. If you are using Sequential, please try HybridSequential instead.(R6RRR=R>RdRcR(RRR ((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRc8s %cKsm||_t|j|_|j|r7|js@|jrPtjdnt t |j ||dS(Ns"{}" is being hybridized while still having forward hook/pre-hook. If "{}" is a child of HybridBlock, the hooks will not take effect.( RR:R[RRRURVRuRvRdRR(RRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRBs   cCs$|jtt|j|dS(N(RRdRR(RR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRKs c s|j|\}}t|d\}}tjdt`}t||fdt||D\}}} |dkrt|dj nWdQXdt|j |D} | j dt|j | Dx1|j jD]} t| | | jqWdS(sGeneric infer attributes.Rtrecordcs+i|]!\}}t||jqS((RR (R|RGR(tattr(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys Us iNcSsi|]\}}||qS(((R|RGR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys Xs cSsi|]\}}||qS(((R|R R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys Ys (RR?Rutcatch_warningsRgRtzipRRRtlist_argumentsRtlist_auxiliary_statesRRstsetattrR ( Rtinfer_fnRRBRRRtwt arg_attrst aux_attrstsdictRG((RsQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt _infer_attrsOs +  cGs|jdd|dS(s'Infers shape of Parameters from inputs.RRN(R(RRB((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR^scGs|jdd|dS(s+Infers data type of Parameters from inputs.t infer_typeRN(R(RRB((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRbsic Cs|jstdn|jd}|jd|t|j}t|j}i}xi|jjD]U\}}||kr|j|d|s cs(i|]\}}|j|qS((Rj(R|RGR(R(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys s NsZHybridBlock requires the first argument to forward be either Symbol or NDArray, but got %scSs%i|]\}}|j|qS((R(R|RGR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys s (R6R tcontextRRRTR[R RR#RRRRR<R>RzR(RRRBR#RRG((RsQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs$  # # cOs tdS(sOverrides to construct symbolic graph for this `Block`. Parameters ---------- x : Symbol or NDArray The first input tensor. *args : list of Symbol or list of NDArray Additional input tensors. N(R(RtFRRBR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs N(R0R1R2RRReRRRRRRcRgRRRRRRRR(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs"+   '        ! cCs|s dS|d}xg|D]_}d}xF|t|kro|t|kro||||kro|d7}q*W|| }qW|S(s#Get the common prefix for all namesRMii(R8(tnamesR"R RG((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyt_common_prefixs  ;cBsSeZdZedddZddZdZdZdZ dZ RS(siConstruct block from symbol. This is useful for using pre-trained models as feature extractors. For example, you may want to extract the output from fc2 layer in AlexNet. Parameters ---------- outputs : Symbol or list of Symbol The desired output for SymbolBlock. inputs : Symbol or list of Symbol The Variables in output's argument that should be used as inputs. params : ParameterDict Parameter dictionary for arguments and auxililary states of outputs that are not inputs. Examples -------- >>> # To extract the feature from fc1 and fc2 layers of AlexNet: >>> alexnet = gluon.model_zoo.vision.alexnet(pretrained=True, ctx=mx.cpu(), prefix='model_') >>> inputs = mx.sym.var('data') >>> out = alexnet(inputs) >>> internals = out.get_internals() >>> print(internals.list_outputs()) ['data', ..., 'model_dense0_relu_fwd_output', ..., 'model_dense1_relu_fwd_output', ...] >>> outputs = [internals['model_dense0_relu_fwd_output'], internals['model_dense1_relu_fwd_output']] >>> # Create SymbolBlock that shares parameters with alexnet >>> feat_model = gluon.SymbolBlock(outputs, inputs, params=alexnet.collect_params()) >>> x = mx.nd.random.normal(shape=(16, 3, 224, 224)) >>> print(feat_model(x)) cCstj|}t|tr*|g}ng|D]}tj|^q1}t||}|dk r|jj|d|n|S(sImport model previously saved by `HybridBlock.export` or `Module.save_checkpoint` as a SymbolBlock for use in Gluon. Parameters ---------- symbol_file : str Path to symbol file. input_names : list of str List of input variable names param_file : str, optional Path to parameter file. ctx : Context, default None The context to initialize SymbolBlock on. Returns ------- SymbolBlock SymbolBlock loaded from symbol and parameter files. Examples -------- >>> net1 = gluon.model_zoo.vision.resnet18_v1( ... prefix='resnet', pretrained=True) >>> net1.hybridize() >>> x = mx.nd.random.normal(shape=(1, 3, 32, 32)) >>> out1 = net1(x) >>> net1.export('net1', epoch=1) >>> >>> net2 = gluon.SymbolBlock.imports( ... 'net1-symbol.json', ['data'], 'net1-0001.params') >>> out2 = net2(x) RN(RRR6R=RRRR(t symbol_fileRt param_fileRRRGRRK((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pytimportss" " c stt|jddddd|_td||_t|tj rpt |j dkrp|g}nt|t t frt |dkr|d}nt|d\}|_t|d\}|_tj|}t}xR|D]J}t |jj dks-tdt||j|jqWtjjd }xS|D]K}xB|jD]4} | jd t|ksktd | jqkWqXW|j} |j} t||| | \} } xIt| D];\}}||kr|jj |d t!d | |qqWxOt| D]A\}}||kr3|jj |ddd t!d | |q3q3W||f|_"t t#t |jj$fd|jj%D|_&dS(NR"R#RMiiRRs@Input symbols must be variable, but %s is an output of operatorst row_sparset__storage_type__sTSymbolBlock doesn't support Parameter '%s' because its storage type is 'row_sparse'.tallow_deferred_initRRRcs#i|]\}}||qS(((R|RWR(t len_prefix(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys 2s ('RdRRRROR RPR6RRR8R9R:R;R?RRRRrt get_internalsR<R=RR Rt_STORAGE_TYPE_STR_TO_IDRRR t_infer_param_typesRR#R!RgRRRR[RT(RRRR#tsymsRRRGtrow_sparse_storageRt arg_paramst aux_paramst arg_typest aux_typesRHtaux((R#sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRsB * '   !    ' -!cGst|tr2|j|j||SWdQXnt|tsWtdt|t|gt|d\}}||j kstdt j |j d}|j dt |j d|Dtt||jdS(NsZHybridBlock requires the first argument to forward be either Symbol or NDArray, but got %sRsInvalid input formaticSs"i|]\}}||jqS((R (R|RwRl((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys ?s i(R6R RRRR<R>R?R:RtcopyRt_composeRRJR(RRRBtin_fmtRK((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR4s "'cCs)|j}tt|j||_dS(N(RRdRR(Rttmp((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRBs cCs$|jtt|j|dS(N(RRdRR(RR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRGs cOs tdS(N(R(RRRRBR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRKsN( R0R1R2R5RRRRRRR(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyRs+ .   cCsmd}d}g|D]}|j^q}g} t} x\|D]T}|jd} | sjt| dkrtt} Pq;| j|jddq;W| rdt|| D} |j| \}} }n|dkst|t|krg}x|D]} |j|qWn|dks<t|t|krcg}x|D]} |j|qIWn||fS(sqUtility function that helps in inferring DType of args and auxs params from given input param. Parameters ---------- in_params: List of Symbol List of input symbol variables. out_params: Symbol Output symbol variable. arg_params: List of Str List of names of argument parametrs. aux_params: List of Str List of names of auxiliary parameters. default_dtype: numpy.dtype or str, default 'float32' Default data type for arg_params and aux_params, if unable to infer the type. Returns ------- arg_types: List of numpy.dtype List of arg_params type. Order is same as arg_params. Defaults to 'float32', if unable to infer type. aux_types: List of numpy.dtype List of aux_params type. Order is same as aux_params. Defaults to 'float32', if unable to infer type. iicSsi|]\}}||qS(((R|RwRl((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pys }s N(RR RgRR8RhRAR(t in_paramst out_paramsR)R*t default_dtypeR+R,tin_paramtinput_sym_namestinput_sym_arg_typestcan_infer_input_typetinput_sym_arg_typeR#R((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyR&Ns. $ $ (#R2t__all__R3R.RuRt collectionsRtbaseRRMRRRRR R Rt parameterR R R tutilsRRRtobjectRR?RJRRRRR&(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/gluon/block.pyts0    5  $