ó
àÆ÷Xc           @` sô   d  d l  m Z m Z m Z d  d l Z d  d l 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 e j j j e j ƒ Z d e j f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   config(   t   tensor(   t   Scan(   t   unittest_toolst   TestGaussNewtonc           B` s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s‡   
    Regression test for code exhibiting various optimization errors.

    This test case is based on code by Sigurd Spieckermann.
    c         C` s   t  j j t j ƒ  ƒ |  _ d  S(   N(   t   numpyt   randomt   RandomStatet   uttt
   fetch_seedt   rng(   t   self(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   setUp   s    c      
   ` s—  | d k r' | | f } | d f } n | | | f } | | d f } t  j |  j j d | ƒ j t j ƒ d t ƒ} t  j |  j j d | ƒ j t j ƒ d t ƒ} | d k rÒ t j	 d ƒ }	 t j	 d ƒ }
 n t j
 d ƒ }	 t j
 d ƒ }
 | j d t ƒ |	 j _ | j d t ƒ |
 j _ t  j d |  j j d | d f ƒ j t j ƒ d t ƒ‰ t  j d |  j j d d ƒ j t j ƒ d t ƒ‰  t  j d |  j j d d ƒ j t j ƒ d t ƒ} t  j t j d ƒ j t j ƒ d t ƒ‰ t  j t j d ƒ j t j ƒ d t ƒ} ˆ ˆ  | ˆ | g } ‡  ‡ ‡ f d †  } | d k rmt j d	 d ƒ j t j ƒ } n! t j d	 | d ƒ j t j ƒ } t  j | d
 |	 g d | g ƒ\ } } t j | | ƒ | } t | ƒ } |
 | d j d d ƒ j ƒ  } t  j | d t g  | D] } | d | f ^ qƒ ƒ} | d | d | d | d t j d ƒ ƒ } t  j g  | g | d i | |	 6| |
 6d | ƒ} | ƒ  d  S(   Ni   t   sizet   borrowt   inputst   targetsg{®Gáz„?i
   c         ` s3   t  j t  j | ˆ  ƒ t  j |  ˆ ƒ ˆ ƒ } | S(   N(   t   Tt   tanht   dot(   t   x_tt   h_tm1t   h(   t   W_hht   W_xht   b_h(    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   stepC   s    /g        t	   sequencest   outputs_infoi   t   axist   replacet   vt   costt
   parameterst   dampg      ð?t   givenst   mode(   i
   i
   (   i
   i   (   t   theanot   sharedR   t   uniformt   astypeR   t   floatXt   TrueR   t   matrixt   tensor3t	   get_valuet   tagt
   test_valueR   t   zerost   alloct   scanR   t   GaussNewtonMatrixt   sumt   meant   clonet   dictt   constantt   function(   R   t   num_featurest   num_timestepst
   batch_sizeR'   t   inputs_sizet   targets_sizeR   R   t   xt   tt   W_hyt   b_yt   paramsR   t   h_0R   t   updatest   yt   gnR#   t   pit   cost_t   Gvt   f(    (   R   R   R   sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   _run   s^    !	!		%	%	**!!	 ,**	c         C` s    |  j  d d d d d t ƒd  S(   Nid   i
   R?   i   R'   (   RO   R'   (   R   (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt
   test_batchl   s    c         C` s/   t  j d ƒ } |  j d d d d d | ƒd  S(   Nt'   remove_constants_and_unused_inputs_scanid   i
   R?   i   R'   (   R'   t	   excludingRO   (   R   t   mode_exc(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_nobatchq   s    (   t   __name__t
   __module__t   __doc__R   RO   RP   RT   (    (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyR      s
   		U	R6   c           B` s   e  Z d  „  Z d „  Z RS(   c         C` s   | |  _  d  S(   N(   t   _s(   R   t   s(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   __init__{   s    c   
      C` s´   t  j |  j | | ƒ } t  j t  j t  j | |  j ƒ | ƒ |  j d | g ƒ} t  j t  j | |  j ƒ | d | | g ƒ} g  t | | ƒ D] \ } }	 | | |	 ^ q } | S(   Nt   consider_constant(   R   t   RopRX   t   gradR7   t   zip(
   R   R"   R#   R$   R%   t   Jvt   HJvt   JHJvt   JHJvit   vi(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   __call__€   s    .0(   RU   RV   RZ   Rd   (    (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyR6   z   s   	t   TestPushOutScanOutputDotc           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s  
    Test class for the PushOutScanOutput optimizer in the case where the inner
    function of a scan op has an output which is the result of a Dot product
    on a non-sequence matrix input to scan and a vector that is the result of
    computation in the inner function.
    c         C` s†  t  j ƒ  } t  j ƒ  } t  j | | ƒ } t j d ƒ } t j | | g t  j | | ƒ d | ƒ} t j	 d ƒ } t j | | g t  j | | ƒ d | ƒ} g  | j
 j j ƒ  D] } t | j t ƒ r¯ | ^ q¯ d }	 t |	 j j ƒ d k sõ t ‚ t |	 j j d t  j ƒ st ‚ t j j d ƒ j t j ƒ }
 t j j d	 ƒ j t j ƒ } | |
 | ƒ } | |
 | ƒ } t j | | ƒ d S(
   sy   
        Test the case where the vector input to the dot is not already an
        output of the inner function.
        R5   R'   t   scanOp_pushout_outputi    i   i   i   N(   i   i   (   R   t   vectorR.   R   R'   t	   includingR(   R<   t   jacobianRR   t   makert   fgrapht   toposortt
   isinstancet   opR   t   lent   outputst   AssertionErrort   DotR   R	   R+   R   R,   R   t   assert_allclose(   R   R"   t   mt   outputt   opt_modet   f_optt   no_opt_modet   f_no_optt   nodet	   scan_nodet   v_valuet   m_valuet
   output_optt   output_no_opt(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_dot_not_output•   s     **"#c      	   C` s¶  t  j ƒ  } t  j ƒ  } d „  } t j d | d d g d d | d | ƒ \ } } t j d ƒ } t j | | g | d | ƒ} t j d	 ƒ } t j | | g | d | ƒ}	 g  | j	 j
 j ƒ  D] }
 t |
 j t ƒ r¿ |
 ^ q¿ d
 } t | j j ƒ d k st ‚ t | j j d
 t  j ƒ s(t ‚ t j j d ƒ j t j ƒ } t j j d ƒ j t j ƒ } | | | ƒ } |	 | | ƒ } t j | d
 | d
 ƒ t j | d | d ƒ d S(   s{   
        Test the case where the vector input to the dot is already a nitsot
        output of the inner function.
        c         S` s    |  d } t  j | | ƒ | f S(   Ni   (   R   R   (   t   vectt   matt   vect_squared(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt	   inner_fctÂ   s    
t   fnR   i   R   t   non_sequencesR5   R'   Rf   i    i   i   i   i   N(   i   i   (   i   i   (   R   R.   R(   R5   t   NoneR'   Rh   R<   RR   Rj   Rk   Rl   Rm   Rn   R   Ro   Rp   Rq   Rr   R   R	   R+   R   R,   R   Rs   (   R   t   at   bR„   Rp   RH   Rv   Rw   Rx   Ry   Rz   R{   t   a_valuet   b_valueR~   R   (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_dot_nitsot_output¹   s*    	"#c      	   C` s¹  t  j ƒ  } t  j ƒ  } d „  } t j d | d | d d g d | d | ƒ \ } } t j d ƒ } t j | | g | d | ƒ} t j d	 ƒ } t j | | g | d | ƒ}	 g  | j	 j
 j ƒ  D] }
 t |
 j t ƒ rÂ |
 ^ qÂ d } t | j j ƒ d
 k st ‚ t | j j d t  j ƒ s+t ‚ t j j d ƒ j t j ƒ } t j j d ƒ j t j ƒ } | | | ƒ } |	 | | ƒ } t j | d | d ƒ t j | d | d ƒ d S(   s›   
        Test the case where the vector input to the dot is not already a
        non-nitsot (in this case a sitsot) output of the inner function.
        c         S` s&   | |  } t  j | | ƒ } | | f S(   N(   R   R   (   t   seq1t   previous_output1t   nonseq1t   output1t   output2(    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyR„   ñ   s    
R…   R   i    R   R†   R5   R'   Rf   i   i   i   i   i   N(   i   i   (   i   i   (   R   R.   R(   R5   R‡   R'   Rh   R<   RR   Rj   Rk   Rl   Rm   Rn   R   Ro   Rp   Rq   Rr   R   R	   R+   R   R,   R   Rs   (   R   Rˆ   R‰   R„   Rp   RH   Rv   Rw   Rx   Ry   Rz   R{   RŠ   R‹   R~   R   (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_dot_sitsot_outputè   s*    	"#(   RU   RV   RW   R€   RŒ   R’   (    (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyRe      s   	$	/t   TestPushOutSumOfDotc           B` s    e  Z d  Z d „  Z d „  Z RS(   sÇ   
    Test case for the PushOutScanOutput optimizer in the case where the scan
    is used to compute the sum over the dot products between the corresponding
    elements of two list of matrices.
    c         ` sM  d } d } d } d } t  j t j j d | | f d d ƒ j t j ƒ ƒ ‰  d ˆ  _ t  j ˆ  j	 ƒ  ƒ ‰ d ˆ _ t  j ˆ  j	 ƒ  ƒ ‰ d	 ˆ _ t
 j d
 ƒ } t j j d | | | f d d ƒ j t j ƒ } t
 j d ƒ } | } t
 j d ƒ }	 | }
 t
 j t j t j d ƒ | | ƒ } ‡  ‡ ‡ f d †  } t j d ƒ } t  j | d | | |	 g d | d | d d d | ƒ\ } } | d j ƒ  } t
 j | ˆ  ˆ ˆ g ƒ } t  j d | | |	 g d | d | ƒ } t j d ƒ } t  j | d | | |	 g d | d | d d d | ƒ\ } } | d j ƒ  } t
 j | ˆ  ˆ ˆ g ƒ } t  j d | | |	 g d | d | ƒ } g  | j j j ƒ  D] } t | j t ƒ rƒ| ^ qƒd } xg | j j D]Y } t | j j t
 j j ƒ ot g  | j j  D] } t | t
 j! ƒ ^ qéƒ s¸t" ‚ q¸W| | | |
 ƒ } | | | |
 ƒ } t# j$ | | ƒ d S(   s  
        This test case comes from https://github.com/rizar/scan-grad-speed and
        is an example of actual computation done with scan in the context of
        machine translation

        'dim' has been reduced from 1000 to 5 to make the test run faster
        iP   i2   i   R   t   scaleg-Cëâ6?t   Ut   Vt   WRB   t   rit   zii    c         ` s   | | j  ˆ  ƒ } | | j  ˆ ƒ } t j j | ƒ } t j j | ƒ } | | } |  | j  ˆ ƒ }	 t j |	 ƒ }
 | |
 d | | } | S(   Ni   (   R   R   t   nnett   sigmoidR   (   RB   R˜   R™   R   t   pre_rt   pre_zt   rt   zt   after_rt   pre_ht   new_ht   res_h(   R•   R–   R—   (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt	   rnn_step1B  s    
R5   R   t   n_stepsR   t   namet   fpass1R'   iÿÿÿÿR   Rp   Rf   i   Ni   (%   R(   R)   R   R	   t   normalR+   R   R,   R¦   R0   R   R/   R4   t   castR'   Rh   R5   R7   R]   R<   RR   Rj   Rk   Rl   Rm   Rn   R   Rp   t   ownert   elemwiset   Elemwiset   anyR   Rr   Rq   R   Rs   (   R   R?   t   seq_lent   n_wordst   dimRB   t   x_valueR˜   t   ri_valueR™   t   zi_valuet   initR¤   Rv   R   t   _R#   t   grad1Rw   Rx   Ry   Rz   t   scan_node_gradRu   t   it   f_opt_outputt   f_no_opt_output(    (   R•   R–   R—   sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_machine_translation  sV    
			%!	!	"6c         ` s  t  j ƒ  } t  j ƒ  } t  j ƒ  } t j t j j d d ƒ ƒ j t j	 ƒ ‰ t j t j j d d ƒ ƒ j t j	 ƒ ‰  ‡  ‡ f d †  } t  j
 t j j d d ƒ ƒ } t j d ƒ } t j | d	 | | | g d
 | d | ƒ\ } } | d }	 t j | | | g |	 d | ƒ}
 t j d ƒ } t j | d	 | | | g d
 | d | ƒ\ } } | d }	 t j | | | g |	 d | ƒ} t j j d ƒ j t j	 ƒ } t j j d ƒ j t j	 ƒ } t j j d ƒ j t j	 ƒ } |
 | | | ƒ } | | | | ƒ } t j | | ƒ d S(   sa   
        Test the case where the initial value for the nitsot output is
        non-zero
        R   i   i   i   i   c         ` sB   t  j |  ˆ ƒ | } t  j | ˆ  ƒ } t  j | | ƒ } | | S(   N(   R   R   (   R   t   seq2t   seq3t   previous_outputt   temp1t   temp2t
   dot_output(   R•   R—   (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyR„   €  s    i   R5   R   R   R'   iÿÿÿÿRf   i   N(   i   i   (   i   i   (   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   R   R/   R(   R)   R   R	   R¨   R+   R   R,   t   as_tensor_variableR'   Rh   R5   R<   RR   R   Rs   (   R   t   input1t   input2t   input3R„   R´   Rv   R   Rµ   Ru   Rw   Rx   Ry   t   input1_valuet   input2_valuet   input3_valueR~   R   (    (   R•   R—   sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   test_non_zero_inits  s:    **
	
	(   RU   RV   RW   R»   RÉ   (    (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyR“     s   	U(    (   t
   __future__R    R   R   R   t   unittestR(   R   R   R   t   theano.scan_module.scan_opR   t   theano.testsR   R   t   compileR'   t   get_modet   TestCaseR   t   objectR6   Re   R“   (    (    (    sF   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_opt.pyt   <module>   s   lŠ