U
    ds                     @   s4   d dl mZ d dlmZ d dlZG dd deZdS )    )schema)
ModelLayerNc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	RandomFourierFeaturesa  
    Implementation of random fourier feature map for feature processing.

    Applies sqrt(2 / output_dims) * cos(wx+b), where:
        output_dims is the output feature dimensions, and
        wx + b applies FC using randomized, fixed weight and bias parameters

    For more information, see the original paper:
        https://people.eecs.berkeley.edu/~brecht/papers/07.rah.rec.nips.pdf

    Inputs:
        output_dims -- output feature dimensions
        sigma -- bandwidth for the Gaussian kernel estimator
        w_init -- initialization options for weight parameter
        b_init -- initialization options for bias parameter

    Nrandom_fourier_featuresc           
         s  t t| j|||f| t|tjs,td| jd }	|	dksNtd|	 || _	| j	dksltd| j	 tt
j| j	ff| d| _|dkstd| |r|nd	dd
| df}|r|ndddt
j df}| jd| j	|	g||jd| _| jd| j	g||jd| _d S )NzIncorrect input typer      z&Expected input dimensions >= 1, got %sz'Expected output dimensions >= 1, got %soutputg        zExpected bandwidth > 0, got %sZGaussianFillg      ?)ZmeanZstdZUniformFill   )minmaxw)
param_nameshapeZinitializerZ	optimizerb)superr   __init__
isinstancer   ZScalarAssertionErrorZ
field_typer   output_dimsnpZfloat32Zget_next_blob_referenceoutput_schemapiZcreate_paramZNoOptimr   r   )
selfmodelinput_recordr   sigmaZw_initZb_initnamekwargsZ
input_dims	__class__ P/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layers/random_fourier_features.pyr      sF      zRandomFourierFeatures.__init__c                 C   sd   | | j | j| jg |d}||g|d}td| j	 }|j
|g| j |d d S )N
cosine_argnew_feature_vecg       @)scale)ZFCr   Zfield_blobsr   r   ZNextScopedBlobZCosr   sqrtr   ZScaler   )r   netr!   r"   r#   r   r   r    add_opsN   s    zRandomFourierFeatures.add_ops)NNr   )__name__
__module____qualname____doc__r   r&   __classcell__r   r   r   r    r      s      0r   )Zcaffe2.pythonr   Zcaffe2.python.layers.layersr   Znumpyr   r   r   r   r   r    <module>   s   