ó
àÆ÷Xc           @` sÖ   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l	 m
 Z
 m Z m Z m Z e j d ƒ Z d Z d e f d „  ƒ  YZ e d	 „ Z d e d d
 „ Z g  e _ e d e d d „ ƒ Z d S(   s?   
Provide a simple user friendly API to Theano-managed memory.

i    (   t   absolute_importt   print_functiont   divisionN(   t	   Containert   Variablet   generict   utilss   theano.compile.sharedvalues   restructuredtext ent   SharedVariablec           B` s}   e  Z d  Z d	 Z d	 d	 d „ Z e e d „ Z e d „ Z e d „ Z	 d „  Z
 d „  Z d „  Z d „  Z e e e ƒ Z RS(
   s¯  
    Variable that is (defaults to being) shared between functions that
    it appears in.

    Parameters
    ----------
    name : str
        The name for this variable (see `Variable`).
    type : str
        The type for this variable (see `Variable`).
    value
        A value to associate with this variable (a new container will be
        created).
    strict
        True : assignments to .value will not be cast or copied, so they must
        have the correct type.
    allow_downcast
        Only applies if `strict` is False.
        True : allow assigned value to lose precision when cast during
        assignment.
        False : never allow precision loss.
        None : only allow downcasting of a Python float to a scalar floatX.
    container
        The container to use for this variable. Illegal to pass this as well as
        a value.

    Notes
    -----
    For more user-friendly constructor, see `shared`.

    c      
   C` s­   t  t |  ƒ j d | d | d d  d d  ƒ | d  k	 rj | |  _ | d  k	 sX | d  k	 r© t d ƒ ‚ q© n? t |  d | j | d | d | ƒg d	 t d | d | ƒ|  _ d  S(
   Nt   typet   namet   ownert   indexs9   value and strict are ignored if you pass a container heret   storaget   strictt   allow_downcastt   readonly(	   t   superR   t   __init__t   Nonet	   containert	   TypeErrorR   t   filtert   False(   t   selfR	   R   t   valueR   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyR   D   s    	c         C` s'   | r |  j  j St j |  j  j ƒ Sd S(   sy  
        Get the non-symbolic value associated with this SharedVariable.

        Parameters
        ----------
        borrow : bool
            True to permit returning of an object aliased to internal memory.
        return_internal_type : bool
            True to permit the returning of an arbitrary type object used
            internally to store the shared variable.

        Only with borrow=False and return_internal_type=True does this function
        guarantee that you actually get the internal object.
        But in that case, you may get different return types when using
        different compute devices.

        N(   R   R   t   copyt   deepcopy(   R   t   borrowt   return_internal_type(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt	   get_valueW   s    
c         C` s.   | r | |  j  _ n t j | ƒ |  j  _ d S(   sâ  
        Set the non-symbolic value associated with this SharedVariable.

        Parameters
        ----------
        borrow : bool
            True to use the new_value directly, potentially creating problems
            related to aliased memory.

        Changes to this value will be visible to all functions using
        this SharedVariable.

        Notes
        -----
        Set_value will work in-place on the GPU, if
        the following conditions are met:

            * The destination on the GPU must be c_contiguous.
            * The source is on the CPU.
            * The old value must have the same dtype as the new value
              (which is a given for now, since only float32 is
              supported).
            * The old and new value must have the same shape.
            * The old value is being completely replaced by the new
              value (not partially modified, e.g. by replacing some
              subtensor of it).

        It is also worth mentioning that, for efficient transfer to the GPU,
        Theano will make the new data ``c_contiguous``. This can require an
        extra copy of the data on the host.

        The inplace on gpu memory work when borrow is either True or False.

        N(   R   R   R   R   (   R   t	   new_valueR   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt	   set_valuen   s    #c         C` s3   | r d |  j  j d <n d |  j  j |  j  _ d S(   s£  
        Set the values of a shared variable to 0.

        Parameters
        ----------
        borrow : bbol
            True to modify the value of a shared variable directly by using
            its previous value. Potentially this can cause problems
            regarding to the aliased memory.

        Changes done with this function will be visible to all functions using
        this SharedVariable.

        i    .N(   R   R   (   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   zero–   s    c         C` sL   |  j  d |  j d |  j d d  d d  d |  j ƒ } t j |  j ƒ | _ | S(   NR	   R   R   R   R   (   t	   __class__R	   R   R   R   R   t   tag(   R   t   cp(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   cloneª   s    			c         G` sX   |  j  d t ƒ } t | t j ƒ r4 d | j } n d t | ƒ } t d | ƒ ‚ d  S(   NR   s¼   a Numpy array with dtype: '%s'. This data type is not currently recognized by Theano tensors: please cast your data into a supported numeric type if you need Theano tensor functionalities.sc   an object of type: %s. Did you forget to cast it into a Numpy array before calling theano.shared()?sZ   The generic 'SharedVariable' object is not subscriptable. This shared variable contains %s(   R   t   Truet
   isinstancet   npt   ndarrayt   dtypeR   R   (   R   t   argsR   t   msg(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   __getitem__´   s    c         C` s   t  d ƒ ‚ d  S(   Nsc   sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead.(   t	   Exception(   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt
   _value_getÉ   s    c         C` s   t  d ƒ ‚ d  S(   Nsc   sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead.(   R-   (   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt
   _value_setÎ   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R    R$   R,   R.   R/   t   propertyR   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyR      s   (	
			c         C` s-   | r t  j j |  ƒ n t  j j |  ƒ |  S(   N(   t   sharedt   constructorst   removet   append(   t   ctorR6   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   shared_constructor×   s    c      
   K` sÌ   y‰ t  |  t ƒ r! t d ƒ ‚ n  xd t t j ƒ D]S } y6 | |  d | d | d | | } t j | ƒ | SWq1 t k
 rƒ q1 q1 Xq1 WWn& t k
 r± } | j	 d | _	 ‚  n Xt d |  | f ƒ ‚ d S(	   s=  Return a SharedVariable Variable, initialized with a copy or
    reference of `value`.

    This function iterates over constructor functions to find a
    suitable SharedVariable subclass.  The suitable one is the first
    constructor that accept the given value.  See the documentation of
    :func:`shared_constructor` for the definition of a constructor
    function.

    This function is meant as a convenient default.  If you want to use a
    specific shared variable constructor, consider calling it directly.

    ``theano.shared`` is a shortcut to this function.

    .. attribute:: constructors

    A list of shared variable constructors that will be tried in reverse
    order.

    Notes
    -----
    By passing kwargs, you effectively limit the set of potential constructors
    to those that can accept those kwargs.

    Some shared variable have ``borrow`` as extra kwargs.
    `See <http://deeplearning.net/software/theano/tutorial/aliasing.    html#borrowing-when-creating-shared-variables>`_ for details.

    Some shared variable have ``broadcastable`` as extra kwargs. As shared
    variable shapes can change, all dimensions default to not being
    broadcastable, even if ``value`` has a shape of 1 along some dimension.
    This parameter allows you to create for example a `row` or `column` 2d
    tensor.

    sL   Shared variable constructor needs numeric values and not symbolic variables.R	   R   R   s:   you might consider using 'theano.shared(..., borrow=True)'s¨   No suitable SharedVariable constructor could be found. Are you sure all kwargs are supported? We do not support the parameter dtype or type. value="%s". parameters="%s"N(   s:   you might consider using 'theano.shared(..., borrow=True)'(
   R&   R   R   t   reversedR4   R5   R   t   add_tag_tracet   MemoryErrorR*   (   R   R	   R   R   t   kwargsR8   t   vart   e(    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyR4   ß   s     %c         C` s%   t  d t d |  d | d | d | ƒ S(   s&   
    SharedVariable Constructor.

    R   R   R	   R   R   (   R   R   (   R   R	   R   R   (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   generic_constructor%  s    (   R2   t
   __future__R    R   R   R   t   loggingt   numpyR'   t
   theano.gofR   R   R   R   t	   getLoggert   _loggert   __docformat__R   R   R9   R   R4   R5   R@   (    (    (    s:   /tmp/pip-build-X4mzal/theano/theano/compile/sharedvalue.pyt   <module>   s   "ÂC	