3
I4W^¤  ã               @   st   d dl Z d dlZd dlmZ ddlmZ ddlmZ ddlm	Z	 G dd	„ d	e	ƒZ
G d
d„ de	ƒZG dd„ de	ƒZdS )é    N)Ú	Parameteré   )Ú
functional)Úinité   )ÚModulec                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚIdentitya…  A placeholder identity operator that is argument-insensitive.

    Args:
        args: any argument (unused)
        kwargs: any keyword argument (unused)

    Examples::

        >>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 20])

    c                s   t t| ƒjƒ  d S )N)Úsuperr   Ú__init__)ÚselfÚargsÚkwargs)Ú	__class__© ú8/tmp/pip-build-e66j97b2/torch/torch/nn/modules/linear.pyr
      s    zIdentity.__init__c             C   s   |S )Nr   )r   Úinputr   r   r   Úforward   s    zIdentity.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Ú__classcell__r   r   )r   r   r   
   s   r   c                   sD   e Zd ZdZdddgZd‡ fdd„	Zdd	„ Zd
d„ Zdd„ Z‡  Z	S )ÚLinearab  Applies a linear transformation to the incoming data: :math:`y = xA^T + b`

    Args:
        in_features: size of each input sample
        out_features: size of each output sample
        bias: If set to ``False``, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input: :math:`(N, *, H_{in})` where :math:`*` means any number of
          additional dimensions and :math:`H_{in} = \text{in\_features}`
        - Output: :math:`(N, *, H_{out})` where all but the last dimension
          are the same shape as the input and :math:`H_{out} = \text{out\_features}`.

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in\_features})`. The values are
            initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
                :math:`k = \frac{1}{\text{in\_features}}`

    Examples::

        >>> m = nn.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
    ÚbiasÚin_featuresÚout_featuresTc                sZ   t t| ƒjƒ  || _|| _ttj||ƒƒ| _|rBttj|ƒƒ| _	n| j
dd ƒ | jƒ  d S )Nr   )r	   r   r
   r   r   r   ÚtorchÚTensorÚweightr   Úregister_parameterÚreset_parameters)r   r   r   r   )r   r   r   r
   D   s    zLinear.__init__c             C   sT   t j| jtjdƒd� | jd k	rPt j| jƒ\}}dtj|ƒ }t j| j| |ƒ d S )Né   )Úar   )r   Zkaiming_uniform_r   ÚmathÚsqrtr   Z_calculate_fan_in_and_fan_outÚuniform_)r   Zfan_inÚ_Úboundr   r   r   r    O   s
    
zLinear.reset_parametersc             C   s   t j|| j| jƒS )N)ÚFZlinearr   r   )r   r   r   r   r   r   V   s    zLinear.forwardc             C   s   dj | j| j| jd k	ƒS )Nz(in_features={}, out_features={}, bias={})Úformatr   r   r   )r   r   r   r   Ú
extra_reprY   s    zLinear.extra_repr)T)
r   r   r   r   Ú__constants__r
   r    r   r*   r   r   r   )r   r   r   !   s    
r   c                   sF   e Zd ZdZddddgZd‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z‡  Z	S )ÚBilineara™  Applies a bilinear transformation to the incoming data:
    :math:`y = x_1 A x_2 + b`

    Args:
        in1_features: size of each first input sample
        in2_features: size of each second input sample
        out_features: size of each output sample
        bias: If set to False, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input1: :math:`(N, *, H_{in1})` where :math:`H_{in1}=\text{in1\_features}` and
          :math:`*` means any number of additional dimensions. All but the last dimension
          of the inputs should be the same.
        - Input2: :math:`(N, *, H_{in2})` where :math:`H_{in2}=\text{in2\_features}`.
        - Output: :math:`(N, *, H_{out})` where :math:`H_{out}=\text{out\_features}`
          and all but the last dimension are the same shape as the input.

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in1\_features}, \text{in2\_features})`.
            The values are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in1\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
                :math:`k = \frac{1}{\text{in1\_features}}`

    Examples::

        >>> m = nn.Bilinear(20, 30, 40)
        >>> input1 = torch.randn(128, 20)
        >>> input2 = torch.randn(128, 30)
        >>> output = m(input1, input2)
        >>> print(output.size())
        torch.Size([128, 40])
    Úin1_featuresÚin2_featuresr   r   Tc                sb   t t| ƒjƒ  || _|| _|| _ttj|||ƒƒ| _	|rJttj|ƒƒ| _
n| jdd ƒ | jƒ  d S )Nr   )r	   r,   r
   r-   r.   r   r   r   r   r   r   r   r    )r   r-   r.   r   r   )r   r   r   r
   ‡   s    zBilinear.__init__c             C   sH   dt j| jjdƒƒ }tj| j| |ƒ | jd k	rDtj| j| |ƒ d S )Nr   )r#   r$   r   Úsizer   r%   r   )r   r'   r   r   r   r    ”   s    
zBilinear.reset_parametersc             C   s   t j||| j| jƒS )N)r(   Zbilinearr   r   )r   Zinput1Zinput2r   r   r   r   š   s    zBilinear.forwardc             C   s   dj | j| j| j| jd k	ƒS )Nz:in1_features={}, in2_features={}, out_features={}, bias={})r)   r-   r.   r   r   )r   r   r   r   r*   �   s    zBilinear.extra_repr)T)
r   r   r   r   r+   r
   r    r   r*   r   r   r   )r   r   r,   _   s   %r,   )r#   r   Ztorch.nn.parameterr   Ú r   r(   r   Úmoduler   r   r   r,   r   r   r   r   Ú<module>   s   >