5 from .module
import Module
6 from ..
import functional
as F
7 from ..._jit_internal
import weak_module, weak_script_method
12 r"""Thresholds each element of the input Tensor. 14 Threshold is defined as: 19 x, &\text{ if } x > \text{threshold} \\ 20 \text{value}, &\text{ otherwise } 24 threshold: The value to threshold at 25 value: The value to replace with 26 inplace: can optionally do the operation in-place. Default: ``False`` 29 - Input: :math:`(N, *)` where `*` means, any number of additional 31 - Output: :math:`(N, *)`, same shape as the input 35 >>> m = nn.Threshold(0.1, 20) 36 >>> input = torch.randn(2) 39 __constants__ = [
'threshold',
'value',
'inplace']
41 def __init__(self, threshold, value, inplace=False):
42 super(Threshold, self).__init__()
49 def forward(self, input):
53 inplace_str =
', inplace' if self.
inplace else '' 54 return 'threshold={}, value={}{}'.format(
61 r"""Applies the rectified linear unit function element-wise: 63 :math:`\text{ReLU}(x)= \max(0, x)` 66 inplace: can optionally do the operation in-place. Default: ``False`` 69 - Input: :math:`(N, *)` where `*` means, any number of additional 71 - Output: :math:`(N, *)`, same shape as the input 73 .. image:: scripts/activation_images/ReLU.png 78 >>> input = torch.randn(2) 82 An implementation of CReLU - https://arxiv.org/abs/1603.05201 85 >>> input = torch.randn(2).unsqueeze(0) 86 >>> output = torch.cat((m(input),m(-input))) 89 def __init__(self, inplace=False):
90 super(ReLU, self).__init__(0., 0., inplace)
93 inplace_str =
'inplace' if self.
inplace else '' 99 r"""Applies the randomized leaky rectified liner unit function, element-wise, 100 as described in the paper: 102 `Empirical Evaluation of Rectified Activations in Convolutional Network`_. 104 The function is defined as: 109 x & \text{if } x \geq 0 \\ 110 ax & \text{ otherwise } 113 where :math:`a` is randomly sampled from uniform distribution 114 :math:`\mathcal{U}(\text{lower}, \text{upper})`. 116 See: https://arxiv.org/pdf/1505.00853.pdf 119 lower: lower bound of the uniform distribution. Default: :math:`\frac{1}{8}` 120 upper: upper bound of the uniform distribution. Default: :math:`\frac{1}{3}` 121 inplace: can optionally do the operation in-place. Default: ``False`` 124 - Input: :math:`(N, *)` where `*` means, any number of additional 126 - Output: :math:`(N, *)`, same shape as the input 130 >>> m = nn.RReLU(0.1, 0.3) 131 >>> input = torch.randn(2) 132 >>> output = m(input) 134 .. _`Empirical Evaluation of Rectified Activations in Convolutional Network`: 135 https://arxiv.org/abs/1505.00853 137 __constants__ = [
'lower',
'upper',
'inplace']
139 def __init__(self, lower=1. / 8, upper=1. / 3, inplace=False):
140 super(RReLU, self).__init__()
146 def forward(self, input):
149 def extra_repr(self):
150 inplace_str =
', inplace' if self.
inplace else '' 151 return 'lower={}, upper={}{}'.format(self.
lower, self.
upper, inplace_str)
156 r"""Applies the HardTanh function element-wise 158 HardTanh is defined as: 161 \text{HardTanh}(x) = \begin{cases} 162 1 & \text{ if } x > 1 \\ 163 -1 & \text{ if } x < -1 \\ 164 x & \text{ otherwise } \\ 167 The range of the linear region :math:`[-1, 1]` can be adjusted using 168 :attr:`min_val` and :attr:`max_val`. 171 min_val: minimum value of the linear region range. Default: -1 172 max_val: maximum value of the linear region range. Default: 1 173 inplace: can optionally do the operation in-place. Default: ``False`` 175 Keyword arguments :attr:`min_value` and :attr:`max_value` 176 have been deprecated in favor of :attr:`min_val` and :attr:`max_val`. 179 - Input: :math:`(N, *)` where `*` means, any number of additional 181 - Output: :math:`(N, *)`, same shape as the input 183 .. image:: scripts/activation_images/Hardtanh.png 187 >>> m = nn.Hardtanh(-2, 2) 188 >>> input = torch.randn(2) 189 >>> output = m(input) 191 __constants__ = [
'min_val',
'max_val',
'inplace']
193 def __init__(self, min_val=-1., max_val=1., inplace=False, min_value=None, max_value=None):
194 super(Hardtanh, self).__init__()
195 if min_value
is not None:
196 warnings.warn(
"keyword argument min_value is deprecated and renamed to min_val")
198 if max_value
is not None:
199 warnings.warn(
"keyword argument max_value is deprecated and renamed to max_val")
208 def forward(self, input):
211 def extra_repr(self):
212 inplace_str =
', inplace' if self.
inplace else '' 213 return 'min_val={}, max_val={}{}'.format(
220 r"""Applies the element-wise function: 223 \text{ReLU6}(x) = \min(\max(0,x), 6) 226 inplace: can optionally do the operation in-place. Default: ``False`` 229 - Input: :math:`(N, *)` where `*` means, any number of additional 231 - Output: :math:`(N, *)`, same shape as the input 233 .. image:: scripts/activation_images/ReLU6.png 238 >>> input = torch.randn(2) 239 >>> output = m(input) 242 def __init__(self, inplace=False):
243 super(ReLU6, self).__init__(0., 6., inplace)
245 def extra_repr(self):
246 inplace_str =
'inplace' if self.
inplace else '' 252 r"""Applies the element-wise function: 255 \text{Sigmoid}(x) = \frac{1}{1 + \exp(-x)} 259 - Input: :math:`(N, *)` where `*` means, any number of additional 261 - Output: :math:`(N, *)`, same shape as the input 263 .. image:: scripts/activation_images/Sigmoid.png 268 >>> input = torch.randn(2) 269 >>> output = m(input) 273 def forward(self, input):
274 return torch.sigmoid(input)
279 r"""Applies the element-wise function: 282 \text{Tanh}(x) = \tanh(x) = \frac{e^x - e^{-x}} {e^x + e^{-x}} 285 - Input: :math:`(N, *)` where `*` means, any number of additional 287 - Output: :math:`(N, *)`, same shape as the input 289 .. image:: scripts/activation_images/Tanh.png 294 >>> input = torch.randn(2) 295 >>> output = m(input) 299 def forward(self, input):
300 return torch.tanh(input)
305 r"""Applies the element-wise function: 308 \text{ELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x) - 1)) 311 alpha: the :math:`\alpha` value for the ELU formulation. Default: 1.0 312 inplace: can optionally do the operation in-place. Default: ``False`` 315 - Input: :math:`(N, *)` where `*` means, any number of additional 317 - Output: :math:`(N, *)`, same shape as the input 319 .. image:: scripts/activation_images/ELU.png 324 >>> input = torch.randn(2) 325 >>> output = m(input) 327 __constants__ = [
'alpha',
'inplace']
329 def __init__(self, alpha=1., inplace=False):
330 super(ELU, self).__init__()
335 def forward(self, input):
338 def extra_repr(self):
339 inplace_str =
', inplace' if self.
inplace else '' 340 return 'alpha={}{}'.format(self.
alpha, inplace_str)
345 r"""Applies the element-wise function: 348 \text{CELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x/\alpha) - 1)) 350 More details can be found in the paper `Continuously Differentiable Exponential Linear Units`_ . 353 alpha: the :math:`\alpha` value for the CELU formulation. Default: 1.0 354 inplace: can optionally do the operation in-place. Default: ``False`` 357 - Input: :math:`(N, *)` where `*` means, any number of additional 359 - Output: :math:`(N, *)`, same shape as the input 361 .. image:: scripts/activation_images/CELU.png 366 >>> input = torch.randn(2) 367 >>> output = m(input) 369 .. _`Continuously Differentiable Exponential Linear Units`: 370 https://arxiv.org/abs/1704.07483 372 __constants__ = [
'alpha',
'inplace']
374 def __init__(self, alpha=1., inplace=False):
375 super(CELU, self).__init__()
380 def forward(self, input):
383 def extra_repr(self):
384 inplace_str =
', inplace' if self.
inplace else '' 385 return 'alpha={}{}'.format(self.
alpha, inplace_str)
390 r"""Applied element-wise, as: 393 \text{SELU}(x) = \text{scale} * (\max(0,x) + \min(0, \alpha * (\exp(x) - 1))) 395 with :math:`\alpha = 1.6732632423543772848170429916717` and 396 :math:`\text{scale} = 1.0507009873554804934193349852946`. 398 More details can be found in the paper `Self-Normalizing Neural Networks`_ . 401 inplace (bool, optional): can optionally do the operation in-place. Default: ``False`` 404 - Input: :math:`(N, *)` where `*` means, any number of additional 406 - Output: :math:`(N, *)`, same shape as the input 408 .. image:: scripts/activation_images/SELU.png 413 >>> input = torch.randn(2) 414 >>> output = m(input) 416 .. _Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515 418 __constants__ = [
'inplace']
420 def __init__(self, inplace=False):
421 super(SELU, self).__init__()
425 def forward(self, input):
426 return F.selu(input, self.
inplace)
428 def extra_repr(self):
429 inplace_str =
'inplace' if self.
inplace else '' 435 r"""Applies the gated linear unit function 436 :math:`{GLU}(a, b)= a \otimes \sigma(b)` where :math:`a` is the first half 437 of the input matrices and :math:`b` is the second half. 440 dim (int): the dimension on which to split the input. Default: -1 443 - Input: :math:`(\ast_1, N, \ast_2)` where `*` means, any number of additional 445 - Output: :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2` 450 >>> input = torch.randn(4, 2) 451 >>> output = m(input) 453 __constants__ = [
'dim']
455 def __init__(self, dim=-1):
456 super(GLU, self).__init__()
460 def forward(self, input):
461 return F.glu(input, self.
dim)
463 def extra_repr(self):
464 return 'dim={}'.format(self.
dim)
469 r"""Applies the hard shrinkage function element-wise: 472 \text{HardShrink}(x) = 474 x, & \text{ if } x > \lambda \\ 475 x, & \text{ if } x < -\lambda \\ 476 0, & \text{ otherwise } 480 lambd: the :math:`\lambda` value for the Hardshrink formulation. Default: 0.5 483 - Input: :math:`(N, *)` where `*` means, any number of additional 485 - Output: :math:`(N, *)`, same shape as the input 487 .. image:: scripts/activation_images/Hardshrink.png 491 >>> m = nn.Hardshrink() 492 >>> input = torch.randn(2) 493 >>> output = m(input) 495 __constants__ = [
'lambd']
497 def __init__(self, lambd=0.5):
498 super(Hardshrink, self).__init__()
502 def forward(self, input):
503 return F.hardshrink(input, self.
lambd)
505 def extra_repr(self):
506 return '{}'.format(self.
lambd)
511 r"""Applies the element-wise function: 514 \text{LeakyReLU}(x) = \max(0, x) + \text{negative\_slope} * \min(0, x) 520 \text{LeakyRELU}(x) = 522 x, & \text{ if } x \geq 0 \\ 523 \text{negative\_slope} \times x, & \text{ otherwise } 527 negative_slope: Controls the angle of the negative slope. Default: 1e-2 528 inplace: can optionally do the operation in-place. Default: ``False`` 531 - Input: :math:`(N, *)` where `*` means, any number of additional 533 - Output: :math:`(N, *)`, same shape as the input 535 .. image:: scripts/activation_images/LeakyReLU.png 539 >>> m = nn.LeakyReLU(0.1) 540 >>> input = torch.randn(2) 541 >>> output = m(input) 543 __constants__ = [
'inplace',
'negative_slope']
545 def __init__(self, negative_slope=1e-2, inplace=False):
546 super(LeakyReLU, self).__init__()
551 def forward(self, input):
554 def extra_repr(self):
555 inplace_str =
', inplace' if self.
inplace else '' 556 return 'negative_slope={}{}'.format(self.
negative_slope, inplace_str)
561 r"""Applies the element-wise function: 564 \text{LogSigmoid}(x) = \log\left(\frac{ 1 }{ 1 + \exp(-x)}\right) 567 - Input: :math:`(N, *)` where `*` means, any number of additional 569 - Output: :math:`(N, *)`, same shape as the input 571 .. image:: scripts/activation_images/LogSigmoid.png 575 >>> m = nn.LogSigmoid() 576 >>> input = torch.randn(2) 577 >>> output = m(input) 581 def forward(self, input):
582 return F.logsigmoid(input)
587 r"""Applies the element-wise function: 590 \text{Softplus}(x) = \frac{1}{\beta} * \log(1 + \exp(\beta * x)) 592 SoftPlus is a smooth approximation to the ReLU function and can be used 593 to constrain the output of a machine to always be positive. 595 For numerical stability the implementation reverts to the linear function 596 for inputs above a certain value. 599 beta: the :math:`\beta` value for the Softplus formulation. Default: 1 600 threshold: values above this revert to a linear function. Default: 20 603 - Input: :math:`(N, *)` where `*` means, any number of additional 605 - Output: :math:`(N, *)`, same shape as the input 607 .. image:: scripts/activation_images/Softplus.png 611 >>> m = nn.Softplus() 612 >>> input = torch.randn(2) 613 >>> output = m(input) 615 __constants__ = [
'beta',
'threshold']
617 def __init__(self, beta=1, threshold=20):
618 super(Softplus, self).__init__()
623 def forward(self, input):
626 def extra_repr(self):
627 return 'beta={}, threshold={}'.format(self.
beta, self.
threshold)
632 r"""Applies the soft shrinkage function elementwise: 635 \text{SoftShrinkage}(x) = 637 x - \lambda, & \text{ if } x > \lambda \\ 638 x + \lambda, & \text{ if } x < -\lambda \\ 639 0, & \text{ otherwise } 643 lambd: the :math:`\lambda` value for the Softshrink formulation. Default: 0.5 646 - Input: :math:`(N, *)` where `*` means, any number of additional 648 - Output: :math:`(N, *)`, same shape as the input 650 .. image:: scripts/activation_images/Softshrink.png 654 >>> m = nn.Softshrink() 655 >>> input = torch.randn(2) 656 >>> output = m(input) 658 __constants__ = [
'lambd']
660 def __init__(self, lambd=0.5):
661 super(Softshrink, self).__init__()
665 def forward(self, input):
666 return F.softshrink(input, self.
lambd)
668 def extra_repr(self):
669 return str(self.
lambd)
674 r"""Applies the element-wise function: 677 \text{PReLU}(x) = \max(0,x) + a * \min(0,x) 684 x, & \text{ if } x \geq 0 \\ 685 ax, & \text{ otherwise } 688 Here :math:`a` is a learnable parameter. When called without arguments, `nn.PReLU()` uses a single 689 parameter :math:`a` across all input channels. If called with `nn.PReLU(nChannels)`, 690 a separate :math:`a` is used for each input channel. 694 weight decay should not be used when learning :math:`a` for good performance. 697 Channel dim is the 2nd dim of input. When input has dims < 2, then there is 698 no channel dim and the number of channels = 1. 701 num_parameters (int): number of :math:`a` to learn. 702 Although it takes an int as input, there is only two values are legitimate: 703 1, or the number of channels at input. Default: 1 704 init (float): the initial value of :math:`a`. Default: 0.25 707 - Input: :math:`(N, *)` where `*` means, any number of additional 709 - Output: :math:`(N, *)`, same shape as the input 712 weight (Tensor): the learnable weights of shape (:attr:`num_parameters`). 714 .. image:: scripts/activation_images/PReLU.png 719 >>> input = torch.randn(2) 720 >>> output = m(input) 723 def __init__(self, num_parameters=1, init=0.25):
725 super(PReLU, self).__init__()
729 def forward(self, input):
730 return F.prelu(input, self.
weight)
732 def extra_repr(self):
738 r"""Applies the element-wise function: 741 \text{SoftSign}(x) = \frac{x}{ 1 + |x|} 744 - Input: :math:`(N, *)` where `*` means, any number of additional 746 - Output: :math:`(N, *)`, same shape as the input 748 .. image:: scripts/activation_images/Softsign.png 752 >>> m = nn.Softsign() 753 >>> input = torch.randn(2) 754 >>> output = m(input) 758 def forward(self, input):
759 return F.softsign(input)
764 r"""Applies the element-wise function: 767 \text{Tanhshrink}(x) = x - \text{Tanh}(x) 770 - Input: :math:`(N, *)` where `*` means, any number of additional 772 - Output: :math:`(N, *)`, same shape as the input 774 .. image:: scripts/activation_images/Tanhshrink.png 778 >>> m = nn.Tanhshrink() 779 >>> input = torch.randn(2) 780 >>> output = m(input) 784 def forward(self, input):
785 return F.tanhshrink(input)
790 r"""Applies the Softmin function to an n-dimensional input Tensor 791 rescaling them so that the elements of the n-dimensional output Tensor 792 lie in the range `[0, 1]` and sum to 1. 794 Softmin is defined as: 797 \text{Softmin}(x_{i}) = \frac{\exp(-x_i)}{\sum_j \exp(-x_j)} 800 - Input: :math:`(*)` where `*` means, any number of additional 802 - Output: :math:`(*)`, same shape as the input 805 dim (int): A dimension along which Softmin will be computed (so every slice 806 along dim will sum to 1). 809 a Tensor of the same dimension and shape as the input, with 810 values in the range [0, 1] 815 >>> input = torch.randn(2, 3) 816 >>> output = m(input) 818 __constants__ = [
'dim']
820 def __init__(self, dim=None):
821 super(Softmin, self).__init__()
825 def forward(self, input):
826 return F.softmin(input, self.
dim, _stacklevel=5)
831 r"""Applies the Softmax function to an n-dimensional input Tensor 832 rescaling them so that the elements of the n-dimensional output Tensor 833 lie in the range [0,1] and sum to 1. 835 Softmax is defined as: 838 \text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)} 841 - Input: :math:`(*)` where `*` means, any number of additional 843 - Output: :math:`(*)`, same shape as the input 846 a Tensor of the same dimension and shape as the input with 847 values in the range [0, 1] 850 dim (int): A dimension along which Softmax will be computed (so every slice 851 along dim will sum to 1). 854 This module doesn't work directly with NLLLoss, 855 which expects the Log to be computed between the Softmax and itself. 856 Use `LogSoftmax` instead (it's faster and has better numerical properties). 861 >>> input = torch.randn(2, 3) 862 >>> output = m(input) 864 __constants__ = [
'dim']
866 def __init__(self, dim=None):
867 super(Softmax, self).__init__()
870 def __setstate__(self, state):
871 self.__dict__.update(state)
872 if not hasattr(self,
'dim'):
876 def forward(self, input):
877 return F.softmax(input, self.
dim, _stacklevel=5)
882 r"""Applies SoftMax over features to each spatial location. 884 When given an image of ``Channels x Height x Width``, it will 885 apply `Softmax` to each location :math:`(Channels, h_i, w_j)` 888 - Input: :math:`(N, C, H, W)` 889 - Output: :math:`(N, C, H, W)` (same shape as input) 892 a Tensor of the same dimension and shape as the input with 893 values in the range [0, 1] 897 >>> m = nn.Softmax2d() 898 >>> # you softmax over the 2nd dimension 899 >>> input = torch.randn(2, 3, 12, 13) 900 >>> output = m(input) 904 def forward(self, input):
905 assert input.dim() == 4,
'Softmax2d requires a 4D tensor as input' 906 return F.softmax(input, 1, _stacklevel=5)
911 r"""Applies the :math:`\log(\text{Softmax}(x))` function to an n-dimensional 912 input Tensor. The LogSoftmax formulation can be simplified as: 915 \text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right) 918 - Input: :math:`(*)` where `*` means, any number of additional 920 - Output: :math:`(*)`, same shape as the input 923 dim (int): A dimension along which LogSoftmax will be computed. 926 a Tensor of the same dimension and shape as the input with 927 values in the range [-inf, 0) 931 >>> m = nn.LogSoftmax() 932 >>> input = torch.randn(2, 3) 933 >>> output = m(input) 935 __constants__ = [
'dim']
937 def __init__(self, dim=None):
938 super(LogSoftmax, self).__init__()
941 def __setstate__(self, state):
942 self.__dict__.update(state)
943 if not hasattr(self,
'dim'):
947 def forward(self, input):
948 return F.log_softmax(input, self.
dim, _stacklevel=5)