ó
†¶2_c           @   s|   d  Z  d d l Z d d l m Z d d l m Z d d l Td d l m	 Z	 d e f d	 „  ƒ  YZ
 d
 e f d „  ƒ  YZ d S(   sg  
Function to convert a SparseConvNet hidden layer to a dense convolutional
layer. Put a SparseToDense convolutional layer (or an ActivePooling layer) at
the top of your sparse network. The output can then pass to a dense
convolutional layers or (if the spatial dimensions have become trivial) a
linear classifier.

Parameters:
dimension : of the input field,
iÿÿÿÿN(   t   Function(   t   Modulei   (   t   *(   t   SparseConvNetTensort   SparseToDenseFunctionc           B   s&   e  Z e d  „  ƒ Z e d „  ƒ Z RS(   c         C   sN   | |  _  | |  _ |  j | | ƒ | j ƒ  } t j j | | | | | ƒ | S(   N(   t   input_metadatat	   dimensiont   save_for_backwardt   newt   sparseconvnett   SCNt   SparseToDense_updateOutput(   t   ctxt   input_featuresR   t   spatial_sizeR   t   nPlanest   output(    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyt   forward   s    			c         C   sS   | j  ƒ  } |  j \ } } t j j | |  j | | | j ƒ  ƒ | d  d  d  d  f S(   N(   R   t   saved_tensorsR	   R
   t   SparseToDense_updateGradInputR   t
   contiguoust   None(   R   t   grad_outputt
   grad_inputR   R   (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyt   backward.   s    	(   t   __name__t
   __module__t   staticmethodR   R   (    (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyR      s   t   SparseToDensec           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s#   t  j |  ƒ | |  _ | |  _ d  S(   N(   R   t   __init__R   R   (   t   selfR   R   (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyR   <   s    	c         C   s(   t  j | j | j | j |  j |  j ƒ S(   N(   R   t   applyt   featurest   metadataR   R   R   (   R   t   input(    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyR   A   s    c         C   s   | S(   N(    (   R   t   out_size(    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyt   input_spatial_sizeI   s    c         C   s&   d t  |  j ƒ d t  |  j ƒ d S(   Ns   SparseToDense(t   ,t   )(   t   strR   R   (   R   (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyt   __repr__L   s    (   R   R   R   R   R$   R(   (    (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyR   ;   s   			(   t   __doc__t   sparseconvnet.SCNR	   t   torch.autogradR    t   torch.nnR   t   utilst   sparseConvNetTensorR   R   R   (    (    (    s>   /home/yuhw/wc/tmp/SparseConvNet/sparseconvnet/sparseToDense.pyt   <module>   s   
"