3
Ðñß^Õ  ã               @   s>   d dl Z d dlmZ d dlmZ G dd„ deƒZd
dd	„ZdS )é    N)Ú	normalize)Ú	Parameterc               @   s:   e Zd Zddd„Zdd„ Zd	d
„ Zdd„ Zedd„ ƒZdS )ÚSpectralNormÚweighté   r   çê-�™—q=c             C   s8   || _ || _|| _|dkr(tdj|ƒƒ‚|| _|| _d S )Nr   zIExpected n_power_iterations to be positive, but got n_power_iterations={})ÚnameÚdimÚnormÚ
ValueErrorÚformatÚn_power_iterationsÚeps)Úselfr   r   r	   r   r
   © r   ú:/home/yuhw/wc/SparseConvNet/sparseconvnet/spectral_norm.pyÚ__init__   s    zSpectralNorm.__init__c       	         sò   t |ˆ jd ƒ}t |ˆ jd ƒ}|}ˆ jdkrV|jˆ jf‡ fdd„t|jƒ ƒD ƒžŽ }|jdƒ}|j|dƒ}tjƒ �N xFtˆ j	ƒD ]8}t
tj|jƒ |ƒdˆ jd�}t
tj||ƒdˆ jd�}q‚W W d Q R X tj|tj||ƒƒ}|ˆ j|  }||fS )	NÚ_origÚ_ur   c                s   g | ]}|ˆ j kr|‘qS r   )r	   )Ú.0Úd)r   r   r   ú
<listcomp>   s    z/SpectralNorm.compute_weight.<locals>.<listcomp>r   )r	   r   éÿÿÿÿ)Úgetattrr   r	   ÚpermuteÚrangeÚsizeÚreshapeÚtorchÚno_gradr   r   ÚmatmulÚtr   Údotr
   )	r   Úmoduler   ÚuÚ
weight_matÚheightÚ_ÚvÚsigmar   )r   r   Úcompute_weight   s    



&zSpectralNorm.compute_weightc             C   sR   t || jƒ}t|| jƒ t|| jd ƒ t|| jd ƒ |j| jtjj|ƒƒ d S )Nr   r   )r   r   ÚdelattrÚregister_parameterr   Únnr   )r   r#   r   r   r   r   Úremove,   s
    zSpectralNorm.removec             C   sb   |j r6| j|ƒ\}}t|| j|ƒ t|| jd |ƒ n(t|| jd ƒj}t|| jƒjƒ j|ƒ d S )Nr   r   )Útrainingr*   Úsetattrr   r   Úrequires_gradÚdetach_Úrequires_grad_)r   r#   Úinputsr   r$   Zr_gr   r   r   Ú__call__3   s    zSpectralNorm.__call__c       
      C   s�   t |||||ƒ}| j| }|j|ƒ}t|j|ƒjddƒd|jd�}	t| |jƒ | j	|jd |ƒ | j
|j|jƒ | j
|jd |	ƒ | j|ƒ |S )Nr   r   )r	   r   r   r   )r   Ú_parametersr   r   Ú	new_emptyÚnormal_r   r+   r   r,   Úregister_bufferÚdataÚregister_forward_pre_hook)
r#   r   r   r	   r   r
   Úfnr   r&   r$   r   r   r   Úapply<   s    


zSpectralNorm.applyN)r   r   r   r   r   )	Ú__name__Ú
__module__Ú__qualname__r   r*   r.   r5   Ústaticmethodr=   r   r   r   r   r   
   s
   

	r   r   çê-�™—q=c             C   s&   | j jƒ d }tj| d||||ƒ | S )zX
    https://github.com/pytorch/pytorch/blob/master/torch/nn/utils/spectral_norm.py
    r   r   )r   Ú
ndimensionr   r=   )r#   r   r   r
   r	   r   r   r   Úspectral_normQ   s    rD   )r   rB   r   )r   Ztorch.nn.functionalr   Ztorch.nn.parameterr   Úobjectr   rD   r   r   r   r   Ú<module>   s   G