3
I4W^CN  ã               @   s¬   d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlZddl	m
Z
 G dd„ de
ƒZG d	d
„ d
e
ƒZG dd„ de
ƒZG dd„ de
ƒZG dd„ de
ƒZG dd„ de
ƒZdS )é    N)ÚOrderedDict)Úcontainer_abcs)Úisliceé   )ÚModulec                   s   e Zd Z‡ fdd„Z‡  ZS )Ú	Containerc                s>   t t| ƒjƒ  tjdƒ x |jƒ D ]\}}| j||ƒ q"W d S )Nzmnn.Container is deprecated. All of it's functionality is now implemented in nn.Module. Subclass that instead.)Úsuperr   Ú__init__ÚwarningsÚwarnÚitemsÚ
add_module)ÚselfÚkwargsÚkeyÚvalue)Ú	__class__© ú;/tmp/pip-build-e66j97b2/torch/torch/nn/modules/container.pyr	      s    
zContainer.__init__)Ú__name__Ú
__module__Ú__qualname__r	   Ú__classcell__r   r   )r   r   r      s   r   c                   s\   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡ fdd„Z
dd„ Z‡  ZS )Ú
Sequentialaö  A sequential container.
    Modules will be added to it in the order they are passed in the constructor.
    Alternatively, an ordered dict of modules can also be passed in.

    To make it easier to understand, here is a small example::

        # Example of using Sequential
        model = nn.Sequential(
                  nn.Conv2d(1,20,5),
                  nn.ReLU(),
                  nn.Conv2d(20,64,5),
                  nn.ReLU()
                )

        # Example of using Sequential with OrderedDict
        model = nn.Sequential(OrderedDict([
                  ('conv1', nn.Conv2d(1,20,5)),
                  ('relu1', nn.ReLU()),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))
    c                sz   t t| ƒjƒ  t|ƒdkrPt|d tƒrPxL|d jƒ D ]\}}| j||ƒ q6W n&x$t|ƒD ]\}}| jt	|ƒ|ƒ qZW d S )Nr   r   )
r   r   r	   ÚlenÚ
isinstancer   r   r   Ú	enumerateÚstr)r   Úargsr   ÚmoduleÚidx)r   r   r   r	   .   s    zSequential.__init__c             C   sR   t | ƒ}tj|ƒ}| |  ko&|k n  s:tdj|ƒƒ‚||; }tt||dƒƒS )z#Get the idx-th item of the iteratorzindex {} is out of rangeN)r   ÚoperatorÚindexÚ
IndexErrorÚformatÚnextr   )r   Úiteratorr    Úsizer   r   r   Ú_get_item_by_idx7   s    
zSequential._get_item_by_idxc             C   s<   t |tƒr&| jtt| jjƒ ƒ| ƒƒS | j| jjƒ |ƒS d S )N)	r   Úslicer   r   ÚlistÚ_modulesr   r(   Úvalues)r   r    r   r   r   Ú__getitem__@   s    
zSequential.__getitem__c             C   s   | j | jjƒ |ƒ}t| ||ƒS )N)r(   r+   ÚkeysÚsetattr)r   r    r   r   r   r   r   Ú__setitem__F   s    zSequential.__setitem__c             C   sR   t |tƒr2xBt| jjƒ ƒ| D ]}t| |ƒ qW n| j| jjƒ |ƒ}t| |ƒ d S )N)r   r)   r*   r+   r.   Údelattrr(   )r   r    r   r   r   r   Ú__delitem__J   s
    
zSequential.__delitem__c             C   s
   t | jƒS )N)r   r+   )r   r   r   r   Ú__len__R   s    zSequential.__len__c                s    t t| ƒjƒ }dd„ |D ƒ}|S )Nc             S   s   g | ]}|j ƒ s|‘qS r   )Úisdigit)Ú.0r   r   r   r   ú
