ó ùµÈ[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@sDeZdZdded„Zd„Zd„Zd„Zd„Z RS(suMonitor 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. 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((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyt asum_stat4sic@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(sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyt stat_helper@s #( tNoneRtintervalRRRRtexestretcompileRtsortR(RRRtpatternRR R((RsM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyt__init__2s          cC@s$|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_callbackRRR(Rtexe((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pytinstallIs 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((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pytticUs  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 ((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyt{stis (i(RRR%R&R'tzipt_symboltlist_argumentsRRRRRRtlist_auxiliary_statesRRt isinstanceRtlisttAssertionErrortshapetstrtasscalartasnumpy( RR#RRtrestntktv_listtstv((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyttoccs> (,(0     cC@sF|jƒ}x3|D]+\}}}tjdj|||ƒƒqWdS(s!End collecting and print results.sBatch: {:7d} {:30s} {:s}N(R>tloggingtinfotformat(RR8R9R:R=((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyt toc_print‹s N( t__name__t __module__t__doc__RRR!R$R)R>RB(((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyR!s   ((REt __future__RRR R?tmathRRRtbaseRRR,tobjectR(((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/monitor.pyts