σ
ΰΖχXc           @` sQ  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z m Z m Z d d l
 m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z d d l Z e j d  Z d Z d d d e e e d  Z d e f d     YZ d d d d e e d e d d d d d  Z  e d d  Z! d   Z" d S(   s&   
Provide a simple user friendly API.

i    (   t   absolute_importt   print_functiont   divisionN(   t   config(   t	   iteritems(   t   orig_functiont   Int   Out(   t   UnusedInputError(   t   SharedVariablet   shared(   t   ProfileStats(   t   Variablet   Constants   theano.compile.pfuncs   restructuredtext enc      	   ` s  t  |  t  r t |   }  n  i    i   g   g           f d    | d k rl g  } n  y t | j    } Wn t k
 r | } n Xx | D] \ } }	 t  | t  sΠ t d |   n  t  |	 t  sξ t |	  }	 n  |   k rt	 d | |	 f   n   |	     | <q£ W| d k r?g  } n     f d   }
 g  | D] } |
 |  ^ qX} x0 | D]( } t  | t
  rwt d |   qwqwW| d k rΈg  } n  xt |  D]τ \ } } t  | t
  sςt d |   n  |  k rt d |  | f   n  y | j j | d t } WnK t k
 rd	 | | j | t | d
 d  f } d } t | |   n X| j | j k st	  |  | < j | | f  qΕWt  |  t  r~g  } x-|  D] } t  | t  r |   } | j |  qΩt  | t  rM | j   } | j t | d | j  qΩt d t |  d t t |     qΩWn t  |  t  r₯ |    } | } n] t  |  t  rή |  j   } t | d |  j } n$ |  d k rσg  } n t d |    d } xt | t   k  r~ | \ } }  |   } |  | <t  | t
  rq|  k rq j |  n  | d 7} qW| |      g f S(   s8  
    Function that allows replacing subgraphs of a computational graph.

    It returns a set of dictionaries and lists which collect (partial?)
    different information about shared variables. This info is required by
    `pfunc`.

    Parameters
    ----------
    outputs : list of Theano Variables (or Theano expressions)
        List of Theano variables or expressions representing the outputs of the
        computational graph.
    inputs : list of Theano Variables (or Theano expressions)
        List of Theano variables or expressions representing the inputs of the
        computational graph (or None).
    replace : dict
        Dictionary describing which subgraphs should be replaced by what.
        orig_value => new_value
    updates : dict
        Dictionary describing updates expressions for shared variables.
    rebuild_strict : bool
        Flag, if true the type of all inputs should be the same as the one for
        the current node.
    copy_inputs_over : bool
        Flag; if False it will clone inputs.
    no_default_updates : either bool or list of Variables
        If True, do not perform any automatic update on Variables.
        If False (default), perform them all.
        Else, perform automatic updates on all Variables that are neither in
        "updates" nor in "no_default_updates".

    c         ` s  |  d k	 s t  |    k r&   |  S|  j rε |  j } |   k rΥ x | j D] }  | |  qN W| j g  | j D] }   | ^ qu d    | <x: t | j   | j  D] \ } }   j | |  q² Wn    j |  |   St |  t	  rΜ|   k r j
 |   n  t |  d  rΜ t k sFt  t  rΙ|   k rΙ|   k rΖ|  j j |  j d t } | j |  j k r£t d |  |  j | | j f   n  |  |  < j
 |  | f  qΖqΙqΜn  | sρt |  t  rt |  d  r  j |  |  j    S  j |  |   Sd S(   s  
        Clones a variable and its inputs recursively until all are in clone_d.
        Also appends all shared variables met along the way to shared inputs,
        and their default_update (if applicable) to update_d and update_expr.

        v can have an fgraph attached to it, case in which we want to clone
        constants (to avoid having a constant belonging to two fgraphs).

        t   strictt   default_updatet   allow_convertsA   an update must have the same type as the original shared variablet   fgraphN(   t   Nonet   AssertionErrort   ownert   inputst   clone_with_new_inputst   zipt   outputst
   setdefaultt
   isinstanceR	   t   appendt   hasattrt   Falset   listt   typet   filter_variableR   t	   TypeErrorR   t   clone(   t   vt   copy_inputs_overR   t   it   old_ot   new_ot   v_update(   t   clone_dt   clone_v_get_shared_updatest   no_default_updatest   rebuild_strictt   shared_inputst   update_dt   update_expr(    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyR*   K   sB    		-&	
s   given keys must be Variables  When using 'givens' or 'replace' with several (old_v, new_v) replacement pairs, you can not have a new_v variable depend on an old_v one. For instance, givens = {a:b, b:(a+1)} is not allowed. Here, the old_v %s is used to compute other new_v's, but it is scheduled to be replaced by %s.c         ` s0    s   j  |  |  j    S  j  |  |   Sd  S(   N(   R   R"   (   R%   (   R)   R$   (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyt   clone_inputs   s    s{   Cannot use a shared variable (%s) as explicit input. Consider substituting a non-shared variable via the `givens` parameters&   update target must be a SharedVariables5   this shared variable already has an update expressionR   s   An update must have the same type as the original shared variable (shared_var=%s, shared_var.type=%s, update_val=%s, update_val.type=%s).R   s¨   If the difference is related to the broadcast pattern, you can call the tensor.unbroadcast(var, axis_to_unbroadcast[, ...]) function to remove broadcastable dimensions.t   borrows;   Outputs must be theano Variable or Out instances. Received s	    of type sB   output must be a theano Variable or Out instance (or list of them)i    i   N(   R   t   tupleR   R   t   itemst	   ExceptionR   R!   R
   R   R	   t   iter_over_pairst
   ValueErrorR   R    R   t   getattrR   R   t   variableR1   t   strt   len(   R   R   t   replacet   updatesR,   R$   R+   t   replace_pairst   v_origt   v_replR0   R%   t   input_variablesR#   t
   store_intot
   update_valt   err_msgt   err_sugt   cloned_outputst   cloned_vR(   t   cloned_v_update(    (   R)   R*   R$   R+   R,   R-   R.   R/   s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyt   rebuild_collect_shared   s²    )!8	
		
		.							
t   Paramc           B` s,   e  Z d  Z d d e e d d d d  Z RS(   s   Deprecated. Use In instead.c	   	      C` sW   t  j d d d t t |   j | d | d | d | d | d | d	 | d
 | d  S(   NsM   The Param class is deprecated. Replace Param(default=N) by theano.In(value=N)t
   stackleveli   t   namet   valuet   mutableR   t   allow_downcastt   implicitR1   (   t   warningst   warnt   superRI   t   __init__(	   t   selfR8   t   defaultRK   RM   R   RN   RO   R1   (    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyRS     s    N(   t   __name__t
   __module__t   __doc__R   R   RS   (    (    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyRI     s   		c   '      C` sL  | d k r g  } n  | d k r* g  } n  |
 d k r` t j pE t j }
 |
 t k r` d }
 q` n  |
 t k r~ t d |  }
 n$ t |
  t k r’ t d |
  }
 n  t	 |  t
 t f  sΖ t d   n  t	 | t  rυ t	 | t
  rυ t d   n  t |  d k r2t d   t |  D  r2t d   n  g  |  D] } t | d |	 ^ q9} g  | D] } | j ^ q^} x` t |  D]R \ } } | | | d k r| j | | d  } t d	 | | | f   qqWt |  } y t
 | j    } Wn t k
 r| } n Xx3 | D]+ \ } } | | k rt d
 |   qqWg  | D] } | j rO| j ^ qO} | d k rg  } n- t	 | t
 t f  r¦t
 |  } n	 | g } | | } t | | d | d | d | d t d | } | \ } } } | \ } }  }! }" | d k rg  }# n2 t	 | t
 t f  rD| t |   }# n
 | d }# xB t | |  D]1 \ } }$ |$ | _ | j r^| | j | _ q^q^Wx |" D] }% |% |  k rγt d |% d |% j  d t d t d |  |% d t  }& n* t d |% d |% j  d t d t d t  }& | j! |&  qWt" | |# | d | d | d |
 d | d | S(   sΉ  
    Function-constructor for graphs with shared variables.

    Parameters
    ----------
    params : list of either Variable or In instances
        Function parameters, these are not allowed to be shared variables.
    outputs : list of Variables or Out instances
        Expressions to compute.
    mode : string or `theano.compile.Mode` instance
        Compilation mode.
    updates : iterable over pairs (shared_variable, new_expression). List, tuple or dict.
        Update the values for SharedVariable inputs according to these
        expressions
    givens : iterable over pairs (Var1, Var2) of Variables. List, tuple or dict.
        The Var1 and Var2 in each pair must have the same Type. Specific
        substitutions to make in the computation graph (Var2 replaces Var1).
    no_default_updates : either bool or list of Variables
        If True, do not perform any automatic update on Variables.
        If False (default), perform them all. Else, perform automatic updates
        on all Variables that are neither in "updates" nor in
        "no_default_updates".
    accept_inplace : bool
        True iff the graph can contain inplace operations prior to the
        optimization phase (default is False). *Note* this parameter is unsupported,
        and its use is not recommended.
    name : None or string
        Attaches a name to the profiling result of this function.
    allow_input_downcast : bool
        True means that the values passed as inputs when calling the function
        can be silently downcasted to fit the dtype of the corresponding
        Variable, which may lose precision. False means that it will only be cast to a more
        general, or precise, type. None (default) is almost like
        False, but allows downcasting of Python float scalars to
        floatX.
    profile : None, True, str, or ProfileStats instance
        Accumulate profiling information into a given ProfileStats instance.
        None is the default, and means to use the value of config.profile.
        If argument is `True` then a new ProfileStats instance will be used.
        If argument is a string, a new ProfileStats instance will be created
        with that string as its `message` attribute. This profiling object will
        be available via self.profile.
    on_unused_input : {'raise', 'warn','ignore', None}
        What to do if a variable in the 'inputs' list is not used in the graph.

    Returns
    -------
    theano.compile.Function
        A callable object that will compute the outputs (given the inputs) and
        update the implicit function arguments according to the `updates`.

    Notes
    -----
    Regarding givens: Be careful to make sure that these substitutions are
    independent--behaviour when Var1 of one pair appears in the graph leading
    to Var2 in another expression is undefined. Replacements specified with
    givens are different from optimizations in that Var2 is not expected to be
    equivalent to Var1.

    t   messages7   in pfunc() the first argument must be a list or a tuples6   no_default_update should be either a boolean or a listi    c         s` s   |  ] } t  | t  Vq d  S(   N(   R   R   (   t   .0R#   (    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pys	   <genexpr>  s   s[   The updates parameter must be an OrderedDict/dict or a list of lists/tuples with 2 elementsRN   i   sΏ   Variable %s is used twice in inputs to theano.function, at indices %i and %i.  This would result in values provided for it being ignored. Please do not duplicate variables in the inputs list.sΝ  You are trying to replace variable '%s' through the `givens` parameter, but this variable is an input to your function. Replacing inputs is currently forbidden because it has no effect. One way to modify an input `x` to a function evaluating f(x) is to define a new input `y` and use `theano.function([y], f(x), givens={x: g(y)})`. Another solution consists in using `theano.clone`, e.g. like this: `theano.function([x], theano.clone(f(x), replace={x: g(x)}))`.R;   R<   R,   R$   R+   R8   RL   RM   R1   t   updateR
   t   accept_inplaceRK   t   profilet   on_unused_inputt   output_keysN(#   R   R   R]   t   print_global_statsR   t   TrueR   R   R9   R   R   R2   R4   t   boolR!   R:   t   anyR5   R6   t   _pfunc_param_to_inR8   t	   enumeratet   indexR   t   setR3   t   AttributeErrort   RuntimeErrorR[   RH   R   R   t	   containerR   R   ('   t   paramsR   t   modeR<   t   givensR+   R\   RK   R,   t   allow_input_downcastR]   R^   R_   t   pR   t   inputt   in_variablesR%   R#   t   dup_v_it
   in_var_sett   givens_pairst   xt   yt   additional_outputst   out_listt   extended_outputst   output_varsR@   t   cloned_extended_outputst   other_stuffR)   R.   R/   R-   RE   t   ivt   svt   si(    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyt   pfunc  s    O		"
	%		
		
		c         C` sv   t  |  t  r! t d |    n  t  |  t  rI t d |  d | d |  St  |  t  r\ |  St d t |     d  S(   Ns#   Constants not allowed in param listR8   R   RN   s   Unknown parameter type: %s(   R   R   R!   R   R   R   (   t   paramR   RN   (    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyRd   ι  s    c         C` s!   t  |  t  r t |   S|  Sd S(   s  
    Return an iterator over pairs present in the 'pairs' input.

    Parameters
    ----------
    pairs : dictionary or iterable
        The pairs to iterate upon. These may be stored either as (key, value)
        items in a dictionary, or directly as pairs in any kind of iterable
        structure.

    Returns
    -------
    iterable
        An iterable yielding pairs.

    N(   R   t   dictR   (   t   pairs(    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyR5   σ  s    
(#   RX   t
   __future__R    R   R   RP   t   theanoR   t   sixR   t   theano.compileR   R   R   R   t   theano.compile.sharedvalueR	   R
   t   theano.compile.profilingR   t
   theano.gofR   R   t   loggingt	   getLoggert   _loggert   __docformat__R   Ra   R   RH   RI   R   Rd   R5   (    (    (    s4   /tmp/pip-build-X4mzal/theano/theano/compile/pfunc.pyt   <module>   s2   ο	Λ