<listcomp>W   s    z&Sequential.__dir__.<locals>.<listcomp>)r   r   Ú__dir__)r   r.   )r   r   r   r7   U   s    zSequential.__dir__c             C   s    x| j jƒ D ]}||ƒ}qW |S )N)r+   r,   )r   Úinputr   r   r   r   ÚforwardZ   s    zSequential.forward)r   r   r   Ú__doc__r	   r(   r-   r0   r2   r3   r7   r9   r   r   r   )r   r   r      s   		r   c                   s~   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )Ú
ModuleLista  Holds submodules in a list.

    :class:`~torch.nn.ModuleList` can be indexed like a regular Python list, but
    modules it contains are properly registered, and will be visible by all
    :class:`~torch.nn.Module` methods.

    Arguments:
        modules (iterable, optional): an iterable of modules to add

    Example::

        class MyModule(nn.Module):
            def __init__(self):
                super(MyModule, self).__init__()
                self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])

            def forward(self, x):
                # ModuleList can act as an iterable, or be indexed using ints
                for i, l in enumerate(self.linears):
                    x = self.linears[i // 2](x) + l(x)
                return x
    Nc                s"   t t| ƒjƒ  |d k	r| |7 } d S )N)r   r;   r	   )r   Úmodules)r   r   r   r	   x   s    zModuleList.__init__c             C   sV   t j|ƒ}t| ƒ |  ko&t| ƒk n  s:tdj|ƒƒ‚|dk rN|t| ƒ7 }t|ƒS )z.Get the absolute index for the list of moduleszindex {} is out of ranger   )r!   r"   r   r#   r$   r   )r   r    r   r   r   Ú_get_abs_string_index}   s    
"z ModuleList._get_abs_string_indexc             C   s6   t |tƒr"| jt| jjƒ ƒ| ƒS | j| j|ƒ S d S )N)r   r)   r   r*   r+   r,   r=   )r   r    r   r   r   r-   †   s    
zModuleList.__getitem__c             C   s   | j |ƒ}t| t|ƒ|ƒS )N)r=   r/   r   )r   r    r   r   r   r   r0   Œ   s    
zModuleList.__setitem__c             C   s|   t |tƒr6x:tt| jƒƒ| D ]}t| t|ƒƒ qW nt| | j|ƒƒ dd„ tt| jƒƒD ƒ}tt	t
|| jjƒ ƒƒƒ| _d S )Nc             S   s   g | ]}t |ƒ‘qS r   )r   )r5   Úir   r   r   r6   —   s    z*ModuleList.__delitem__.<locals>.<listcomp>)r   r)   Úranger   r+   r1   r   r=   r   r*   Úzipr,   )r   r    ÚkZstr_indicesr   r   r   r2   �   s    
zModuleList.__delitem__c             C   s
   t | jƒS )N)r   r+   )r   r   r   r   r3   š   s    zModuleList.__len__c             C   s   t | jjƒ ƒS )N)Úiterr+   r,   )r   r   r   r   Ú__iter__�   s    zModuleList.__iter__c             C   s
   | j |ƒS )N)Úextend)r   r<   r   r   r   Ú__iadd__    s    zModuleList.__iadd__c                s    t t| ƒjƒ }dd„ |D ƒ}|S )Nc             S   s   g | ]}|j ƒ s|‘qS r   )r4   )r5   r   r   r   r   r6   ¥   s    z&ModuleList.__dir__.<locals>.<listcomp>)r   r;   r7   )r   r.   )r   r   r   r7   £   s    zModuleList.__dir__c             C   sJ   x6t t| jƒ|dƒD ] }| jt|d ƒ | jt|ƒ< qW || jt|ƒ< dS )z¯Insert a given module before a given index in the list.

        Arguments:
            index (int): index to insert.
            module (nn.Module): module to insert
        r   Néÿÿÿÿ)r?   r   r+   r   )r   r"   r   r>   r   r   r   Úinsert¨   s     zModuleList.insertc             C   s   | j tt| ƒƒ|ƒ | S )z|Appends a given module to the end of the list.

        Arguments:
            module (nn.Module): module to append
        )r   r   r   )r   r   r   r   r   Úappend³   s    zModuleList.appendc             C   sT   t |tjƒstdt|ƒj ƒ‚t| ƒ}x(t|ƒD ]\}}| jt	|| ƒ|ƒ q0W | S )z™Appends modules from a Python iterable to the end of the list.

        Arguments:
            modules (iterable): iterable of modules to append
        z=ModuleList.extend should be called with an iterable, but got )
