|
40 | 40 | __all__ = [ |
41 | 41 | 'Unfold', |
42 | 42 | 'Linear', |
| 43 | + 'Softmax', |
43 | 44 | 'AvgPool1D', |
44 | 45 | 'AvgPool2D', |
45 | 46 | 'AvgPool3D', |
@@ -463,7 +464,6 @@ def to_list_if_necessary(x): |
463 | 464 | strides=to_list_if_necessary(self.strides), |
464 | 465 | paddings=to_list_if_necessary(self.paddings), |
465 | 466 | dilations=to_list_if_necessary(self.dilations), |
466 | | - name=self.name, |
467 | 467 | ) |
468 | 468 |
|
469 | 469 |
|
@@ -610,6 +610,135 @@ def reset_parameters(self) -> None: |
610 | 610 | nn.init.uniform_(self.bias, -bound, bound) |
611 | 611 |
|
612 | 612 |
|
| 613 | +class Softmax(nn.Layer): |
| 614 | + r""" |
| 615 | + Softmax Activation. |
| 616 | +
|
| 617 | + This operator implements the softmax layer. The calculation process is as follows: |
| 618 | +
|
| 619 | + 1. The dimension :attr:`dim` of ``input`` will be permuted to the last. |
| 620 | +
|
| 621 | + 2. Then ``input`` will be logically flattened to a 2-D matrix. The matrix's second |
| 622 | + dimension(row length) is the same as the dimension :attr:`dim` of ``input``, |
| 623 | + and the first dimension(column length) is the product of all other dimensions |
| 624 | + of ``input``. For each row of the matrix, the softmax operator squashes the |
| 625 | + K-dimensional(K is the width of the matrix, which is also the size of ``input``'s |
| 626 | + dimension :attr:`dim`) vector of arbitrary real values to a K-dimensional |
| 627 | + vector of real values in the range [0, 1] that add up to 1. |
| 628 | +
|
| 629 | + 3. After the softmax operation is completed, the inverse operations of steps 1 and 2 |
| 630 | + are performed to restore the two-dimensional matrix to the same dimension as the ``input`` . |
| 631 | +
|
| 632 | + It computes the exponential of the given dimension and the sum of exponential |
| 633 | + values of all the other dimensions in the K-dimensional vector input. |
| 634 | + Then the ratio of the exponential of the given dimension and the sum of |
| 635 | + exponential values of all the other dimensions is the output of the softmax |
| 636 | + operator. |
| 637 | +
|
| 638 | + For each row :math:`i` and each column :math:`j` in the matrix, we have: |
| 639 | +
|
| 640 | + .. math:: |
| 641 | +
|
| 642 | + Softmax[i, j] = \frac{\exp(x[i, j])}{\sum_j(exp(x[i, j])} |
| 643 | +
|
| 644 | + Example: |
| 645 | +
|
| 646 | + .. code-block:: text |
| 647 | +
|
| 648 | + Case 1: |
| 649 | + Input: |
| 650 | + x.shape = [2, 3, 4] |
| 651 | + x.data = [[[2.0, 3.0, 4.0, 5.0], |
| 652 | + [3.0, 4.0, 5.0, 6.0], |
| 653 | + [7.0, 8.0, 8.0, 9.0]], |
| 654 | + [[1.0, 2.0, 3.0, 4.0], |
| 655 | + [5.0, 6.0, 7.0, 8.0], |
| 656 | + [6.0, 7.0, 8.0, 9.0]]] |
| 657 | +
|
| 658 | + Attrs: |
| 659 | + dim = -1 |
| 660 | +
|
| 661 | + Output: |
| 662 | + out.shape = [2, 3, 4] |
| 663 | + out.data = [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426], |
| 664 | + [0.0320586 , 0.08714432, 0.23688282, 0.64391426], |
| 665 | + [0.07232949, 0.19661193, 0.19661193, 0.53444665]], |
| 666 | + [[0.0320586 , 0.08714432, 0.23688282, 0.64391426], |
| 667 | + [0.0320586 , 0.08714432, 0.23688282, 0.64391426], |
| 668 | + [0.0320586 , 0.08714432, 0.23688282, 0.64391426]]] |
| 669 | +
|
| 670 | + Case 2: |
| 671 | + Input: |
| 672 | + x.shape = [2, 3, 4] |
| 673 | + x.data = [[[2.0, 3.0, 4.0, 5.0], |
| 674 | + [3.0, 4.0, 5.0, 6.0], |
| 675 | + [7.0, 8.0, 8.0, 9.0]], |
| 676 | + [[1.0, 2.0, 3.0, 4.0], |
| 677 | + [5.0, 6.0, 7.0, 8.0], |
| 678 | + [6.0, 7.0, 8.0, 9.0]]] |
| 679 | + Attrs: |
| 680 | + dim = 1 |
| 681 | +
|
| 682 | + Output: |
| 683 | + out.shape = [2, 3, 4] |
| 684 | + out.data = [[[0.00657326, 0.00657326, 0.01714783, 0.01714783], |
| 685 | + [0.01786798, 0.01786798, 0.04661262, 0.04661262], |
| 686 | + [0.97555875, 0.97555875, 0.93623955, 0.93623955]], |
| 687 | + [[0.00490169, 0.00490169, 0.00490169, 0.00490169], |
| 688 | + [0.26762315, 0.26762315, 0.26762315, 0.26762315], |
| 689 | + [0.72747516, 0.72747516, 0.72747516, 0.72747516]]] |
| 690 | +
|
| 691 | + Parameters: |
| 692 | + dim (int, optional): The dim along which to perform log_softmax |
| 693 | + calculations. It should be in range [-D, D), where D is the |
| 694 | + dimensions of ``input`` . If ``dim`` < 0, it works the same way as |
| 695 | + :math:`dim + D` . Default is None. |
| 696 | +
|
| 697 | + Shape: |
| 698 | + - input: Tensor with any shape. |
| 699 | + - output: Tensor with the same shape as input. |
| 700 | +
|
| 701 | + Examples: |
| 702 | + .. code-block:: python |
| 703 | +
|
| 704 | + >>> import paddle |
| 705 | +
|
| 706 | + >>> x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0], |
| 707 | + ... [3.0, 4.0, 5.0, 6.0], |
| 708 | + ... [7.0, 8.0, 8.0, 9.0]], |
| 709 | + ... [[1.0, 2.0, 3.0, 4.0], |
| 710 | + ... [5.0, 6.0, 7.0, 8.0], |
| 711 | + ... [6.0, 7.0, 8.0, 9.0]]], dtype='float32') |
| 712 | + >>> m = paddle.compat.nn.Softmax() |
| 713 | + >>> out = m(x) |
| 714 | + >>> print(out) |
| 715 | + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 716 | + [[[0.73105854, 0.73105854, 0.73105854, 0.73105854], |
| 717 | + [0.11920292, 0.11920292, 0.11920292, 0.11920292], |
| 718 | + [0.73105854, 0.73105854, 0.50000000, 0.50000000]], |
| 719 | + [[0.26894143, 0.26894143, 0.26894143, 0.26894143], |
| 720 | + [0.88079703, 0.88079703, 0.88079703, 0.88079703], |
| 721 | + [0.26894143, 0.26894143, 0.50000000, 0.50000000]]]) |
| 722 | +
|
| 723 | + """ |
| 724 | + |
| 725 | + @ForbidKeywordsDecorator( |
| 726 | + illegal_keys={"axis"}, |
| 727 | + func_name="paddle.compat.nn.Softmax", |
| 728 | + correct_name="paddle.nn.Softmax", |
| 729 | + ) |
| 730 | + def __init__(self, dim: int | None = None) -> None: |
| 731 | + super().__init__() |
| 732 | + self._dim = dim |
| 733 | + self._dtype = None |
| 734 | + |
| 735 | + def forward(self, input: Tensor) -> Tensor: |
| 736 | + return functional.softmax(input, self._dim) |
| 737 | + |
| 738 | + def extra_repr(self) -> str: |
| 739 | + return f"dim={self.dim}" |
| 740 | + |
| 741 | + |
613 | 742 | AvgPool1d = AvgPool1D |
614 | 743 | AvgPool2d = AvgPool2D |
615 | 744 | AvgPool3d = AvgPool3D |
0 commit comments