B
    `                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlm  mZ ddl	m
Z
 ddl	mZ dd	d
dgZdddZddd	Zddd
ZdddZdddZdddZdd Zdd ZdS )z1Utilities for initializing neural network layers.    )absolute_import)division)print_functionN)
dtype_util)prefer_staticglorot_normalglorot_uniform	he_normal
he_uniformc                s   d fdd	S )aP  The Glorot normal initializer, aka Xavier normal initializer.

  It draws samples from a truncated normal distribution centered on 0
  with standard deviation (after truncation) given by
  `stddev = sqrt(2 / (fan_in + fan_out))` where `fan_in` is the number
  of input units in the weight tensor and `fan_out` is the number of
  output units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds.
      Default value: `None`.

  Returns:
    init_fn: A python `callable` which takes a shape `Tensor`, dtype and an
      optional scalar `int` number of batch dims and returns a randomly
      initialized `Tensor` with the specified shape and dtype.

  References:
    [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
    ([pdf](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
  r   c          	      s   t | ||ddd dS )Ng      ?fan_avgtruncated_normal)scalemodedistributionseed)_initialize)shapedtypebatch_ndims)r    v/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/experimental/nn/initializers/initializers.py<lambda>8   s   zglorot_normal.<locals>.<lambda>)r   r   )r   r   )r   r   r   "   s    c                s   d fdd	S )a$  The Glorot uniform initializer, aka Xavier uniform initializer.

  It draws samples from a uniform distribution within [-limit, limit]
  where `limit` is `sqrt(6 / (fan_in + fan_out))`
  where `fan_in` is the number of input units in the weight tensor
  and `fan_out` is the number of output units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds.
      Default value: `None`.

  Returns:
    init_fn: A python `callable` which takes a shape `Tensor`, dtype and an
      optional scalar `int` number of batch dims and returns a randomly
      initialized `Tensor` with the specified shape and dtype.

  References:
    [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
    ([pdf](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
  r   c          	      s   t | ||ddd dS )Ng      ?r   uniform)r   r   r   r   )r   )r   r   r   )r   r   r   r   R   s   z glorot_uniform.<locals>.<lambda>)r   r   )r   r   )r   r   r   =   s    c                s   d fdd	S )a>  He normal initializer.

  It draws samples from a truncated normal distribution centered on 0
  with standard deviation (after truncation) given by
  `stddev = sqrt(2 / fan_in)` where `fan_in` is the number of
  input units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds.
      Default value: `None`.

  Returns:
    init_fn: A python `callable` which takes a shape `Tensor`, dtype and an
      optional scalar `int` number of batch dims and returns a randomly
      initialized `Tensor` with the specified shape and dtype.

  References:
    [He et al., 2015](https://www.cv-foundation.org/openaccess/content_iccv_2015/html/He_Delving_Deep_into_ICCV_2015_paper.html)
    ([pdf](https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf))
  r   c          	      s   t | ||ddd dS )Ng       @fan_inr   )r   r   r   r   )r   )r   r   r   )r   r   r   r   n   s   zhe_normal.<locals>.<lambda>)r   r   )r   r   )r   r   r	   W   s    c                s   d fdd	S )a'  He uniform variance scaling initializer.

  It draws samples from a uniform distribution within [-limit, limit]
  where `limit` is `sqrt(6 / fan_in)`
  where `fan_in` is the number of input units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds.
      Default value: `None`.

  Returns:
    init_fn: A python `callable` which takes a shape `Tensor`, dtype and an
      optional scalar `int` number of batch dims and returns a randomly
      initialized `Tensor` with the specified shape and dtype.

  References:
    [He et al., 2015]
    (https://www.cv-foundation.org/openaccess/content_iccv_2015/html/He_Delving_Deep_into_ICCV_2015_paper.html)
    ([pdf](https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf))
  r   c          	      s   t | ||ddd dS )Ng       @r   r   )r   r   r   r   )r   )r   r   r   )r   r   r   r      s   zhe_uniform.<locals>.<lambda>)r   r   )r   r   )r   r   r
   s   s    c       
      C   sd   t |std|tj| dgd} t| |\}}t||||}	t||}t	| ||	 |||S )z-Samples a random `Tensor` per specified args.z0Argument `dtype` must be float type (saw: "{}").)r   )
r   Zis_floating	TypeErrorformatr   Zreshape_compute_fans_from_shape_summarize_fanscast_sample_distribution)
r   r   r   r   r   r   r   r   fan_outZfansr   r   r   r      s    
r   c             C   sr   t ddt |  }t j| d|ggdd} t | |dddg\}}}}t |}|d | }|d | }||fS )z9Extracts `fan_in, fan_out` from specified shape `Tensor`.r         )ZpaddingsZconstant_valuesr   )r   maximumsizepadsplitZreduce_prod)r   r   Znum_padZbatch_shapeZextra_shaper   r!   Zreceptive_field_sizer   r   r   r      s    
r   c             C   sb   t | |} t ||}t| }|dkr0| S |dkr<|S |dkrP| | d S td|dS )z2Combines `fan_in`, `fan_out` per specified `mode`.r   r!   r   g       @zUnrecognized mode: "{}".N)r   r   strlower
ValueErrorr   )r   r!   r   r   r   r   r   r      s    r   c             C   s   t | }|dkr8t|d }tjj| d|||dS |dkrftd| }tjj| | |||dS |dkrt|}tjj| d|||dS t	d
|d	S )
zHSamples from specified distribution (by appropriately scaling `var` arg.r   g۶%?g        )r   r   g      @Zuntruncated_normalz Unrecognized distribution: "{}".N)r(   r)   r   sqrttfrandomr   r   normalr*   r   )r   varr   r   r   stddevlimitr   r   r   r       s    
r    )N)N)N)N)N)r   )__doc__
__future__r   r   r   Ztensorflow.compat.v2compatZv2r,   Z&tensorflow_probability.python.internalr   r   __all__r   r   r	   r
   r   r   r   r    r   r   r   r   <module>   s$   