r   r   ÚIterableÚ	TypeErrorÚtyper   r   r   r   r   )r   r<   Úoffsetr>   r   r   r   r   rD   ¼   s    zModuleList.extend)N)r   r   r   r:   r	   r=   r-   r0   r2   r3   rC   rE   r7   rG   rH   rD   r   r   r   )r   r   r;   `   s   	
	r;   c                   sŠ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) Ú
ModuleDictaí  Holds submodules in a dictionary.

    :class:`~torch.nn.ModuleDict` can be indexed like a regular Python dictionary,
    but modules it contains are properly registered, and will be visible by all
    :class:`~torch.nn.Module` methods.

    :class:`~torch.nn.ModuleDict` is an **ordered** dictionary that respects

    * the order of insertion, and

    * in :meth:`~torch.nn.ModuleDict.update`, the order of the merged ``OrderedDict``
      or another :class:`~torch.nn.ModuleDict` (the argument to :meth:`~torch.nn.ModuleDict.update`).

    Note that :meth:`~torch.nn.ModuleDict.update` with other unordered mapping
    types (e.g., Python's plain ``dict``) does not preserve the order of the
    merged mapping.

    Arguments:
        modules (iterable, optional): a mapping (dictionary) of (string: module)
            or an iterable of key-value pairs of type (string, module)

    Example::

        class MyModule(nn.Module):
            def __init__(self):
                super(MyModule, self).__init__()
                self.choices = nn.ModuleDict({
                        'conv': nn.Conv2d(10, 10, 3),
                        'pool': nn.MaxPool2d(3)
                })
                self.activations = nn.ModuleDict([
                        ['lrelu', nn.LeakyReLU()],
                        ['prelu', nn.PReLU()]
                ])

            def forward(self, x, choice, act):
                x = self.choices[choice](x)
                x = self.activations[act](x)
                return x
    Nc                s$   t t| ƒjƒ  |d k	r | j|ƒ d S )N)r   rM   r	   Úupdate)r   r<   )r   r   r   r	   õ   s    zModuleDict.__init__c             C   s
   | j | S )N)r+   )r   r   r   r   r   r-   ú   s    zModuleDict.__getitem__c             C   s   | j ||ƒ d S )N)r   )r   r   r   r   r   r   r0   ý   s    zModuleDict.__setitem__c             C   s   | j |= d S )N)r+   )r   r   r   r   r   r2      s    zModuleDict.__delitem__c             C   s
   t | jƒS )N)r   r+   )r   r   r   r   r3     s    zModuleDict.__len__c             C   s
   t | jƒS )N)rB   r+   )r   r   r   r   rC     s    zModuleDict.__iter__c             C   s
   || j kS )N)r+   )r   r   r   r   r   Ú__contains__	  s    zModuleDict.__contains__c             C   s   | j jƒ  dS )z.Remove all items from the ModuleDict.
        N)r+   Úclear)r   r   r   r   rP     s    zModuleDict.clearc             C   s   | | }| |= |S )z‹Remove key from the ModuleDict and return its module.

        Arguments:
            key (string): key to pop from the ModuleDict
        r   )r   r   Úvr   r   r   Úpop  s    zModuleDict.popc             C   s
   | j jƒ S )z3Return an iterable of the ModuleDict keys.
        )r+   r.   )r   r   r   r   r.     s    zModuleDict.keysc             C   s
   | j jƒ S )z>Return an iterable of the ModuleDict key/value pairs.
        )r+   r   )r   r   r   r   r      s    zModuleDict.itemsc             C   s
   | j jƒ S )z5Return an iterable of the ModuleDict values.
        )r+   r,   )r   r   r   r   r,   %  s    zModuleDict.valuesc             C   s   t |tjƒstdt|ƒj ƒ‚t |tjƒr|t |ttfƒrXx@|j	ƒ D ]\}}|| |< qBW n"x¢t
|j	ƒ ƒD ]\}}|| |< qfW n€x~t|ƒD ]r\}}t |tjƒs¸tdt|ƒ d t|ƒj ƒ‚t|ƒdksètdt|ƒ d tt|ƒƒ d ƒ‚|d | |d < q†W d	S )
a&  Update the :class:`~torch.nn.ModuleDict` with the key-value pairs from a
        mapping or an iterable, overwriting existing keys.

        .. note::
            If :attr:`modules` is an ``OrderedDict``, a :class:`~torch.nn.ModuleDict`, or
            an iterable of key-value pairs, the order of new elements in it is preserved.

        Arguments:
            modules (iterable): a mapping (dictionary) from string to :class:`~torch.nn.Module`,
                or an iterable of key-value pairs of type (string, :class:`~torch.nn.Module`)
        zPModuleDict.update should be called with an iterable of key/value pairs, but got z$ModuleDict update sequence element #z should be Iterable; isé   z has length z; 2 is requiredr   r   N)r   r   rI   rJ   rK   r   ÚMappingr   rM   r   Úsortedr   r   r   Ú
ValueError)r   r<   r   r   ÚjÚmr   r   r   rN   *  s&    zModuleDict.updatec             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)r   r   r   r   r9   N  s    zModuleDict.forward)N)r   r   r   r:   r	   r-   r0   r2   r3   rC   rO   rP   rR   r.   r   r,   rN   r9   r   r   r   )r   r   rM   Ë   s   (
$rM   c                   sv   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚParameterLista?  Holds parameters in a list.

    :class:`~torch.nn.ParameterList` can be indexed like a regular Python
    list, but parameters it contains are properly registered, and will be
    visible by all :class:`~torch.nn.Module` methods.

    Arguments:
        parameters (iterable, optional): an iterable of :class:`~torch.nn.Parameter` to add

    Example::

        class MyModule(nn.Module):
            def __init__(self):
                super(MyModule, self).__init__()
                self.params = nn.ParameterList([nn.Parameter(torch.randn(10, 10)) for i in range(10)])

            def forward(self, x):
                # ParameterList can act as an iterable, or be indexed using ints
                for i, p in enumerate(self.params):
                    x = self.params[i // 2].mm(x) + p.mm(x)
                return x
    Nc                s"   t t| ƒjƒ  |d k	r| |7 } d S )N)r   rZ   r	   )r   Ú
parameters)r   r   r   r	   j  s    zParameterList.__init__c             C   sV   t j|ƒ}t| ƒ |  ko&t| ƒk n  s:tdj|ƒƒ‚|dk rN|t| ƒ7 }t|ƒS )z.Get the absolute index for the list of moduleszindex {} is out of ranger   )r!   r"   r   r#   r$   r   )r   r    r   r   r   r=   o  s    
"z#ParameterList._get_abs_string_indexc             C   s>   t |tƒr"| jt| jjƒ ƒ| ƒS | j|ƒ}| jt|ƒ S d S )N)r   r)   r   r*   Ú_parametersr,   r=   r   )r   r    r   r   r   r-   x  s    

zParameterList.__getitem__c             C   s   | j |ƒ}| jt|ƒ|ƒS )N)r=   Úregister_parameterr   )r   r    Úparamr   r   r   r0     s    
zParameterList.__setitem__c             C   s
   t | jƒS )N)r   r\   )r   r   r   r   r3   ƒ  s    zParameterList.__len__c             C   s   t | jjƒ ƒS )N)rB   r\   r,   )r   r   r   r   rC   †  s    zParameterList.__iter__c             C   s
   | j |ƒS )N)rD   )r   r[   r   r   r   rE   ‰  s    zParameterList.__iadd__c                s    t t| ƒjƒ }dd„ |D ƒ}|S )Nc             S   s   g | ]}|j ƒ s|‘qS r   )r4   )r5   r   r   r   r   r6   Ž  s    z)ParameterList.__dir__.<locals>.<listcomp>)r   rZ   r7   )r   r.   )r   r   r   r7   Œ  s    zParameterList.__dir__c             C   s   | j tt| ƒƒ|ƒ | S )zˆAppends a given parameter at the end of the list.

        Arguments:
            parameter (nn.Parameter): parameter to append
        )r]   r   r   )r   Ú	parameterr   r   r   rH   ‘  s    zParameterList.appendc             C   sT   t |tjƒstdt|ƒj ƒ‚t| ƒ}x(t|ƒD ]\}}| jt	|| ƒ|ƒ q0W | S )z¢Appends parameters from a Python iterable to the end of the list.

        Arguments:
            parameters (iterable): iterable of parameters to append
        z@ParameterList.extend should be called with an iterable, but got )
r   r   rI   rJ   rK   r   r   r   r]   r   )r   r[   rL   r>   r^   r   r   r   rD   š  s    zParameterList.extendc             C   sŠ   g }xv| j jƒ D ]h\}}djdd„ |jƒ D ƒƒ}|js:dndj|jƒ ƒ}djtj|j	ƒ||ƒ}|j
dt|ƒ d | ƒ qW d	j|ƒ}|S )
NÚxc             s   s   | ]}t |ƒV  qd S )N)r   )r5   r'   r   r   r   ú	<genexpr>«  s    z+ParameterList.extra_repr.<locals>.<genexpr>Ú z	 (GPU {})z'Parameter containing: [{} of size {}{}]z  (z): Ú
)r\   r   Újoinr'   Úis_cudar$   Ú
get_deviceÚtorchÚtypenameÚdatarH   r   )r   Úchild_linesrA   ÚpÚsize_strÚ
device_strÚparastrÚtmpstrr   r   r   Ú
extra_repr¨  s    
zParameterList.extra_repr)N)r   r   r   r:   r	   r=   r-   r0   r3   rC   rE   r7   rH   rD   rp   r   r   r   )r   r   rZ   R  s   		rZ   c                   sŠ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) ÚParameterDictac  Holds parameters in a dictionary.

    ParameterDict can be indexed like a regular Python dictionary, but parameters it
    contains are properly registered, and will be visible by all Module methods.

    :class:`~torch.nn.ParameterDict` is an **ordered** dictionary that respects

    * the order of insertion, and

    * in :meth:`~torch.nn.ParameterDict.update`, the order of the merged ``OrderedDict``
      or another :class:`~torch.nn.ParameterDict` (the argument to
      :meth:`~torch.nn.ParameterDict.update`).

    Note that :meth:`~torch.nn.ParameterDict.update` with other unordered mapping
    types (e.g., Python's plain ``dict``) does not preserve the order of the
    merged mapping.

    Arguments:
        parameters (iterable, optional): a mapping (dictionary) of
            (string : :class:`~torch.nn.Parameter`) or an iterable of key-value pairs
            of type (string, :class:`~torch.nn.Parameter`)

    Example::

        class MyModule(nn.Module):
            def __init__(self):
                super(MyModule, self).__init__()
                self.params = nn.ParameterDict({
                        'left': nn.Parameter(torch.randn(5, 10)),
                        'right': nn.Parameter(torch.randn(5, 10))
                })

            def forward(self, x, choice):
                x = self.params[choice].mm(x)
                return x
    Nc                s$   t t| ƒjƒ  |d k	r | j|ƒ d S )N)r   rq   r	   rN   )r   r[   )r   r   r   r	   Ú  s    zParameterDict.__init__c             C   s
   | j | S )N)r\   )r   r   r   r   r   r-   ß  s    zParameterDict.__getitem__c             C   s   | j ||ƒ d S )N)r]   )r   r   r_   r   r   r   r0   â  s    zParameterDict.__setitem__c             C   s   | j |= d S )N)r\   )r   r   r   r   r   r2   å  s    zParameterDict.__delitem__c             C   s
   t | jƒS )N)r   r\   )r   r   r   r   r3   è  s    zParameterDict.__len__c             C   s   t | jjƒ ƒS )N)rB   r\   r.   )r   r   r   r   rC   ë  s    zParameterDict.__iter__c             C   s
   || j kS )N)r\   )r   r   r   r   r   rO   î  s    zParameterDict.__contains__c             C   s   | j jƒ  dS )z1Remove all items from the ParameterDict.
        N)r\   rP   )r   r   r   r   rP   ñ  s    zParameterDict.clearc             C   s   | | }| |= |S )z”Remove key from the ParameterDict and return its parameter.

        Arguments:
            key (string): key to pop from the ParameterDict
        r   )r   r   rQ   r   r   r   rR   ö  s    zParameterDict.popc             C   s
   | j jƒ S )z6Return an iterable of the ParameterDict keys.
        )r\   r.   )r   r   r   r   r.      s    zParameterDict.keysc             C   s
   | j jƒ S )zAReturn an iterable of the ParameterDict key/value pairs.
        )r\   r   )r   r   r   r   r     s    zParameterDict.itemsc             C   s
   | j jƒ S )z8Return an iterable of the ParameterDict values.
        )r\   r,   )r   r   r   r   r,   
  s    zParameterDict.valuesc             C   s   t |tjƒstdt|ƒj ƒ‚t |tjƒr|t |ttfƒrXx@|j	ƒ D ]\}}|| |< qBW n"x¢t
|j	ƒ ƒD ]\}}|| |< qfW n€x~t|ƒD ]r\}}t |tjƒs¸tdt|ƒ d t|ƒj ƒ‚t|ƒdksètdt|ƒ d tt|ƒƒ d ƒ‚|d | |d < q†W d	S )
aH  Update the :class:`~torch.nn.ParameterDict` with the key-value pairs from a
        mapping or an iterable, overwriting existing keys.

        .. note::
            If :attr:`parameters` is an ``OrderedDict``, a :class:`~torch.nn.ParameterDict`, or
            an iterable of key-value pairs, the order of new elements in it is preserved.

        Arguments:
            parameters (iterable): a mapping (dictionary) from string to
                :class:`~torch.nn.Parameter`, or an iterable of
                key-value pairs of type (string, :class:`~torch.nn.Parameter`)
        zTParametersDict.update should be called with an iterable of key/value pairs, but got z'ParameterDict update sequence element #z should be Iterable; isrS   z has length z; 2 is requiredr   r   N)r   r   rI   rJ   rK   r   rT   r   rq   r   rU   r   r   r   rV   )r   r[   r   r_   rW   rk   r   r   r   rN     s&    zParameterDict.updatec             C   s†   g }xr| j jƒ D ]d\}}djdd„ |jƒ D ƒƒ}|js:dndj|jƒ ƒ}djtj|j	ƒ||ƒ}|j
d| d | ƒ qW d	j|ƒ}|S )
Nr`   c             s   s   | ]}t |ƒV  qd S )N)r   )r5   r'   r   r   r   ra   7  s    z+ParameterDict.extra_repr.<locals>.<genexpr>rb   z	 (GPU {})z'Parameter containing: [{} of size {}{}]z  (z): rc   )r\   r   rd   r'   re   r$   rf   rg   rh   ri   rH   )r   rj   rA   rk   rl   rm   rn   ro   r   r   r   rp   4  s    
zParameterDict.extra_repr)N)r   r   r   r:   r	   r-   r0   r2   r3   rC   rO   rP   rR   r.   r   r,   rN   rp   r   r   r   )r   r   rq   ´  s   $
%rq   )r
   Úcollectionsr   Z
torch._sixr   Ú	itertoolsr   r!   rg   r   r   r   r   r;   rM   rZ   rq   r   r   r   r   Ú<module>   s   Jk b