ó —Àv]c@@sšdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z ddl mZd efd „ƒYZdS( s6Monitor outputs, weights, and gradients for debugging.i(tabsolute_importN(tsqrti(tNDArray(t NDArrayHandletpy_str(tndarraytMonitorcB@sGeZdZddeed„Zd„Zd„Zd„Zd„Z RS(sòMonitor inputs, outputs, weights, and gradients for debugging. Parameters ---------- interval : int Number of batches between printing. stat_func : function A function that computes statistics of tensors. Takes an `NDArray` and returns an `NDArray`. Defaults to mean absolute value |x|/size(x). pattern : str A regular expression specifying which tensors to monitor. Only tensors with names that match `name_pattern` will be included. For example, '.*weight|.*output' will print all weights and outputs and '.*backward.*' will print all gradients. monitor_all : bool, default False If true, monitor both input and output, otherwise monitor output only. s.*c@s”|dkrd„}|}n|ˆ_|ˆ_tˆ_gˆ_dˆ_gˆ_tj |ƒˆ_ |ˆ_ |ˆ_ ‡fd†}|ˆ_ dS(NcS@stj|ƒt|jƒS(s%returns |x|/size(x), async execution.(RtnormRtsize(tx((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyt asum_stat6sic@sztj|tƒ}t|dtƒ}ˆj sGˆjjt|ƒƒ rKdSˆj j ˆj t|ƒˆj |ƒfƒdS(swrapper for executor callbacktwritableN( tctypestcastRRtFalset activatedtre_progtmatchRtqueuetappendtstept stat_func(tnametarray(tself(s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyt stat_helperCs #(tNoneRtintervalRRRRtexestretcompileRtsortt monitor_allR(RRRtpatternRR R R((Rs./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyt__init__4s           cC@s*|j|j|jƒ|jj|ƒdS(sàinstall callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to. N(tset_monitor_callbackRR RR(Rtexe((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pytinstallLs cC@sŽ|j|jdkr{xM|jD]B}x|jD]}|jƒq0Wx|jD]}|jƒqNWq Wg|_t|_n|jd7_dS(sNStart collecting stats for current batch. Call before calling forward.iiN( RRRt arg_arrayst wait_to_readt aux_arraysRtTrueR(RR$R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pytticXs  c C@sP|js gSxM|jD]B}x|jD]}|jƒq'Wx|jD]}|jƒqEWqWxÝ|jD]Ò}xct|jjƒ|jƒD]F\}}|jj |ƒr‰|j j |j ||j |ƒfƒq‰q‰Wxct|jjƒ|jƒD]F\}}|jj |ƒrï|j j |j ||j |ƒfƒqïqïWqgWt|_g}|jrn|j jdd„ƒnxÒ|j D]Ç\}}}t|tƒr¢|g}nt|tƒs·t‚d}xi|D]a} t| tƒsßt‚| jdkr |t| jƒƒd7}qÄ|t| jƒƒd7}qÄW|j |||fƒqxWg|_ |S(sEnd collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of tkeycS@s|dS(Ni((R ((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyt~tR-is (i(RRR&R'R(tzipt_symboltlist_argumentsRRRRRRtlist_auxiliary_statesRRt isinstanceRtlisttAssertionErrortshapetstrtasscalartasnumpy( RR$RRtrestntktv_listtstv((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyttocfs> (,(0     cC@sF|jƒ}x3|D]+\}}}tjdj|||ƒƒqWdS(s!End collecting and print results.sBatch: {:7d} {:30s} {:s}N(R?tloggingtinfotformat(RR9R:R;R>((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyt toc_printŽs N( t__name__t __module__t__doc__RRR"R%R*R?RC(((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyR!s   ((RFt __future__RRR R@tmathRRRtbaseRRR-tobjectR(((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/monitor.pyts