ó
àÆ÷Xc           @` sg   d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z d e f d „  ƒ  YZ	 d „  Z
 d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   Modet   MonitorModec           B` sM   e  Z d  Z d d d d d „ Z d „  Z d „  Z d „  Z d d d „ Z RS(	   s  
    `MonitorMode` is a debug mode to easily step through function execution.

    Its default behavior is to behave like the 'FAST_RUN' mode. By providing
    either a `pre_func` (called before a node is executed) or a `post_func`
    (called after a node is executed) monitoring function, the user can inspect
    node behavior.

    A typical use case is to detect the introduction of NaN values in a graph.
    For an example of such a use case, see doc/tutorial/debug_faq.txt.

    Parameters
    ----------
    pre_func
        A function to call before executing a thunk, with arguments:
        - the thunk index
        - the Apply node
        - the thunk to be called
    post_func
        A function to call after executing a thunk, with the same three
        arguments as `pre_func`.
    optimizer
        The optimizer to use. One may use for instance 'fast_compile' to skip
        optimizations.
    linker
        DO NOT USE. This mode uses its own linker. The parameter is needed to
        allow selecting optimizers to use.

    t   defaultc         C` s¥   | |  _  | |  _ t j j t j j ƒ  g |  j g ƒ } | d k rT t j j } n  | d  k	 r… t
 | j t ƒ r… t d | ƒ ‚ n  t t |  ƒ j | d | ƒd  S(   NR   sD   MonitorMode can only use its own linker! You should not provide one.t	   optimizer(   t   pre_funct	   post_funct   theanot   goft   WrapLinkerManyt   OpWiseCLinkert   evalt   configR   t   Nonet
   isinstancet   modeR   t	   Exceptiont   supert   __init__(   t   selfR   R   R   t   linkert   wrap_linker(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyR   )   s    		c         C` s1   t  t |  ƒ j ƒ  \ } } | | |  j |  j f S(   N(   R   R   t   __getstate__R   R   (   R   t   lnkt   opt(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyR   8   s    c         C` sD   | \ } } } } | |  _  | |  _ t t |  ƒ j | | f ƒ d  S(   N(   R   R   R   R   t   __setstate__(   R   t   stateR   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyR   <   s    		c         C` sU   |  j  d k	 r% |  j  | | | ƒ n  | ƒ  |  j d k	 rQ |  j | | | ƒ n  d S(   s8   
        The method that calls the thunk `fn`.

        N(   R   R   R   (   R   t   it   nodet   fn(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyR   B   s
    t    c      	   K` sI   | d k r |  j  } n  t |  ƒ d |  j d |  j d d d | ƒ } | S(   sÄ   
        Create a new instance of this Mode.

        Keyword arguments can be provided for the linker, but they will be
        ignored, because MonitorMode needs to use its own linker.

        R    R   R   R   R   N(   t   provided_optimizert   typeR   R   R   (   R   t   link_kwargsR   t   kwargst   new_mode(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyt   cloneM   s    		N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R&   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyR   
   s   			c         C` s¹   x² | j  D]§ } t | d t j j ƒ r
 t j | d ƒ j ƒ  r
 t d ƒ t j	 j
 | ƒ t d g  | j D] } | d ^ qm ƒ t d g  | j  D] } | d ^ q• ƒ Pq
 q
 Wd  S(   Ni    s   *** NaN detected ***s   Inputs : %ss   Outputs: %s(   t   outputsR   t   npt   randomt   RandomStatet   isnant   anyt   printR	   t   printingt
   debugprintt   inputs(   R   R   R   t   outputt   input(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyt
   detect_nan^   s    
(((   t
   __future__R    R   R   t   numpyR+   R	   t   theano.compile.modeR   R   R6   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/monitormode.pyt   <module>   s
   T