σ
ΰΖχXc           @` s±   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l Z d d l m	 Z	 d d l
 j j Z d d l m Z m Z m Z m Z m Z d e j f d     YZ d S(	   s    
    Tests for block sparse dot
i    (   t   absolute_importt   print_functiont   divisionN(   t   randn(   t   tensor(   t   sparse_block_dott   sparse_block_gemvt   sparse_block_outert   SparseBlockGemvt   SparseBlockOutert   BlockSparse_Gemv_and_Outerc           B` sΕ   e  Z d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e 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` sq   t  j   d  } t j j d k r+ d } n  t j j |  j d  |  _ t	 |  _
 t |  _ t |  _ t |  _ d  S(   Nt   FAST_COMPILEt   FAST_RUNt   constant_folding(   t   uttt   seed_rngt   Nonet   theanot   configt   modet   compilet   get_modet	   excludingR   t   gemv_opR   t   outer_opR   t
   gemv_classR	   t   outer_class(   t   selfR   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   setUp   s    
				c          ` s  d  d  d }  d } d   d  d } t  |   |   j d  } t j j  t j     f d	   t |  D  j d
  } t j    f d   t |  D  j d
  } t    |  |  j d  } t   |  j d  } | | | | | f S(   Ni   i   i   i   i   i   i   t   float32c         3` s   |  ] }       Vq d  S(   N(    (   t   .0t   _(   t   inputWindowSizet   nInputBlockt   permutation(    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pys	   <genexpr>-   s   t   int32c         3` s   |  ] }       Vq d  S(   N(    (   R   R   (   t   nOutputBlockt   outputWindowSizeR"   (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pys	   <genexpr>0   s   (   R   t   astypet   numpyt   randomR"   t   vstackt   range(   t	   inputSizet
   outputSizet	   batchSizet   inputt   inputIndicet   outputIndicet   weightt   bias(    (   R    R!   R$   R%   R"   sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt	   gemv_data    s$    	c          ` s  d   d  d }  d } d  d  d } t     |  |  j d  } t  |  |   j d  } t  |  |  j d  } t j j  t j     f d	   t |  D  j d
  } t j    f d   t |  D  j d
  } | | | | | f S(   Ni   i   i   i   i   i   i   R   c         3` s$   |  ] }  d    d  Vq d S(   i    t   sizeN(    (   R   R   (   R!   t   randintt   xWindowSize(    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pys	   <genexpr>F   s   R#   c         3` s$   |  ] }  d    d  Vq d S(   i    R4   N(    (   R   R   (   R$   R5   t   yWindowSize(    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pys	   <genexpr>H   s   (   R   R&   R'   R(   R5   R)   R*   (   t   xSizet   ySizeR-   t   ot   xt   yt   xIdxt   yIdx(    (   R!   R$   R5   R6   R7   sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt
   outer_data8   s     c      
   C` sΓ   xΌ t  |  j d  D]§ } x t  |  j d  D] } | | | f } xp t  | j d  D][ } | | | f }	 | |	 | f }
 |  | | d  d   f c t j | | | f |
  7<qX Wq. Wq W|  S(   Ni    i   (   R*   t   shapeR'   t   dot(   R:   t   Wt   ht   iIdxt   oIdxt   bt   jt	   outputIdxt   it   inputIdxt   w(    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt
   gemv_numpyM   s    Ac         C` sΘ   d d l  m } x± t |  j d  D] } | | | | | |  j d d  } | j | j d | j d | j d | j d f  } |  | c t  j | | j   |  j |  j d  7<q$ W|  S(   s&   
        Other implementation
        i    (   t   ix_i   i   i   (   R'   RM   R*   R@   t   swapaxest   reshapeRA   t   ravel(   R:   RB   RC   RD   RE   RM   RF   RK   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   gemv_numpy2X   s    '9:c         C` sp   d d l  m } xY t |  j d  D]D } | | | | | |  } |  | c t  j d | | |  7<q$ W|  S(   s&   
        Other implementation
        i    (   RM   s   ik,ijkl(   R'   RM   R*   R@   t   einsum(   R:   RB   RC   RD   RE   RM   RF   RK   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   gemv_numpy3d   s
    'c         C` sΊ   x³ t  | j d  D] } x t  | j d  D] } xw t  | j d  D]b } |  | | | f | | | f f c t j | | | d  d   f | | | d  d   f  7<qH Wq. Wq W|  S(   Ni    i   (   R*   R@   R'   t   outer(   R:   R;   R<   R=   R>   RF   RI   RG   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   outer_numpys   s    @(c         C` sο   t  j   } t  j   } t  j   } t  j   } t  j   } t | | | | |  } t j | | | | | g | d |  j } t	 j
   \ } }	 }
 } } | | |	 |
 | |  } t	 j | j | d d | |	 |
 |  } t j | |  d S(   sT   
        Compares the numpy version of sparseblockgemv to sparse_block_dot.
        R   t   axisi    N(   R   t   fmatrixt   ftensor4t   ftensor3t   imatrixR   R   t   functionR   R
   R3   RL   t   takeR   t   assert_allclose(   R   RF   RB   RC   RD   RE   R:   t   ft   W_valt   h_valt   iIdx_valt   b_valt   oIdx_valt   th_outt   ref_out(    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockdot|   s    *$c         C` s  t  j   } t  j   } t  j   } t  j   } t  j   } |  j | j | d d | | | |  } t j | | | | | g | d |  j	 } t
 j   \ } }	 }
 } } | | |	 |
 | |  } t
 j | j | d d | |	 |
 |  } t j | |  d S(   sL   
        Compares the numpy and theano versions of sparseblockgemv.
        RV   i    R   N(   R   RW   RX   RY   RZ   R   R\   R   R[   R   R
   R3   RL   R   R]   (   R   RF   RB   RC   RD   RE   R:   R^   R_   R`   Ra   Rb   Rc   Rd   Re   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockgemv   s    **$c         C` s7  t  j   } t  j   } t  j   } t  j   } t  j   } |  j | j | d d t  j t t t t f d  t  j	 |   | | |  } t
 j | | | | | g | d |  j } t j   \ } }	 }
 } } | t j | d d  |	 |
 | |  } t j | j | d d | |	 |
 |  } t j | |  d S(	   sm   
            Test the fortan order for W (which can happen in the grad for some
            graphs).
        RV   i    i   i   i   R   N(   i    i   i   i   (   R   RW   RX   RY   RZ   R   R\   t
   DimShufflet   Falset   as_tensor_variableR   R[   R   R
   R3   R'   RN   RL   R   R]   (   R   RF   RB   RC   RD   RE   R:   R^   R_   R`   Ra   Rb   Rc   Rd   Re   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockgemvF«   s"    *	$c   	      ` sΐ   t  j   \ } } } } } t j j |    t j j |      f d   }     f d   } d } t j | | | | g d  j d | t j | | | | g d  j d | d  S(   Nc         ` s   t  | |   |    S(   N(   R   (   RF   RC   RB   (   RD   RE   (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   metaopΠ   s    c         ` s(    j  |  j  d d | |     S(   NRV   i    (   R   R\   (   RF   RC   RB   (   RD   RE   R   (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   opΣ   s    gϊ~jΌth?R   t   eps(   R
   R3   R   R   t   constantR   t   verify_gradR   (	   R   R_   R`   Ra   Rb   Rc   Rl   Rm   Rn   (    (   RD   RE   R   sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockgemv_gradΘ   s    (c         ` s0  t  d d d  j d  } t j j d  d  d  d  d   f } t j j d  d  d  d  d   f } t  d d d d  j d  } t  d d  j d  } t j j |    t j j |      f d   }     f d   } t	 j
 | | | | g d  j t	 j
 | | | | g d  j d  S(   Ni   R   c         ` s   t  | |   |    S(   N(   R   (   RF   RC   RB   (   RD   RE   (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyRl   ε   s    c         ` s(    j  |  j  d d | |     S(   NRV   i    (   R   R\   (   RF   RC   RB   (   RD   RE   R   (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyRm   θ   s    R   (   R   R&   R'   R(   R"   R   R   R   Ro   R   Rp   R   (   R   R`   Ra   Rc   R_   Rb   Rl   Rm   (    (   RD   RE   R   sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockgemv_grad_1Ϊ   s    &&"c         C` s9  t  j   } t  j   } t  j   } t  j   } t  j   } |  j | j | d d | | | |  } t j | j	   | | | g  } t j
 | | | | | g | d |  j } t j   \ }	 }
 } } } | |	 |
 | | |  \ } } } | j | j k st  | j |
 j k st  | j |	 j k s5t  d  S(   NRV   i    R   (   R   RW   RX   RY   RZ   R   R\   R   t   gradt   sumR[   R   R
   R3   R@   t   AssertionError(   R   RF   RB   RC   RD   RE   R:   t   goR^   R_   R`   Ra   Rb   Rc   t   b_gt   W_gt   h_g(    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockgemv_grad_shapeξ   s    *!*!c         C` sι   t  j   } t  j   } t  j   } t  j   } t  j   } |  j | | | | |  } t j | | | | | g | d d d |  j } t j	   \ } }	 }
 } } | | |	 |
 | |  } t j
 | |	 |
 | |  } t j | |  d  S(   Nt   on_unused_inputt   warnR   (   R   RX   RY   RZ   R   R   R[   R   R
   R?   RU   R   R]   (   R   R:   R;   R<   R=   R>   t   outR^   t   o_valt   x_valt   y_valt   xIdx_valt   yIdx_valRd   Re   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_sparseblockouter  s    c         C` s   t  j   } t  j   } t  j   } t  j   } t  j   } |  j | | | | | g t | | | | |  g |  j   |  j  d  S(   N(	   R   RW   RX   RY   RZ   t   _compile_and_checkR   R3   R   (   R   RF   RB   RC   RD   RE   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_dot_infershape  s    	c         C` s   t  j   } t  j   } t  j   } t  j   } t  j   } |  j | | | | | g |  j | j | d d | | | |  g |  j   |  j	  d  S(   NRV   i    (
   R   RW   RX   RY   RZ   R   R   R\   R3   R   (   R   RF   RB   RC   RD   RE   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_gemv_infershape%  s    *	c         C` s   t  j   } t  j   } t  j   } t  j   } t  j   } |  j | | | | | g |  j | | | | |  g |  j   |  j  d  S(   N(   R   RX   RY   RZ   R   R   R?   R   (   R   R:   R;   R<   R=   R>   (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   test_outer_infershape2  s    	(   t   __name__t
   __module__R   t   staticmethodR3   R?   RL   RQ   RS   RU   Rf   Rg   Rk   Rq   Rr   Rz   R   R   R   R   (    (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyR
      s"   											(   t   __doc__t
   __future__R    R   R   R'   t   numpy.randomR   R   R   t   theano.tests.unittest_toolst   testst   unittest_toolsR   t   theano.tensor.nnet.blocksparseR   R   R   R   R	   t   InferShapeTesterR
   (    (    (    sI   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/tests/test_blocksparse.pyt   <module>   s   (