ó
àÆ÷Xc           @` s´  d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z e j j d ƒ Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d e j f d „  ƒ  YZ# d e j f d „  ƒ  YZ$ d e j f d „  ƒ  YZ% d „  Z& e' d k r°e j( ƒ  n  d S(   i    (   t   absolute_importt   print_functiont   division(   t   OrderedDictN(   t   xrange(   t   gof(   t   izip(   t   unittest_tools(   t   gradient(   t   config(   t   NullTypeg      ð?c         C` su   | d k r; t j j j g  |  D] } | d ^ q ƒ } n  t t | t j j d d d t |  ƒ d | d | ƒ ƒ ƒ S(   s‰   
    This implements the old grad_sources_inputs function in terms of
    the new interface so the tests don't need to be rewritten.
    i    t   costt   known_gradst   wrtt   consider_constantN(	   t   Nonet   theanoR   t   grapht   inputst   dictR   R   t   grad(   t   sourcesR   t   source(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   grad_sources_inputs   s    /'t   testgrad_sources_inputsc           B` s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` sQ   d t  j j f d „  ƒ  Y} | ƒ  j ƒ  } |  j t t | j t f g d ƒ d S(   s4   Test that it is not ok to return None from op.grad()t   retNonec           B` s    e  Z d Z d  „  Z d „  Z RS(   c         S` s7   t  j j ƒ  g } t  j j ƒ  g } t j |  | | ƒ S(   N(   R   t   tensort   vectorR   t   Apply(   t   selfR   t   outputs(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt	   make_node)   s    c         S` s   | \ } | \ } d  S(   N(    (   R   t   inpt   gradst   xt   gz(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   .   s    		(    (   t   __name__t
   __module__t	   __props__R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   &   s   	N(
   R   t   opt   OpR   t   assertRaisest	   TypeErrorR   t   outt   oneR   (   R   R   t   a(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_retNone1$   s    c         C` s    d t  j j f d „  ƒ  Y} t j j ƒ  } t j j ƒ  } | ƒ  j | ƒ } t | j t	 f g d ƒ | ƒ  j | | ƒ } |  j t t | j t	 f g d ƒ d S(   sL   Test that it is not ok to return the wrong number of gradient terms
        t   retOnec           B` s    e  Z d Z d  „  Z d „  Z RS(   c         W` s%   t  j j ƒ  g } t j |  | | ƒ S(   N(   R   R   R   R   R   (   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   ;   s    c         S` s   | d j  ƒ  g S(   Ni    (   t
   zeros_like(   R   R   R!   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   ?   s    (    (   R$   R%   R&   R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR/   8   s   	N(   R   R'   R(   R   R   R   R   R   R+   R,   R   R)   t
   ValueError(   R   R/   t   it   jt   a1t   a2(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_wrong_rval_len15   s    
c         ` s   t  j j ƒ  ‰  d t j j f ‡  f d †  ƒ  Y} | ƒ  j ƒ  } t | j d t	 f g d ƒ } |  j | | j d ˆ  k ƒ d S(   s-   Test grad is called correctly for a 1-to-1 opt   Oc           ` s&   e  Z d Z d  „  Z ‡  f d †  Z RS(   c         S` s7   t  j j ƒ  g } t  j j ƒ  g } t j |  | | ƒ S(   N(   R   R   t   matrixR   R   (   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   P   s    c         ` s   ˆ  f S(   N(    (   R   R    R!   (   t   gval(    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   U   s    (    (   R$   R%   R&   R   R   (    (   R9   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR7   M   s   	i    N(   R   R   R8   R   R'   R(   R   R   R   R,   R   t
   assertTrueR   (   R   R7   R4   t   g(    (   R9   s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_1in_1outI   s
    "
c         ` s   t  j j ƒ  ‰  d t j j f ‡  f d †  ƒ  Y} | ƒ  j ƒ  } t | j d t	 f g d ƒ } |  j | | j d ˆ  k ƒ d S(   s0   Test grad is called correctly for a 1-to-many opR7   c           ` s&   e  Z d Z d  „  Z ‡  f d †  Z RS(   c         S` sC   t  j j ƒ  g } t  j j ƒ  t  j j ƒ  g } t j |  | | ƒ S(   N(   R   R   R8   t   scalarR   R   (   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   b   s    c         ` s   | \ } | \ } } ˆ  f S(   N(    (   R   R    R!   R"   t   gz1t   gz2(   R9   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   g   s    	(    (   R$   R%   R&   R   R   (    (   R9   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR7   _   s   	i    N(   R   R   R8   R   R'   R(   R   R   R   R,   R   R:   R   (   R   R7   R4   R;   (    (   R9   s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_1in_Nout[   s
    "c         ` s±   t  j j ƒ  ‰  t  j j ƒ  ‰ d t j j f ‡  ‡ f d †  ƒ  Y} | ƒ  j ƒ  } t | j d t	 f g d ƒ } |  j | | j d ˆ  k ƒ |  j | | j d ˆ k ƒ d S(   s0   Test grad is called correctly for a many-to-1 opR7   c           ` s)   e  Z d Z d  „  Z ‡  ‡ f d †  Z RS(   c         S` sC   t  j j ƒ  t  j j ƒ  g } t  j j ƒ  g } t j |  | | ƒ S(   N(   R   R   R=   R8   R   R   (   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   w   s    c         ` s   | \ } } | \ } ˆ  ˆ f S(   N(    (   R   R    R!   t   x0t   x1R#   (   t   gval0t   gval1(    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   |   s    	(    (   R$   R%   R&   R   R   (    (   RC   RD   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR7   t   s   	i    i   N(   R   R   R=   R   R'   R(   R   R   R   R,   R   R:   R   (   R   R7   R4   R;   (    (   RC   RD   s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_Nin_1outo   s    %c         ` s±   t  j j ƒ  ‰  t  j j ƒ  ‰ d t j j f ‡  ‡ f d †  ƒ  Y} | ƒ  j ƒ  } t | j d t	 f g d ƒ } |  j | | j d ˆ  k ƒ |  j | | j d ˆ k ƒ d S(   s3   Test grad is called correctly for a many-to-many opR7   c           ` s)   e  Z d Z d  „  Z ‡  ‡ f d †  Z RS(   c         S` sO   t  j j ƒ  t  j j ƒ  g } t  j j ƒ  t  j j ƒ  g } t j |  | | ƒ S(   N(   R   R   R8   R   R   (   R   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR      s    c         ` s
   ˆ  ˆ f S(   N(    (   R   R    R!   (   RC   RD   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   ’   s    (    (   R$   R%   R&   R   R   (    (   RC   RD   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR7   Š   s   	i    i   N(   R   R   R8   R   R'   R(   R   R   R   R,   R   R:   R   (   R   R7   R4   R;   (    (   RC   RD   s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_Nin_Nout…   s    %
(   R$   R%   R.   R6   R<   R@   RE   RF   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   "   s   					t	   test_gradc           B` s˜   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` sS   t  j j ƒ  } t  j j t  j j d | ƒ } |  j t t  j | g | d d ƒd  S(   Ni    t   on_unused_inputt   ignore(	   R   R   R   R   t   grad_not_implementedt   addR)   R*   t   function(   R   R-   t   b(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_unimplemented_grad_funcœ   s    c         C` sS   t  j j ƒ  } t  j j t  j j d | ƒ } |  j t t  j | g | d d ƒd  S(   Ni    RH   RI   (	   R   R   R   R   t   grad_undefinedRK   R)   R*   RL   (   R   R-   RM   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_undefined_grad_func£   s    c         C` sW   d t  j f d „  ƒ  Y} t j j ƒ  } | ƒ  | ƒ } |  j t t j j | | ƒ d  S(   Nt   DummyOpc           B` s    e  Z d Z d  „  Z d „  Z RS(   c         S` s   t  j |  | g | j ƒ  g ƒ S(   N(   R   R   t   type(   R   R"   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   ¯   s    c         S` s   t  j j |  d | d ƒ g S(   Ni    (   R   R   RJ   (   R   R   t   output_grads(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   ²   s    (    (   R$   R%   R&   R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRQ   ¬   s   	(	   R   R(   R   R   R=   R)   R*   R   R   (   R   RQ   R-   RM   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_unimplemented_grad_grad©   s    	c         C` sW   d t  j f d „  ƒ  Y} t j j ƒ  } | ƒ  | ƒ } |  j t t j j | | ƒ d  S(   NRQ   c           B` s    e  Z d Z d  „  Z d „  Z RS(   c         S` s   t  j |  | g | j ƒ  g ƒ S(   N(   R   R   RR   (   R   R"   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   À   s    c         S` s   t  j j |  d | d ƒ g S(   Ni    (   R   R   RO   (   R   R   RS   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   Ã   s    (    (   R$   R%   R&   R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRQ   ½   s   	(	   R   R(   R   R   R=   R)   R*   R   R   (   R   RQ   R-   RM   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_undefined_grad_gradº   s    	c         C` s   t  j j d ƒ } t  j j d ƒ } t  j j | t  j j | | ƒ ƒ } d | _ t  j j | | ƒ } | j d k s{ t ‚ d  S(   Nt   AR"   t   fs   (df/dx)(   R   R   R8   R   t   dott   nameR   t   AssertionError(   R   RV   R"   RW   R;   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_nameË   s    $	c         C` sP   d „  } t  j j d d d g ƒ } | j d ƒ } t j j j | | g ƒ d  S(   Nc         S` s   |  |  S(   N(    (   R"   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   outputØ   s    iÜ  i   i   i   (   t   npt   randomt   RandomStatet   randnR   t   testsR   t   verify_grad(   R   R\   t   rngt   vx(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_duplicate_inputÓ   s    	c         C` se   d „  } t  j j d d d g ƒ } | j d ƒ } | j d d ƒ } t j j j | | | g ƒ d  S(   Nc         S` s"   t  j j |  t  j j | |  ƒ ƒ S(   N(   R   R   RX   (   R"   RV   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   å   s    iÜ  i   i   i   (   R]   R^   R_   R`   R   Ra   R   Rb   (   R   R   Rc   Rd   t   vA(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_quadraticá   s
    	c         C` se   d „  } t  j j d d d g ƒ } | j d ƒ } | j d d ƒ } t j j j | | | g ƒ d  S(   Nc         S` s   t  j j |  |  | ƒ S(   N(   R   R   RX   (   R"   RV   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR\   ó   s    iÜ  i   i   i   (   R]   R^   R_   R`   R   Ra   R   Rb   (   R   R\   Rc   Rd   Rf   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_quadratic_vectorï   s
    	c         C` se   d „  } t  j j d d d g ƒ } | j d ƒ } | j d d ƒ } t j j j | | | g ƒ d  S(   Nc         S` s&   t  j j |  |  t  j j | |  ƒ ƒ S(   N(   R   R   RX   (   R"   RV   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR     s    iÜ  i   i   i   (   R]   R^   R_   R`   R   Ra   R   Rb   (   R   R   Rc   Rd   Rf   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_cubicý   s
    	c         C` se   d „  } t  j j d d d g ƒ } | j d ƒ } | j d d ƒ } t j j j | | | g ƒ d  S(   Nc         S` s7   t  j j |  t  j j | |  ƒ ƒ } t  j j | |  ƒ S(   N(   R   R   RX   R   R   (   R"   RV   t	   orig_cost(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR\     s    $iÜ  i   i   i   (   R]   R^   R_   R`   R   Ra   R   Rb   (   R   R\   Rc   Rd   Rf   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_grad_quadratic  s
    	c         C` se   d „  } t  j j d d d g ƒ } | j d ƒ } | j d d ƒ } t j j j | | | g ƒ d  S(   Nc         S` s;   t  j j |  |  t  j j | |  ƒ ƒ } t  j j | |  ƒ S(   N(   R   R   RX   R   R   (   R"   RV   Rj   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR\     s    (iÜ  i   i   i   (   R]   R^   R_   R`   R   Ra   R   Rb   (   R   R\   Rc   Rd   Rf   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_grad_cubic  s
    	c         ` sb  t  j j ƒ  ‰  t  j j ƒ  ‰ ‡  ‡ f d †  } | t  j j ƒ  ƒ } | t  j j d d ƒ ƒ } d } d } d } t j j d d d g ƒ } t  j j ƒ  j } d }	 t j	 | | j
 | | ƒ d	 ƒ }
 t j	 ˆ  j | j
 | | ƒ ƒ ‰  t j	 ˆ j | j
 | ƒ ƒ ‰ | |
 ˆ  ˆ ƒ } | t j	 |	 |
 ƒ ˆ  ˆ ƒ } t j | | ƒ s^t | | f ƒ ‚ d  S(
   Nc         ` sk   t  j j |  ˆ  ƒ ˆ } t  j j j | ƒ } | j ƒ  } t j | |  ƒ } t  j |  ˆ  ˆ g | d d ƒS(   NRH   RI   (	   R   R   RX   t   nnett   sigmoidt   sumR   R   RL   (   t   Xt   Zt   HR   R;   (   t   WRM   (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   make_grad_func1  s
    t   dtypet   float64i   i   i   iÜ  i	   g     À_@(   R   R   R8   R   t   imatrixR]   R^   R_   Ru   t   castR`   t   allcloseRZ   (   R   Rt   t   int_funct
   float_funct   mt   dt   nRc   t   int_typet
   float_typeRp   t
   int_resultt   float_result(    (   Rs   RM   s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_int)  s"    #"c   	      C` s  t  j j d d ƒ } | j ƒ  } d | _ | j d } d | _ t  j j | | | ƒ } d | _ | j ƒ  } d | _ t j | | d t	 ƒ} t  j
 | g | ƒ } t j j d	 d
 d g ƒ } t j | j | j d ƒ ƒ } | | ƒ } t j | t j | j d | j ƒƒ st ‚ d  S(   NRY   R"   t   totali    t   num_elementst   silly_vectorR   t	   add_namesiÜ  i	   i   i   Ru   (   R   R   R   Ro   RY   t   shapet   allocR   R   t   FalseRL   R]   R^   R_   Rx   Ru   R`   Ry   t   onesRZ   (	   R   R"   R„   R…   R†   R   R;   RW   Rc   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_disconnectedO  s    				c         C` s„   d t  j j f d „  ƒ  Y} d t  j j f d „  ƒ  Y} t  j j ƒ  } | ƒ  | ƒ \ } } | ƒ  | | ƒ } t j | | ƒ } d  S(   Nt   Op1c           B` s)   e  Z d Z d  „  Z d „  Z d „  Z RS(   c         S` s1   t  j |  d | g d | j ƒ  t  j j ƒ  g ƒS(   NR   R   (   R   R   RR   R   R=   (   R   R"   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   o  s    c         S` s   t  t g g S(   N(   t   TrueRŠ   (   R   t   node(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   connection_patternr  s    c         S` s   | d j  ƒ  g S(   Ni    (   R0   (   R   R   RS   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   u  s    (    (   R$   R%   R&   R   R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   l  s   		t   Op2c           B` s    e  Z d Z d  „  Z d „  Z RS(   c         S` s+   t  j |  d | | g d t  j j ƒ  g ƒS(   NR   R   (   R   R   R   R=   (   R   RW   R;   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   }  s    c         S` s   | d j  ƒ  t ƒ  ƒ  g S(   Ni    (   R0   R
   (   R   R   RS   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR   €  s    (    (   R$   R%   R&   R   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyR‘   z  s   	(   R   R   R(   R   R   R   R   (   R   R   R‘   R"   RW   R;   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_disconnected_nanf  s    	c   	      C` sº   t  j j d ƒ } | d } t  j j d ƒ } | | } t  j | | | | | g ƒ \ } } } } | j d k sw t ‚ | j d k sŒ t ‚ | j d k s¡ t ‚ | j d k s¶ t ‚ d  S(	   NR"   i   t   zt   float32Rv   (   R”   s   float64(   R”   s   float64(   R”   s   float64(   R   R   t   fscalart   lscalarR   Ru   RZ   (	   R   R"   t   yR“   t   ct   dc_dxt   dc_dyt   dc_dzt   dc_dc(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_downcast_dtype‘  s    

*c         C` sÕ   t  j j ƒ  } t  j j ƒ  } | | } t | } t  j j | | d | g ƒ} t  j j | t ƒ } t  j | | g | | g ƒ } | d d ƒ \ } } t j | | ƒ sÑ t d t	 | ƒ d t	 | ƒ ƒ ‚ n  d  S(   NR   i   g      à?s$   Gradient using consider constant is s3    but gradient with respect to the same Constant is (
   R   R   R=   R,   R   RL   R]   Ry   RZ   t   str(   R   R"   R—   t   z_xt   z_onet   g_xt   g_oneRW   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_constant¥  s    

(   R$   R%   RN   RP   RT   RU   R[   Re   Rg   Rh   Ri   Rk   Rl   Rƒ   RŒ   R’   R   R£   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRG   š   s    												&		+	c       
   C` sÿ  t  j j d ƒ }  t  j j d ƒ } t  j j d ƒ } |  | } d | _ t  j j d ƒ } | | } d | _ | | } d | _ | | } d | _ t  j j | ƒ } d	 | _ | g | g | | g | | | g | | |  | g g }	 | | | g }
 t j	 j
 d
 d d g ƒ } | j d ƒ | j d ƒ | j ƒ  g } g  t |
 | ƒ D]" \ } } t j | j | ƒ ^ qF} t  j j | |
 d d ƒ} t  j |
 | ƒ } | | Œ  } xQ|	 D]I} t  j j | | d d ƒ} t t | | ƒ ƒ } t  j j d	 d  d | d |
 d d ƒ } t  j |
 | ƒ } | | Œ  } t | ƒ t | ƒ k sGt ‚ x­ t | | |
 ƒ D]™ \ } } } t j | | ƒ sZt d ƒ t | ƒ t | ƒ t | ƒ t | ƒ x4 | D], } t | d t  j |
 | | ƒ | Œ  ƒ q´Wt sót ‚ qZqZWq®Wd  S(   Ni
   R"   t   tt   ftR˜   t   ctt   pR—   R   iÜ  i   i   t   disconnected_inputsRI   R   R   t   Failuret   :(   R   R   t   arangeR=   t   iscalarRY   R   t   sqrR]   R^   R_   R`   t   randintt   zipRx   Ru   R   RL   R   R   R   t   lenRZ   Ry   t   printRŠ   (   t
   full_rangeR"   R¤   R¥   t   coeffsR¦   R§   R—   R   t   layersR   Rc   t   valuest   iptt   valuet
   true_gradst   layert   firstt   knownt   fullR-   RM   t   vart   v(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_known_grads¼  sN    
	
	
	
		6'8'"




*c          C` sV   t  j j ƒ  }  t  j j |  |  ƒ } | j i d |  6ƒ } t j | d ƒ sR t ‚ d  S(   Ni   g      ð?(   R   R   R¬   R   t   evalR]   Ry   RZ   (   R"   R;   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt	   test_dxdxð  s    
c          C` s¢   t  j j ƒ  }  t  j j ƒ  } t  j j d d  d i | |  6d |  ƒ } t  j | g | ƒ } d }  t j	 t  j
 j d ƒ } | | ƒ } t j | | ƒ sž t ‚ d  S(   NR   R   R   iýÿÿÿg333333ã?(   R   R   R¬   R=   R   R   R   RL   R]   Rx   R	   t   floatXRy   RZ   (   R"   t
   g_expectedt   g_gradRW   t   gvt   g_actual(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_known_grads_integers  s    (c          C` sž   t  j j ƒ  }  t  j j ƒ  } |  | } | j t  j j k sC t ‚ y0 t  j j | |  | g d i t ƒ  ƒ  | 6ƒWn t  j j	 k
 r d  SXt d ƒ ‚ d  S(   NR   s'   An undefined gradient has been ignored.(
   R   R   R¬   Ru   t   discrete_dtypesRZ   R   R
   R   t   NullTypeGradError(   R"   R—   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_undefined_cost_grad  s    
0c          C` s§   t  j j ƒ  }  t  j j ƒ  } |  | } | j t  j j k sC t ‚ y9 t  j j | |  | g d i t j ƒ  ƒ  | 6d d ƒWn t  j j	 k
 r– d  SXt d ƒ ‚ d  S(   NR   R¨   t   raises)   A disconnected gradient has been ignored.(
   R   R   R¬   Ru   RÈ   RZ   R   R   t   DisconnectedTypet   DisconnectedInputError(   R"   R—   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_disconnected_cost_grad(  s    
9c          C` sÎ  t  j j d ƒ }  t  j j d ƒ } t  j t j j d d ƒ ƒ } t  j t j j d d ƒ ƒ } t  j j t  j j |  | ƒ ƒ } t  j j t  j j | | ƒ ƒ } t  j j	 | | ƒ j
 ƒ  } | t  j j	 | j
 ƒ  ƒ 7} t  j j	 | j
 ƒ  ƒ } | g | g g } | | g }	 | g |  g g }
 | |  g } t j j d d d g ƒ } | j d ƒ | j d ƒ g } g  t | | ƒ D]" \ } } t j | j | ƒ ^ qw} | | g } | | } t  j | | ƒ } t  j | | ƒ } | | Œ  } d  } g  } xp t d ƒ D]b } t  j d	 | | d
 |
 | d | d |	 | ƒ \ } } t t |
 | | ƒ ƒ } | j | ƒ qþWt  j | | ƒ } | | Œ  } xE t | | ƒ D]4 \ } } t j
 t j | | ƒ ƒ d k  s’t ‚ q’Wd  S(   NR"   R¤   i   i   i   iÜ  i   i   R   t   endt   startR   gñhãˆµøä>(   R   R   t   fvectort   sharedR]   R^   R`   t   tanhRX   R­   Ro   R_   R¯   Rx   Ru   R   RL   R   R   t   subgraph_gradR   R   t   extendt   absRZ   (   R"   R¤   t   w1t   w2R4   R5   t   cost2t   cost1t   paramst   costst	   grad_endsR   Rc   Rµ   R¶   R·   R   R   R¸   t	   next_gradt   param_gradsR2   t
   param_gradt   pgradst	   true_gradt   pgrad(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_subgraph_grad;  sB    !!8
	t   TestConsiderConstantc           B` s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C` s,   t  j ƒ  t j j d t  j ƒ  ƒ |  _ d  S(   Nt   seed(   t   uttt   seed_rngR]   R^   R_   t
   fetch_seedRc   (   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   setUpk  s    
c         C` su   t  j j d ƒ } | t j | ƒ } t  j | g | ƒ } t j g  | j j j	 ƒ  D] } | j
 ^ qS k sq t ‚ d  S(   NR"   (   R   R   R8   R   R   RL   t   consider_constant_t   makert   fgrapht   toposortR'   RZ   (   R   R"   R—   RW   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_op_removedo  s
    c   
      C` sM  t  j } t j |  j j d d ƒ d t j ƒ} | j d ƒ } | t	 j
 | ƒ | f | t	 j
 | j | ƒ ƒ | j | ƒ f t	 j
 | ƒ | j d ƒ f | d t	 j
 | ƒ d | d f g } x† | D]~ \ } } t	 j | j ƒ  | ƒ } t  j | g | d d ƒ} t  j | g | d d ƒ}	 t j | | ƒ |	 | ƒ ƒ sÇ t ‚ qÇ Wd  S(   Ni   Ru   R"   g        i   RH   RI   (   R   R   R]   t   asarrayRc   R`   R	   RÂ   R8   R   R   t   expt   constantR   Ro   RL   Ry   RZ   (
   R   t   TR-   R"   t   expressions_gradientst   exprt	   expr_gradR;   RW   t   f2(    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRG   x  s    	(((   R$   R%   Rê   Rï   RG   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRå   i  s   			t   TestZeroGradc           B` s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C` s,   t  j ƒ  t j j d t  j ƒ  ƒ |  _ d  S(   NRæ   (   Rç   Rè   R]   R^   R_   Ré   Rc   (   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRê   ’  s    
c         C` su   t  j j d ƒ } | t j | ƒ } t  j | g | ƒ } t j g  | j j j	 ƒ  D] } | j
 ^ qS k sq t ‚ d  S(   NR"   (   R   R   R8   R   t	   zero_gradRL   t
   zero_grad_Rì   Rí   Rî   R'   RZ   (   R   R"   R—   RW   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRï   –  s
    c   
      C` sM  t  j } t j |  j j d d ƒ d t j ƒ} | j d ƒ } | t	 j
 | ƒ | f | t	 j
 | j | ƒ ƒ | j | ƒ f t	 j
 | ƒ | j d ƒ f | d t	 j
 | ƒ d | d f g } x† | D]~ \ } } t	 j | j ƒ  | ƒ } t  j | g | d d ƒ} t  j | g | d d ƒ}	 t j | | ƒ |	 | ƒ ƒ sÇ t ‚ qÇ Wd  S(   Ni   Ru   R"   g        i   RH   RI   (   R   R   R]   Rð   Rc   R`   R	   RÂ   R8   R   Rù   Rñ   Rò   R   Ro   RL   Ry   RZ   (
   R   Ró   R-   R"   Rô   Rõ   Rö   R;   RW   R÷   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRG   Ÿ  s    	(((   R$   R%   Rê   Rï   RG   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRø     s   			t   TestDisconnectedGradc           B` s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s,   t  j ƒ  t j j d t  j ƒ  ƒ |  _ d  S(   NRæ   (   Rç   Rè   R]   R^   R_   Ré   Rc   (   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRê   ¹  s    
c         C` su   t  j j d ƒ } | t j | ƒ } t  j | g | ƒ } t j g  | j j j	 ƒ  D] } | j
 ^ qS k sq t ‚ d  S(   NR"   (   R   R   R8   R   t   disconnected_gradRL   t   disconnected_grad_Rì   Rí   Rî   R'   RZ   (   R   R"   R—   RW   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRï   ½  s
    c   
      C` s2  t  j } t j |  j j d d ƒ d t j ƒ} | j d ƒ } | t	 j
 | ƒ | f | t	 j
 | j | ƒ ƒ | j | ƒ f | d t	 j
 | ƒ d | d f g } x† | D]~ \ } } t	 j | j ƒ  | ƒ } t  j | g | d d ƒ} t  j | g | d d ƒ}	 t j | | ƒ |	 | ƒ ƒ s¬ t ‚ q¬ Wd  S(   Ni   Ru   R"   i   RH   RI   (   R   R   R]   Rð   Rc   R`   R	   RÂ   R8   R   Rü   Rñ   R   Ro   RL   Ry   RZ   (
   R   Ró   R-   R"   Rô   Rõ   Rö   R;   RW   R÷   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRG   Æ  s    	((c         C` s[   t  j } | j d ƒ } t j | ƒ } | j j j | j ƒ } | t g g k sW t	 ‚ d  S(   NR"   (
   R   R   R8   R   Rü   t   ownerR'   R   RŠ   RZ   (   R   Ró   R"   R—   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_connection_patternÜ  s
    	c         C` sü   t  j } t j |  j j d d ƒ d t j ƒ} | j d ƒ } |  j	 t
 j t
 j t
 j | ƒ j ƒ  | ƒ t
 j | t
 j | ƒ j ƒ  | ƒ } | j d ƒ } | j d ƒ } | t
 j | ƒ } |  j	 t
 j t
 j | j ƒ  | ƒ t
 j | j ƒ  | ƒ d  S(   Ni   Ru   R"   R-   RM   (   R   R   R]   Rð   Rc   R`   R	   RÂ   R8   R)   R   RÍ   R   Rü   Ro   (   R   Ró   R-   R"   R—   RM   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_disconnected_pathsä  s    	%(   R$   R%   Rê   Rï   RG   Rÿ   R   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyRû   ·  s
   					c          C` s  t  j j ƒ  }  t  j j t j |  d d ƒ d |  ƒ } t  j j |  d |  ƒ } t  j |  g d | | g ƒ} t  j j d k rÊ | j	 j
 j ƒ  } t g  | D] } t | j t j ƒ ^ qœ ƒ sÊ t ‚ n  | d ƒ } t j | d	 ƒ sî t ‚ t j | d | d ƒ st ‚ d  S(
   Niÿÿÿÿi   i   R   t   FAST_COMPILEg       @i   i    (   i   i   (   R   R   R=   R   R   t	   grad_clipRL   R	   t   modeRì   Rí   Rî   t   anyt
   isinstanceR'   t   GradClipRZ   R]   Ry   (   R"   R“   t   z2RW   t   topoR   R+   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   test_grad_clip  s    (2t   __main__()   t
   __future__R    R   R   t   collectionsR   t   unittestt   numpyR]   t	   six.movesR   R   R   t   theano.compatR   t   theano.testsR   Rç   R   R	   t   theano.gof.null_typeR
   R   t   as_tensor_variableR,   R   t   TestCaseR   RG   R¿   RÁ   RÇ   RÊ   RÎ   Rä   Rå   Rø   Rû   R	  R$   t   main(    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/tests/test_gradient.pyt   <module>   s8   	xÿ #	4					.''J	