/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
NameSizeModeActions
activation.cpython-36.pyc476350644editdlrm
adaptive.cpython-36.pyc97250644editdlrm
batchnorm.cpython-36.pyc310390644editdlrm
channelshuffle.cpython-36.pyc19010644editdlrm
container.cpython-36.pyc279700644editdlrm
conv.cpython-36.pyc577890644editdlrm
distance.cpython-36.pyc36840644editdlrm
dropout.cpython-36.pyc103100644editdlrm
flatten.cpython-36.pyc58130644editdlrm
fold.cpython-36.pyc128070644editdlrm
instancenorm.cpython-36.pyc188080644editdlrm
lazy.cpython-36.pyc116900644editdlrm
linear.cpython-36.pyc101880644editdlrm
loss.cpython-36.pyc914650644editdlrm
module.cpython-36.pyc665300644editdlrm
normalization.cpython-36.pyc113600644editdlrm
padding.cpython-36.pyc222170644editdlrm
pixelshuffle.cpython-36.pyc43980644editdlrm
pooling.cpython-36.pyc532030644editdlrm
rnn.cpython-36.pyc446530644editdlrm
sparse.cpython-36.pyc209560644editdlrm
transformer.cpython-36.pyc207370644editdlrm
upsampling.cpython-36.pyc107800644editdlrm
utils.cpython-36.pyc25280644editdlrm
_functions.cpython-36.pyc54410644editdlrm
__init__.cpython-36.pyc52360644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/activation.cpython-36.pyc (47635B)
3 Eg@sDddlZddlmZmZddlZddlmZddlmZddlm Z m Z m Z ddl m Z ddlmZd d lmZGd d d eZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGd!d"d"eZGd#d$d$eZGd%d&d&eZ Gd'd(d(eZ!Gd)d*d*eZ"Gd+d,d,eZ#Gd-d.d.eZ$Gd/d0d0eZ%Gd1d2d2eZ&Gd3d4d4eZ'Gd5d6d6eZ(Gd7d8d8eZ)Gd9d:d:eZ*Gd;d<dd>eZ,Gd?d@d@eZ-GdAdBdBeZ.GdCdDdDeZ/dS)EN)OptionalTuple)Tensor)NonDynamicallyQuantizableLinear) constant_xavier_normal_xavier_uniform_) Parameter)Module) functionalcs^eZdZUdZdddgZeee deeeddfdd Z e e d d d Z d dZ ZS) ThresholdaThresholds each element of the input Tensor. Threshold is defined as: .. math:: y = \begin{cases} x, &\text{ if } x > \text{threshold} \\ \text{value}, &\text{ otherwise } \end{cases} Args: threshold: The value to threshold at value: The value to replace with inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.Threshold(0.1, 20) >>> input = torch.randn(2) >>> output = m(input) thresholdvalueinplaceFN)rrrreturncs$tt|j||_||_||_dS)N)superr__init__rrr)selfrrr) __class__G/usr/local/lib64/python3.6/site-packages/torch/nn/modules/activation.pyr.szThreshold.__init__)inputrcCstj||j|j|jS)N)Frrr)rrrrrforward5szThreshold.forwardcCs |jr dnd}dj|j|j|S)Nz, inplace=Truezthreshold={}, value={}{})rformatrr)r inplace_strrrr extra_repr8szThreshold.extra_repr)F)__name__ __module__ __qualname____doc__ __constants__floatrrboolrrrrr __classcell__rr)rrr s  rcsReZdZUdZdgZed edfdd Zeeddd Z e d d d Z Z S)ReLUaApplies the rectified linear unit function element-wise: :math:`\text{ReLU}(x) = (x)^+ = \max(0, x)` Args: inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/ReLU.png Examples:: >>> m = nn.ReLU() >>> input = torch.randn(2) >>> output = m(input) An implementation of CReLU - https://arxiv.org/abs/1603.05201 >>> m = nn.ReLU() >>> input = torch.randn(2).unsqueeze(0) >>> output = torch.cat((m(input),m(-input))) rF)rcstt|j||_dS)N)rr(rr)rr)rrrr]sz ReLU.__init__)rrcCstj||jdS)N)r)rZrelur)rrrrrrasz ReLU.forward)rcCs|jr dnd}|S)Nz inplace=Truer)r)rrrrrrdszReLU.extra_repr)F) r r!r"r#r$r&rrrrstrrr'rr)rrr(?s r(cs\eZdZUdZdddgZeee deeed fd d Z e e d d dZ ddZ ZS)RReLUaApplies the randomized leaky rectified liner unit function, element-wise, as described in the paper: `Empirical Evaluation of Rectified Activations in Convolutional Network`_. The function is defined as: .. math:: \text{RReLU}(x) = \begin{cases} x & \text{if } x \geq 0 \\ ax & \text{ otherwise } \end{cases} where :math:`a` is randomly sampled from uniform distribution :math:`\mathcal{U}(\text{lower}, \text{upper})`. See: https://arxiv.org/pdf/1505.00853.pdf Args: lower: lower bound of the uniform distribution. Default: :math:`\frac{1}{8}` upper: upper bound of the uniform distribution. Default: :math:`\frac{1}{3}` inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.RReLU(0.1, 0.3) >>> input = torch.randn(2) >>> output = m(input) .. _`Empirical Evaluation of Rectified Activations in Convolutional Network`: https://arxiv.org/abs/1505.00853 lowerupperrg?F)r+r,rcs$tt|j||_||_||_dS)N)rr*rr+r,r)rr+r,r)rrrrszRReLU.__init__)rrcCstj||j|j|j|jS)N)rZrrelur+r,trainingr)rrrrrrsz RReLU.forwardcCs |jr dnd}dj|j|j|S)Nz, inplace=Truerzlower={}, upper={}{})rrr+r,)rrrrrrszRReLU.extra_repr?UUUUUU?)r0r1F)r r!r"r#r$r%r+r,r&rrrrrr'rr)rrr*is % r*cspeZdZUdZdddgZeee deeee ee eddfd d Z e e d d d Z edddZZS)Hardtanha/Applies the HardTanh function element-wise HardTanh is defined as: .. math:: \text{HardTanh}(x) = \begin{cases} 1 & \text{ if } x > 1 \\ -1 & \text{ if } x < -1 \\ x & \text{ otherwise } \\ \end{cases} The range of the linear region :math:`[-1, 1]` can be adjusted using :attr:`min_val` and :attr:`max_val`. Args: min_val: minimum value of the linear region range. Default: -1 max_val: maximum value of the linear region range. Default: 1 inplace: can optionally do the operation in-place. Default: ``False`` Keyword arguments :attr:`min_value` and :attr:`max_value` have been deprecated in favor of :attr:`min_val` and :attr:`max_val`. Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Hardtanh.png Examples:: >>> m = nn.Hardtanh(-2, 2) >>> input = torch.randn(2) >>> output = m(input) min_valmax_valr?FN)r3r4r min_value max_valuercs`tt|j|dk r$tjd|}|dk r:tjd|}||_||_||_|j|jks\tdS)Nz>keyword argument min_value is deprecated and rename to min_valz>keyword argument max_value is deprecated and rename to max_val) rr2rwarningswarnr3r4rAssertionError)rr3r4rr6r7)rrrrs  zHardtanh.__init__)rrcCstj||j|j|jS)N)rZhardtanhr3r4r)rrrrrrszHardtanh.forward)rcCs |jr dnd}dj|j|j|S)Nz, inplace=Truerzmin_val={}, max_val={}{})rrr3r4)rrrrrrszHardtanh.extra_repr)r;r5FNN)r r!r"r#r$r%r3r4r&rrrrrr)rr'rr)rrr2s " r2cs6eZdZdZd edfdd ZedddZZS) ReLU6aApplies the element-wise function: .. math:: \text{ReLU6}(x) = \min(\max(0,x), 6) Args: inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/ReLU6.png Examples:: >>> m = nn.ReLU6() >>> input = torch.randn(2) >>> output = m(input) F)rcstt|jdd|dS)Ngg@)rr<r)rr)rrrrszReLU6.__init__)rcCs|jr dnd}|S)Nz inplace=Truer)r)rrrrrr szReLU6.extra_repr)F) r r!r"r#r&rr)rr'rr)rrr<sr<c@s eZdZdZeedddZdS)SigmoidaApplies the element-wise function: .. math:: \text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)} Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Sigmoid.png Examples:: >>> m = nn.Sigmoid() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)torchZsigmoid)rrrrrr"szSigmoid.forwardN)r r!r"r#rrrrrrr=sr=csFeZdZUdZdgZed eddfdd Zeedd d Z Z S) HardsigmoidaSApplies the element-wise function: .. math:: \text{Hardsigmoid}(x) = \begin{cases} 0 & \text{if~} x \le -3, \\ 1 & \text{if~} x \ge +3, \\ x / 6 + 1 / 2 & \text{otherwise} \end{cases} Args: inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.Hardsigmoid() >>> input = torch.randn(2) >>> output = m(input) rFN)rrcstt|j||_dS)N)rr?rr)rr)rrrrAszHardsigmoid.__init__)rrcCstj||jS)N)rZ hardsigmoidr)rrrrrrEszHardsigmoid.forward)F) r r!r"r#r$r&rrrrr'rr)rrr?&s r?c@s eZdZdZeedddZdS)TanhaApplies the element-wise function: .. math:: \text{Tanh}(x) = \tanh(x) = \frac{\exp(x) - \exp(-x)} {\exp(x) + \exp(-x)} Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Tanh.png Examples:: >>> m = nn.Tanh() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)r>tanh)rrrrrr\sz Tanh.forwardN)r r!r"r#rrrrrrr@Isr@csReZdZUdZdgZed edfdd Zeeddd Z e d d d Z Z S)SiLUaApplies the Sigmoid Linear Unit (SiLU) function, element-wise. The SiLU function is also known as the swish function. .. math:: \text{silu}(x) = x * \sigma(x), \text{where } \sigma(x) \text{ is the logistic sigmoid.} .. note:: See `Gaussian Error Linear Units (GELUs) `_ where the SiLU (Sigmoid Linear Unit) was originally coined, and see `Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning `_ and `Swish: a Self-Gated Activation Function `_ where the SiLU was experimented with later. Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.SiLU() >>> input = torch.randn(2) >>> output = m(input) rF)rcstt|j||_dS)N)rrBrr)rr)rrrr{sz SiLU.__init__)rrcCstj||jdS)N)r)rZsilur)rrrrrrsz SiLU.forward)rcCs|jr dnd}|S)Nz inplace=Truer)r)rrrrrrszSiLU.extra_repr)F) r r!r"r#r$r&rrrrr)rr'rr)rrrB_s rBcsReZdZUdZdgZed edfdd Zeeddd Z e d d d Z Z S)MishaAApplies the Mish function, element-wise. Mish: A Self Regularized Non-Monotonic Neural Activation Function. .. math:: \text{Mish}(x) = x * \text{Tanh}(\text{Softplus}(x)) .. note:: See `Mish: A Self Regularized Non-Monotonic Neural Activation Function `_ Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.Mish() >>> input = torch.randn(2) >>> output = m(input) rF)rcstt|j||_dS)N)rrCrr)rr)rrrrsz Mish.__init__)rrcCstj||jdS)N)r)rZmishr)rrrrrrsz Mish.forward)rcCs|jr dnd}|S)Nz inplace=Truer)r)rrrrrrszMish.extra_repr)F) r r!r"r#r$r&rrrrr)rr'rr)rrrCs rCcsFeZdZUdZdgZed eddfdd Zeedd d Z Z S) HardswishaApplies the hardswish function, element-wise, as described in the paper: `Searching for MobileNetV3`_. .. math:: \text{Hardswish}(x) = \begin{cases} 0 & \text{if~} x \le -3, \\ x & \text{if~} x \ge +3, \\ x \cdot (x + 3) /6 & \text{otherwise} \end{cases} Args: inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. Examples:: >>> m = nn.Hardswish() >>> input = torch.randn(2) >>> output = m(input) .. _`Searching for MobileNetV3`: https://arxiv.org/abs/1905.02244 rFN)rrcstt|j||_dS)N)rrDrr)rr)rrrrszHardswish.__init__)rrcCstj||jS)N)rZ hardswishr)rrrrrrszHardswish.forward)F) r r!r"r#r$r&rrrrr'rr)rrrDs rDcs\eZdZUdZddgZeedeeddfdd Z e e d d d Z e d ddZ ZS)ELUaApplies the element-wise function: .. math:: \text{ELU}(x) = \begin{cases} x, & \text{ if } x > 0\\ \alpha * (\exp(x) - 1), & \text{ if } x \leq 0 \end{cases} Args: alpha: the :math:`\alpha` value for the ELU formulation. Default: 1.0 inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/ELU.png Examples:: >>> m = nn.ELU() >>> input = torch.randn(2) >>> output = m(input) alphar?FN)rFrrcstt|j||_||_dS)N)rrErrFr)rrFr)rrrrsz ELU.__init__)rrcCstj||j|jS)N)rZelurFr)rrrrrrsz ELU.forward)rcCs|jr dnd}dj|j|S)Nz, inplace=Truerz alpha={}{})rrrF)rrrrrrszELU.extra_repr)rGF)r r!r"r#r$r%rFr&rrrrr)rr'rr)rrrEs rEcs\eZdZUdZddgZeedeeddfdd Z e e d d d Z e d ddZ ZS)CELUa.Applies the element-wise function: .. math:: \text{CELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x/\alpha) - 1)) More details can be found in the paper `Continuously Differentiable Exponential Linear Units`_ . Args: alpha: the :math:`\alpha` value for the CELU formulation. Default: 1.0 inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/CELU.png Examples:: >>> m = nn.CELU() >>> input = torch.randn(2) >>> output = m(input) .. _`Continuously Differentiable Exponential Linear Units`: https://arxiv.org/abs/1704.07483 rFr?FN)rFrrcstt|j||_||_dS)N)rrHrrFr)rrFr)rrrrsz CELU.__init__)rrcCstj||j|jS)N)rZcelurFr)rrrrrrsz CELU.forward)rcCs|jr dnd}dj|j|S)Nz, inplace=Truerz alpha={}{})rrrF)rrrrrr!szCELU.extra_repr)rIF)r r!r"r#r$r%rFr&rrrrr)rr'rr)rrrHs rHcsTeZdZUdZdgZededdfdd Zeedd d Z e d d d Z Z S)SELUayApplied element-wise, as: .. math:: \text{SELU}(x) = \text{scale} * (\max(0,x) + \min(0, \alpha * (\exp(x) - 1))) with :math:`\alpha = 1.6732632423543772848170429916717` and :math:`\text{scale} = 1.0507009873554804934193349852946`. .. warning:: When using ``kaiming_normal`` or ``kaiming_normal_`` for initialisation, ``nonlinearity='linear'`` should be used instead of ``nonlinearity='selu'`` in order to get `Self-Normalizing Neural Networks`_. See :func:`torch.nn.init.calculate_gain` for more information. More details can be found in the paper `Self-Normalizing Neural Networks`_ . Args: inplace (bool, optional): can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/SELU.png Examples:: >>> m = nn.SELU() >>> input = torch.randn(2) >>> output = m(input) .. _Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515 rFN)rrcstt|j||_dS)N)rrJrr)rr)rrrrKsz SELU.__init__)rrcCstj||jS)N)rZselur)rrrrrrOsz SELU.forward)rcCs|jr dnd}|S)Nz inplace=Truer)r)rrrrrrRszSELU.extra_repr)F) r r!r"r#r$r&rrrrr)rr'rr)rrrJ&s !rJcsTeZdZUdZdgZededdfdd Zeedd d Z e d d d Z Z S)GLUa3Applies the gated linear unit function :math:`{GLU}(a, b)= a \otimes \sigma(b)` where :math:`a` is the first half of the input matrices and :math:`b` is the second half. Args: dim (int): the dimension on which to split the input. Default: -1 Shape: - Input: :math:`(\ast_1, N, \ast_2)` where `*` means, any number of additional dimensions - Output: :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2` Examples:: >>> m = nn.GLU() >>> input = torch.randn(4, 2) >>> output = m(input) dimrN)rLrcstt|j||_dS)N)rrKrrL)rrL)rrrrmsz GLU.__init__)rrcCstj||jS)N)rZglurL)rrrrrrqsz GLU.forward)rcCs dj|jS)Nzdim={})rrL)rrrrrtszGLU.extra_repr)rM) r r!r"r#r$intrLrrrr)rr'rr)rrrKWs rKc@s eZdZdZeedddZdS)GELUaApplies the Gaussian Error Linear Units function: .. math:: \text{GELU}(x) = x * \Phi(x) where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution. Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/GELU.png Examples:: >>> m = nn.GELU() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)rZgelu)rrrrrrsz GELU.forwardN)r r!r"r#rrrrrrrOxsrOcsTeZdZUdZdgZededdfdd Zeedd d Z e d d d Z Z S) HardshrinkaApplies the hard shrinkage function element-wise: .. math:: \text{HardShrink}(x) = \begin{cases} x, & \text{ if } x > \lambda \\ x, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases} Args: lambd: the :math:`\lambda` value for the Hardshrink formulation. Default: 0.5 Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Hardshrink.png Examples:: >>> m = nn.Hardshrink() >>> input = torch.randn(2) >>> output = m(input) lambd?N)rQrcstt|j||_dS)N)rrPrrQ)rrQ)rrrrszHardshrink.__init__)rrcCstj||jS)N)rZ hardshrinkrQ)rrrrrrszHardshrink.forward)rcCs dj|jS)Nz{})rrQ)rrrrrszHardshrink.extra_repr)rR) r r!r"r#r$r%rQrrrr)rr'rr)rrrPs rPcs\eZdZUdZddgZeedeeddfdd Z e e d d d Z e d ddZ ZS) LeakyReLUa?Applies the element-wise function: .. math:: \text{LeakyReLU}(x) = \max(0, x) + \text{negative\_slope} * \min(0, x) or .. math:: \text{LeakyRELU}(x) = \begin{cases} x, & \text{ if } x \geq 0 \\ \text{negative\_slope} \times x, & \text{ otherwise } \end{cases} Args: negative_slope: Controls the angle of the negative slope. Default: 1e-2 inplace: can optionally do the operation in-place. Default: ``False`` Shape: - Input: :math:`(*)` where `*` means, any number of additional dimensions - Output: :math:`(*)`, same shape as the input .. image:: ../scripts/activation_images/LeakyReLU.png Examples:: >>> m = nn.LeakyReLU(0.1) >>> input = torch.randn(2) >>> output = m(input) rnegative_slope{Gz?FN)rTrrcstt|j||_||_dS)N)rrSrrTr)rrTr)rrrrszLeakyReLU.__init__)rrcCstj||j|jS)N)rZ leaky_relurTr)rrrrrrszLeakyReLU.forward)rcCs|jr dnd}dj|j|S)Nz, inplace=Truerznegative_slope={}{})rrrT)rrrrrrszLeakyReLU.extra_repr)rUF)r r!r"r#r$r&rr%rTrrrr)rr'rr)rrrSs rSc@s eZdZdZeedddZdS) LogSigmoidaApplies the element-wise function: .. math:: \text{LogSigmoid}(x) = \log\left(\frac{ 1 }{ 1 + \exp(-x)}\right) Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/LogSigmoid.png Examples:: >>> m = nn.LogSigmoid() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)rZ logsigmoid)rrrrrrszLogSigmoid.forwardN)r r!r"r#rrrrrrrVsrVcs\eZdZUdZddgZeedeeddfdd Ze e d d d Z e d ddZ Z S)SoftplusapApplies the element-wise function: .. math:: \text{Softplus}(x) = \frac{1}{\beta} * \log(1 + \exp(\beta * x)) SoftPlus is a smooth approximation to the ReLU function and can be used to constrain the output of a machine to always be positive. For numerical stability the implementation reverts to the linear function when :math:`input \times \beta > threshold`. Args: beta: the :math:`\beta` value for the Softplus formulation. Default: 1 threshold: values above this revert to a linear function. Default: 20 Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Softplus.png Examples:: >>> m = nn.Softplus() >>> input = torch.randn(2) >>> output = m(input) betarrN)rXrrcstt|j||_||_dS)N)rrWrrXr)rrXr)rrrr szSoftplus.__init__)rrcCstj||j|jS)N)rZsoftplusrXr)rrrrrr%szSoftplus.forward)rcCsdj|j|jS)Nzbeta={}, threshold={})rrXr)rrrrr(szSoftplus.extra_repr)rrY)r r!r"r#r$rNrXrrrrr)rr'rr)rrrWs rWcsTeZdZUdZdgZededdfdd Zeedd d Z e d d d Z Z S) SoftshrinkaApplies the soft shrinkage function elementwise: .. math:: \text{SoftShrinkage}(x) = \begin{cases} x - \lambda, & \text{ if } x > \lambda \\ x + \lambda, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases} Args: lambd: the :math:`\lambda` (must be no less than zero) value for the Softshrink formulation. Default: 0.5 Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Softshrink.png Examples:: >>> m = nn.Softshrink() >>> input = torch.randn(2) >>> output = m(input) rQ?N)rQrcstt|j||_dS)N)rrZrrQ)rrQ)rrrrIszSoftshrink.__init__)rrcCstj||jS)N)rZ softshrinkrQ)rrrrrrMszSoftshrink.forward)rcCs t|jS)N)r)rQ)rrrrrPszSoftshrink.extra_repr)r[) r r!r"r#r$r%rQrrrr)rr'rr)rrrZ,s rZc seZdZUdZdgZeejeej dddfdd Z d d Z fd d Z deeeeee eeeeeefdddZZS)MultiheadAttentionaAllows the model to jointly attend to information from different representation subspaces. See `Attention Is All You Need `_. .. math:: \text{MultiHead}(Q, K, V) = \text{Concat}(head_1,\dots,head_h)W^O where :math:`head_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)`. Args: embed_dim: Total dimension of the model. num_heads: Number of parallel attention heads. Note that ``embed_dim`` will be split across ``num_heads`` (i.e. each head will have dimension ``embed_dim // num_heads``). dropout: Dropout probability on ``attn_output_weights``. Default: ``0.0`` (no dropout). bias: If specified, adds bias to input / output projection layers. Default: ``True``. add_bias_kv: If specified, adds bias to the key and value sequences at dim=0. Default: ``False``. add_zero_attn: If specified, adds a new batch of zeros to the key and value sequences at dim=1. Default: ``False``. kdim: Total number of features for keys. Default: ``None`` (uses ``kdim=embed_dim``). vdim: Total number of features for values. Default: ``None`` (uses ``vdim=embed_dim``). batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False`` (seq, batch, feature). Examples:: >>> multihead_attn = nn.MultiheadAttention(embed_dim, num_heads) >>> attn_output, attn_output_weights = multihead_attn(query, key, value) batch_firstTFN)rc s| | d} tt|j||_|dk r*|n||_|dk r<|n||_|j|koT|j|k|_||_||_| |_ |||_ |j ||jkst d|jdkrt t j||ff| |_t t j||jff| |_t t j||jff| |_|jddn@t t jd||ff| |_|jdd|jdd|jdd|rPt t jd|f| |_n |jd dt||fd |i| |_|rt t jd d |ff| |_t t jd d |ff| |_n d|_|_||_|jdS) N)devicedtypez(embed_dim must be divisible by num_headsFin_proj_weightr. q_proj_weight k_proj_weight v_proj_weight in_proj_biasbiasr)rr\r embed_dimkdimvdim_qkv_same_embed_dim num_headsdropoutr]Zhead_dimr:r r>emptyrbrcrdZregister_parameterrarerout_projbias_kbias_v add_zero_attn_reset_parameters) rrgrkrlrfZ add_bias_kvrqrhrir]r_r`factory_kwargs)rrrrus<        zMultiheadAttention.__init__cCs|jrt|jnt|jt|jt|j|jdk rTt|jdt|jj d|j dk rht |j |j dk r|t |j dS)Ng) rjr rarbrcrdrerrnrfrorrp)rrrrrrs         z$MultiheadAttention._reset_parameterscs$d|krd|d<tt|j|dS)NrjT)rr\ __setstate__)rstate)rrrrtszMultiheadAttention.__setstate__)querykeyrkey_padding_mask need_weights attn_maskrc Cs|jr dd|||fD\}}}|jstj||||j|j|j|j|j|j |j |j |j j |j j|j|||d|j|j|jd\}}nJtj||||j|j|j|j|j|j |j |j |j j |j j|j|||d\}}|jr|jdd|fS||fSdS) a\ Args: query: Query embeddings of shape :math:`(L, N, E_q)` when ``batch_first=False`` or :math:`(N, L, E_q)` when ``batch_first=True``, where :math:`L` is the target sequence length, :math:`N` is the batch size, and :math:`E_q` is the query embedding dimension ``embed_dim``. Queries are compared against key-value pairs to produce the output. See "Attention Is All You Need" for more details. key: Key embeddings of shape :math:`(S, N, E_k)` when ``batch_first=False`` or :math:`(N, S, E_k)` when ``batch_first=True``, where :math:`S` is the source sequence length, :math:`N` is the batch size, and :math:`E_k` is the key embedding dimension ``kdim``. See "Attention Is All You Need" for more details. value: Value embeddings of shape :math:`(S, N, E_v)` when ``batch_first=False`` or :math:`(N, S, E_v)` when ``batch_first=True``, where :math:`S` is the source sequence length, :math:`N` is the batch size, and :math:`E_v` is the value embedding dimension ``vdim``. See "Attention Is All You Need" for more details. key_padding_mask: If specified, a mask of shape :math:`(N, S)` indicating which elements within ``key`` to ignore for the purpose of attention (i.e. treat as "padding"). Binary and byte masks are supported. For a binary mask, a ``True`` value indicates that the corresponding ``key`` value will be ignored for the purpose of attention. For a byte mask, a non-zero value indicates that the corresponding ``key`` value will be ignored. need_weights: If specified, returns ``attn_output_weights`` in addition to ``attn_outputs``. Default: ``True``. attn_mask: If specified, a 2D or 3D mask preventing attention to certain positions. Must be of shape :math:`(L, S)` or :math:`(N\cdot\text{num\_heads}, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and :math:`S` is the source sequence length. A 2D mask will be broadcasted across the batch while a 3D mask allows for a different mask for each entry in the batch. Binary, byte, and float masks are supported. For a binary mask, a ``True`` value indicates that the corresponding position is not allowed to attend. For a byte mask, a non-zero value indicates that the corresponding position is not allowed to attend. For a float mask, the mask values will be added to the attention weight. Outputs: - **attn_output** - Attention outputs of shape :math:`(L, N, E)` when ``batch_first=False`` or :math:`(N, L, E)` when ``batch_first=True``, where :math:`L` is the target sequence length, :math:`N` is the batch size, and :math:`E` is the embedding dimension ``embed_dim``. - **attn_output_weights** - Attention output weights of shape :math:`(N, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and :math:`S` is the source sequence length. Only returned when ``need_weights=True``. cSsg|]}|jddqS)rr) transpose).0xrrr sz.MultiheadAttention.forward..T)r/rxryrzZuse_separate_proj_weightrbrcrd)r/rxryrzrrN)r]rjrZmulti_head_attention_forwardrgrkrarerorprqrlrnweightrfr/rbrcrdr{) rrvrwrrxryrzZ attn_outputZattn_output_weightsrrrrs0&   zMultiheadAttention.forward) r^TFFNNFNN)NTN)r r!r"r#r$rr>rrorprrrrtr&rrr'rr)rrr\Ts   ) r\csVeZdZUdZdgZedeeddfdd Ze e d d d Z e d d dZ Z S)PReLUaApplies the element-wise function: .. math:: \text{PReLU}(x) = \max(0,x) + a * \min(0,x) or .. math:: \text{PReLU}(x) = \begin{cases} x, & \text{ if } x \geq 0 \\ ax, & \text{ otherwise } \end{cases} Here :math:`a` is a learnable parameter. When called without arguments, `nn.PReLU()` uses a single parameter :math:`a` across all input channels. If called with `nn.PReLU(nChannels)`, a separate :math:`a` is used for each input channel. .. note:: weight decay should not be used when learning :math:`a` for good performance. .. note:: Channel dim is the 2nd dim of input. When input has dims < 2, then there is no channel dim and the number of channels = 1. Args: num_parameters (int): number of :math:`a` to learn. Although it takes an int as input, there is only two values are legitimate: 1, or the number of channels at input. Default: 1 init (float): the initial value of :math:`a`. Default: 0.25 Shape: - Input: :math:`( *)` where `*` means, any number of additional dimensions. - Output: :math:`(*)`, same shape as the input. Attributes: weight (Tensor): the learnable weights of shape (:attr:`num_parameters`). .. image:: ../scripts/activation_images/PReLU.png Examples:: >>> m = nn.PReLU() >>> input = torch.randn(2) >>> output = m(input) num_parametersr?N)rinitrcs<||d}||_tt|jttj|f|j||_dS)N)r_r`) rrrrr r>rmZfill_r)rrrr_r`rs)rrrr,s zPReLU.__init__)rrcCstj||jS)N)rZprelur)rrrrrr3sz PReLU.forward)rcCs dj|jS)Nznum_parameters={})rr)rrrrr6szPReLU.extra_repr)rrNN)r r!r"r#r$rNrr%rrrr)rr'rr)rrrs 0rc@s eZdZdZeedddZdS)SoftsignaApplies the element-wise function: .. math:: \text{SoftSign}(x) = \frac{x}{ 1 + |x|} Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Softsign.png Examples:: >>> m = nn.Softsign() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)rZsoftsign)rrrrrrMszSoftsign.forwardN)r r!r"r#rrrrrrr:src@s eZdZdZeedddZdS) TanhshrinkaApplies the element-wise function: .. math:: \text{Tanhshrink}(x) = x - \tanh(x) Shape: - Input: :math:`(*)`, where :math:`*` means any number of dimensions. - Output: :math:`(*)`, same shape as the input. .. image:: ../scripts/activation_images/Tanhshrink.png Examples:: >>> m = nn.Tanhshrink() >>> input = torch.randn(2) >>> output = m(input) )rrcCs tj|S)N)rZ tanhshrink)rrrrrrdszTanhshrink.forwardN)r r!r"r#rrrrrrrQsrcs^eZdZUdZdgZeedeeddfdd ZddZ e e d d d Z d d Z Z S)Softmina4Applies the Softmin function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range `[0, 1]` and sum to 1. Softmin is defined as: .. math:: \text{Softmin}(x_{i}) = \frac{\exp(-x_i)}{\sum_j \exp(-x_j)} Shape: - Input: :math:`(*)` where `*` means, any number of additional dimensions - Output: :math:`(*)`, same shape as the input Args: dim (int): A dimension along which Softmin will be computed (so every slice along dim will sum to 1). Returns: a Tensor of the same dimension and shape as the input, with values in the range [0, 1] Examples:: >>> m = nn.Softmin() >>> input = torch.randn(2, 3) >>> output = m(input) rLN)rLrcstt|j||_dS)N)rrrrL)rrL)rrrrszSoftmin.__init__cCs |jj|t|dsd|_dS)NrL)__dict__updatehasattrrL)rrurrrrts  zSoftmin.__setstate__)rrcCstj||jddS)N) _stacklevel)rZsoftminrL)rrrrrrszSoftmin.forwardcCsdj|jdS)Nz dim={dim})rL)rrL)rrrrrszSoftmin.extra_repr)N)r r!r"r#r$rrNrLrrtrrrr'rr)rrrhs rcsdeZdZUdZdgZeedeeddfdd ZddZ e e d d d Z e d d dZ ZS)SoftmaxaApplies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1. Softmax is defined as: .. math:: \text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)} When the input Tensor is a sparse tensor then the unspecifed values are treated as ``-inf``. Shape: - Input: :math:`(*)` where `*` means, any number of additional dimensions - Output: :math:`(*)`, same shape as the input Returns: a Tensor of the same dimension and shape as the input with values in the range [0, 1] Args: dim (int): A dimension along which Softmax will be computed (so every slice along dim will sum to 1). .. note:: This module doesn't work directly with NLLLoss, which expects the Log to be computed between the Softmax and itself. Use `LogSoftmax` instead (it's faster and has better numerical properties). Examples:: >>> m = nn.Softmax(dim=1) >>> input = torch.randn(2, 3) >>> output = m(input) rLN)rLrcstt|j||_dS)N)rrrrL)rrL)rrrrszSoftmax.__init__cCs |jj|t|dsd|_dS)NrL)rrrrL)rrurrrrts  zSoftmax.__setstate__)rrcCstj||jddS)Nr)r)rsoftmaxrL)rrrrrrszSoftmax.forward)rcCsdj|jdS)Nz dim={dim})rL)rrL)rrrrrszSoftmax.extra_repr)N)r r!r"r#r$rrNrLrrtrrr)rr'rr)rrrs %rc@s eZdZdZeedddZdS) Softmax2da|Applies SoftMax over features to each spatial location. When given an image of ``Channels x Height x Width``, it will apply `Softmax` to each location :math:`(Channels, h_i, w_j)` Shape: - Input: :math:`(N, C, H, W)` or :math:`(C, H, W)`. - Output: :math:`(N, C, H, W)` or :math:`(C, H, W)` (same shape as input) Returns: a Tensor of the same dimension and shape as the input with values in the range [0, 1] Examples:: >>> m = nn.Softmax2d() >>> # you softmax over the 2nd dimension >>> input = torch.randn(2, 3, 12, 13) >>> output = m(input) )rrcCs0|jdks |jdks tdtj|dddS)Nr.z-Softmax2d requires a 3D or 4D tensor as inputr)r)rLr:rr)rrrrrrs zSoftmax2d.forwardN)r r!r"r#rrrrrrrsrcs^eZdZUdZdgZeedeeddfdd ZddZ e e d d d Z d d Z Z S) LogSoftmaxaApplies the :math:`\log(\text{Softmax}(x))` function to an n-dimensional input Tensor. The LogSoftmax formulation can be simplified as: .. math:: \text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right) Shape: - Input: :math:`(*)` where `*` means, any number of additional dimensions - Output: :math:`(*)`, same shape as the input Args: dim (int): A dimension along which LogSoftmax will be computed. Returns: a Tensor of the same dimension and shape as the input with values in the range [-inf, 0) Examples:: >>> m = nn.LogSoftmax() >>> input = torch.randn(2, 3) >>> output = m(input) rLN)rLrcstt|j||_dS)N)rrrrL)rrL)rrrrszLogSoftmax.__init__cCs |jj|t|dsd|_dS)NrL)rrrrL)rrurrrrt s  zLogSoftmax.__setstate__)rrcCstj||jddS)Nr)r)rZ log_softmaxrL)rrrrrrszLogSoftmax.forwardcCsdj|jdS)Nz dim={dim})rL)rrL)rrrrrszLogSoftmax.extra_repr)N)r r!r"r#r$rrNrLrrtrrrr'rr)rrrs r)0r8typingrrr>rZlinearrZ torch.nn.initrrr Ztorch.nn.parameterr moduler rr rrr(r*r2r<r=r?r@rBrCrDrErHrJrKrOrPrSrVrWrZr\rrrrrrrrrrrsL     2*?H#'"(*,1!(2,(%B/9