σ
ΎχXc           @@  s  d  d l  m Z d  d l Z d d l m Z d d l m Z d d l m Z d e	 f d     YZ
 d	 e
 f d
     YZ d e
 f d     YZ d e
 f d     YZ d e
 f d     YZ e Z e Z e Z e Z e Z e Z e Z d   Z d d  Z d   Z d S(   i    (   t   absolute_importNi   (   t   backend(   t   serialize_keras_object(   t   deserialize_keras_objectt
   Constraintc           B@  s   e  Z d    Z d   Z RS(   c         C@  s   | S(   N(    (   t   selft   w(    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt   __call__
   s    c         C@  s   i  S(   N(    (   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt
   get_config   s    (   t   __name__t
   __module__R   R   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR      s   	t   MaxNormc           B@  s/   e  Z d  Z d d d  Z d   Z d   Z RS(   sκ  MaxNorm weight constraint.

    Constrains the weights incident to each hidden unit
    to have a norm less than or equal to a desired value.

    # Arguments
        m: the maximum norm for the incoming weights.
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Convolution2D` layer with `data_format="channels_last"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.

    # References
        - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
    i   i    c         C@  s   | |  _  | |  _ d  S(   N(   t	   max_valuet   axis(   R   R   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt   __init__)   s    	c         C@  sd   t  j t  j t  j |  d |  j d t  } t  j | d |  j  } | | t  j   | 9} | S(   NR   t   keepdimsi    (	   t   Kt   sqrtt   sumt   squareR   t   Truet   clipR   t   epsilon(   R   R   t   normst   desired(    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   -   s    0c         C@  s   i |  j  d 6|  j d 6S(   NR   R   (   R   R   (   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   3   s    (   R	   R
   t   __doc__R   R   R   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR      s   	t   NonNegc           B@  s   e  Z d  Z d   Z RS(   s/   Constrains the weights to be non-negative.
    c         C@  s&   | t  j | d k t  j    9} | S(   Ng        (   R   t   castt   floatx(   R   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   <   s    "(   R	   R
   R   R   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   8   s   t   UnitNormc           B@  s,   e  Z d  Z d d  Z d   Z d   Z RS(   s―  Constrains the weights incident to each hidden unit to have unit norm.

    # Arguments
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Convolution2D` layer with `data_format="channels_last"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.
    i    c         C@  s   | |  _  d  S(   N(   R   (   R   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   R   s    c      	   C@  s<   | t  j   t  j t  j t  j |  d |  j d t  S(   NR   R   (   R   R   R   R   R   R   R   (   R   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   U   s    '	c         C@  s   i |  j  d 6S(   NR   (   R   (   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   Z   s    (   R	   R
   R   R   R   R   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   A   s   	t
   MinMaxNormc           B@  s5   e  Z d  Z d d d d d  Z d   Z d   Z RS(   s  MinMaxNorm weight constraint.

    Constrains the weights incident to each hidden unit
    to have the norm between a lower bound and an upper bound.

    # Arguments
        min_value: the minimum norm for the incoming weights.
        max_value: the maximum norm for the incoming weights.
        rate: rate for enforcing the constraint: weights will be
            rescaled to yield
            `(1 - rate) * norm + rate * norm.clip(min_value, max_value)`.
            Effectively, this means that rate=1.0 stands for strict
            enforcement of the constraint, while rate<1.0 means that
            weights will be rescaled at each step to slowly move
            towards a value inside the desired interval.
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Convolution2D` layer with `dim_ordering="tf"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.
    g        g      π?i    c         C@  s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t	   min_valueR   t   rateR   (   R   R   R   R    R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   {   s    			c         C@  s}   t  j t  j t  j |  d |  j d t  } |  j t  j | |  j |  j	  d |  j | } | | t  j
   | 9} | S(   NR   R   i   (   R   R   R   R   R   R   R    R   R   R   R   (   R   R   R   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR      s
    0c         C@  s,   i |  j  d 6|  j d 6|  j d 6|  j d 6S(   NR   R   R    R   (   R   R   R    R   (   R   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR      s    

(   R	   R
   R   R   R   R   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyR   ^   s   	c         C@  s
   t  |   S(   N(   R   (   t
   constraint(    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt	   serialize   s    c         C@  s   t  |  d t   d | d d S(   Nt   module_objectst   custom_objectst   printable_module_nameR!   (   R   t   globals(   t   configR$   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt   deserialize‘   s    		c         C@  s   |  d  k r d  St |  t  r) t |   St |  t j  r_ i t |   d 6i  d 6} t |  St |   ro |  St d |    d  S(   Nt
   class_nameR'   s*   Could not interpret constraint identifier:(	   t   Nonet
   isinstancet   dictR(   t   sixt   string_typest   strt   callablet
   ValueError(   t
   identifierR'   (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt   get¨   s    

(   t
   __future__R    R-   t    R   R   t   utils.generic_utilsR   R   t   objectR   R   R   R   R   t   max_normt   non_negt	   unit_normt   min_max_normt   maxnormt   nonnegt   unitnormR"   R*   R(   R3   (    (    (    s0   /tmp/pip-build-isqEY4/keras/keras/constraints.pyt   <module>   s&   	'	3	