Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9734,12 +9734,10 @@ def swish(x, beta=1.0, name=None):
@deprecated(since="2.0.0", update_to="paddle.nn.functional.prelu")
def prelu(x, mode, param_attr=None, name=None):
"""
:api_attr: Static Graph

Equation:
prelu activation.

.. math::
y = \max(0, x) + \\alpha * \min(0, x)
prelu(x) = max(0, x) + \\alpha * min(0, x)

There are three modes for the activation:

Expand All @@ -9749,34 +9747,28 @@ def prelu(x, mode, param_attr=None, name=None):
channel: Elements in same channel share same alpha.
element: All elements do not share alpha. Each element has its own alpha.

Args:
x (Variable): The input Tensor or LoDTensor with data type float32.
Parameters:
x (Tensor): The input Tensor or LoDTensor with data type float32.
mode (str): The mode for weight sharing.
param_attr(ParamAttr|None): The parameter attribute for the learnable
weight (alpha), it can be create by ParamAttr. None by default.
For detailed information, please refer to :ref:`api_fluid_ParamAttr`.
name(str|None): For detailed information, please refer
to :ref:`api_guide_Name`. Usually name is no need to set and
None by default.
param_attr (ParamAttr|None, optional): The parameter attribute for the learnable
weight (alpha), it can be create by ParamAttr. None by default.
For detailed information, please refer to :ref:`api_fluid_ParamAttr`.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.

Returns:
Variable:

output(Variable): The tensor or LoDTensor with the same shape as input.
The data type is float32.
Tensor: A tensor with the same shape and data type as x.

Examples:

.. code-block:: python

import paddle.fluid as fluid
import paddle
paddle.enable_static()
from paddle.fluid.param_attr import ParamAttr
x = fluid.data(name="x", shape=[None,5,10,10], dtype="float32")
mode = 'channel'
output = fluid.layers.prelu(
x,mode,param_attr=ParamAttr(name='alpha'))

x = paddle.to_tensor([-1., 2., 3.])
param = paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(0.2))
out = paddle.static.nn.prelu(x, 'all', param)
# [-0.2, 2., 3.]

"""
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'prelu')
Expand Down
45 changes: 20 additions & 25 deletions python/paddle/nn/functional/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def elu(x, alpha=1.0, name=None):

import paddle
import paddle.nn.functional as F
import numpy as np

x = paddle.to_tensor(np.array([[-1,6],[1,15.6]]))
x = paddle.to_tensor([[-1., 6.], [1., 15.6]])
out = F.elu(x, alpha=0.2)
# [[-0.12642411 6. ]
# [ 1. 15.6 ]]
Expand Down Expand Up @@ -131,11 +130,14 @@ def gelu(x, approximate=False, name=None):

import paddle
import paddle.nn.functional as F
import numpy as np

x = paddle.to_tensor(np.array([[-1, 0.5],[1, 1.5]]))
out1 = F.gelu(x) # [-0.158655 0.345731 0.841345 1.39979]
out2 = F.gelu(x, True) # [-0.158808 0.345714 0.841192 1.39957]
x = paddle.to_tensor([[-1, 0.5], [1, 1.5]])
out1 = F.gelu(x)
# [[-0.15865529, 0.34573123],
# [ 0.84134471, 1.39978933]]
out2 = F.gelu(x, True)
# [[-0.15880799, 0.34571400],
# [ 0.84119201, 1.39957154]]
"""

if in_dygraph_mode():
Expand Down Expand Up @@ -181,11 +183,8 @@ def hardshrink(x, threshold=0.5, name=None):

import paddle
import paddle.nn.functional as F
import numpy as np

paddle.disable_static()

x = paddle.to_tensor(np.array([-1, 0.3, 2.5]))
x = paddle.to_tensor([-1, 0.3, 2.5])
out = F.hardshrink(x) # [-1., 0., 2.5]

"""
Expand Down Expand Up @@ -385,11 +384,8 @@ def leaky_relu(x, negative_slope=0.01, name=None):

import paddle
import paddle.nn.functional as F
import numpy as np

paddle.disable_static()

x = paddle.to_tensor(np.array([-2, 0, 1], 'float32'))
x = paddle.to_tensor([-2., 0., 1.])
out = F.leaky_relu(x) # [-0.02, 0., 1.]

"""
Expand Down Expand Up @@ -1147,8 +1143,10 @@ def log_softmax(x, axis=-1, dtype=None, name=None):

.. math::

log\\_softmax[i, j] = log(softmax(x))
= log(\\frac{\exp(X[i, j])}{\\sum_j(exp(X[i, j])})
\\begin{aligned}
log\\_softmax[i, j] &= log(softmax(x)) \\\\
&= log(\\frac{\\exp(X[i, j])}{\\sum_j(\\exp(X[i, j])})
\\end{aligned}

Parameters:
x (Tensor): The input Tensor with data type float32, float64.
Expand All @@ -1174,16 +1172,13 @@ def log_softmax(x, axis=-1, dtype=None, name=None):

import paddle
import paddle.nn.functional as F
import numpy as np

paddle.disable_static()

x = np.array([[[-2.0, 3.0, -4.0, 5.0],
[3.0, -4.0, 5.0, -6.0],
[-7.0, -8.0, 8.0, 9.0]],
[[1.0, -2.0, -3.0, 4.0],
[-5.0, 6.0, 7.0, -8.0],
[6.0, 7.0, 8.0, 9.0]]], 'float32')
x = [[[-2.0, 3.0, -4.0, 5.0],
[3.0, -4.0, 5.0, -6.0],
[-7.0, -8.0, 8.0, 9.0]],
[[1.0, -2.0, -3.0, 4.0],
[-5.0, 6.0, 7.0, -8.0],
[6.0, 7.0, 8.0, 9.0]]]
x = paddle.to_tensor(x)
out1 = F.log_softmax(x)
out2 = F.log_softmax(x, dtype='float64')
Expand Down
39 changes: 16 additions & 23 deletions python/paddle/nn/layer/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ class ELU(layers.Layer):
.. code-block:: python

import paddle
import numpy as np

x = paddle.to_tensor(np.array([[-1,6],[1,15.6]]))
x = paddle.to_tensor([[-1. ,6.], [1., 15.6]])
m = paddle.nn.ELU(0.2)
out = m(x)
# [[-0.12642411 6. ]
Expand Down Expand Up @@ -166,11 +165,8 @@ class Hardshrink(layers.Layer):
.. code-block:: python

import paddle
import numpy as np

paddle.disable_static()

x = paddle.to_tensor(np.array([-1, 0.3, 2.5]))
x = paddle.to_tensor([-1, 0.3, 2.5])
m = paddle.nn.Hardshrink()
out = m(x) # [-1., 0., 2.5]
"""
Expand Down Expand Up @@ -293,11 +289,10 @@ class Hardtanh(layers.Layer):
.. code-block:: python

import paddle
import numpy as np

x = paddle.to_tensor(np.array([-1.5, 0.3, 2.5]))
x = paddle.to_tensor([-1.5, 0.3, 2.5])
m = paddle.nn.Hardtanh()
out = m(x) # # [-1., 0.3, 1.]
out = m(x) # [-1., 0.3, 1.]
"""

def __init__(self, min=-1.0, max=1.0, name=None):
Expand Down Expand Up @@ -397,9 +392,8 @@ class ReLU(layers.Layer):
.. code-block:: python

import paddle
import numpy as np

x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32'))
x = paddle.to_tensor([-2., 0., 1.])
m = paddle.nn.ReLU()
out = m(x) # [0., 0., 1.]
"""
Expand Down Expand Up @@ -613,7 +607,7 @@ class Hardsigmoid(layers.Layer):

import paddle

m = paddle.nn.Sigmoid()
m = paddle.nn.Hardsigmoid()
x = paddle.to_tensor([-4., 5., 1.])
out = m(x) # [0., 1, 0.666667]
"""
Expand Down Expand Up @@ -1016,8 +1010,10 @@ class LogSoftmax(layers.Layer):

.. math::

Out[i, j] = log(softmax(x))
= log(\\frac{\exp(X[i, j])}{\\sum_j(exp(X[i, j])})
\\begin{aligned}
Out[i, j] &= log(softmax(x)) \\\\
&= log(\\frac{\\exp(X[i, j])}{\\sum_j(\\exp(X[i, j])})
\\end{aligned}

Parameters:
axis (int, optional): The axis along which to perform log_softmax
Expand All @@ -1035,16 +1031,13 @@ class LogSoftmax(layers.Layer):
.. code-block:: python

import paddle
import numpy as np

paddle.disable_static()

x = np.array([[[-2.0, 3.0, -4.0, 5.0],
[3.0, -4.0, 5.0, -6.0],
[-7.0, -8.0, 8.0, 9.0]],
[[1.0, -2.0, -3.0, 4.0],
[-5.0, 6.0, 7.0, -8.0],
[6.0, 7.0, 8.0, 9.0]]])
x = [[[-2.0, 3.0, -4.0, 5.0],
[3.0, -4.0, 5.0, -6.0],
[-7.0, -8.0, 8.0, 9.0]],
[[1.0, -2.0, -3.0, 4.0],
[-5.0, 6.0, 7.0, -8.0],
[6.0, 7.0, 8.0, 9.0]]]
m = paddle.nn.LogSoftmax()
x = paddle.to_tensor(x)
out = m(x)
Expand Down
50 changes: 17 additions & 33 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,6 @@ def ones(shape, dtype=None, name=None):

def ones_like(x, dtype=None, name=None):
"""
:alias_main: paddle.ones_like
:alias: paddle.tensor.ones_like, paddle.tensor.creation.ones_like

This OP returns a Tensor filled with the value 1, with the same shape and
data type (use ``dtype`` if ``dtype`` is not None) as ``x``.

Expand All @@ -323,15 +320,13 @@ def ones_like(x, dtype=None, name=None):

Raise:
TypeError: If ``dtype`` is not None and is not bool, float16, float32,
float64, int32 or int64.
float64, int32 or int64.

Examples:
.. code-block:: python

import paddle

paddle.disable_static()

x = paddle.to_tensor([1,2,3])
out1 = paddle.zeros_like(x) # [1., 1., 1.]
out2 = paddle.zeros_like(x, dtype='int32') # [1, 1, 1]
Expand Down Expand Up @@ -380,9 +375,6 @@ def zeros(shape, dtype=None, name=None):

def zeros_like(x, dtype=None, name=None):
"""
:alias_main: paddle.zeros_like
:alias: paddle.tensor.zeros_like, paddle.tensor.creation.zeros_like

This OP returns a Tensor filled with the value 0, with the same shape and
data type (use ``dtype`` if ``dtype`` is not None) as ``x``.

Expand All @@ -403,16 +395,14 @@ def zeros_like(x, dtype=None, name=None):

Raise:
TypeError: If ``dtype`` is not None and is not bool, float16, float32,
float64, int32 or int64.
float64, int32 or int64.

Examples:
.. code-block:: python

import paddle

paddle.disable_static()

x = paddle.to_tensor([1,2,3])
x = paddle.to_tensor([1, 2, 3])
out1 = paddle.zeros_like(x) # [0., 0., 0.]
out2 = paddle.zeros_like(x, dtype='int32') # [0, 0, 0]

Expand Down Expand Up @@ -519,9 +509,6 @@ def full(shape, fill_value, dtype=None, name=None):

def arange(start=0, end=None, step=1, dtype=None, name=None):
"""
:alias_main: paddle.arange
:alias: paddle.tensor.arange, paddle.tensor.creation.arange

This OP returns a 1-D Tensor with spaced values within a given interval.

Values are generated into the half-open interval [``start``, ``end``) with
Expand Down Expand Up @@ -552,33 +539,30 @@ def arange(start=0, end=None, step=1, dtype=None, name=None):

Returns:
Tensor: A 1-D Tensor with values from the interval [``start``, ``end``)
taken with common difference ``step`` beginning from ``start``. Its
data type is set by ``dtype``.
taken with common difference ``step`` beginning from ``start``. Its
data type is set by ``dtype``.

Raises:
TypeError: If ``dtype`` is not int32, int64, float32, float64.

examples:

Examples:
.. code-block:: python

import paddle

paddle.disable_static()
import paddle

out1 = paddle.arange(5)
# [0, 1, 2, 3, 4]
out1 = paddle.arange(5)
# [0, 1, 2, 3, 4]

out2 = paddle.arange(3, 9, 2.0)
# [3, 5, 7]
out2 = paddle.arange(3, 9, 2.0)
# [3, 5, 7]

# use 4.999 instead of 5.0 to avoid floating point rounding errors
out3 = paddle.arange(4.999, dtype='float32')
# [0., 1., 2., 3., 4.]
# use 4.999 instead of 5.0 to avoid floating point rounding errors
out3 = paddle.arange(4.999, dtype='float32')
# [0., 1., 2., 3., 4.]

start_var = paddle.to_tensor([3])
out4 = paddle.arange(start_var, 7)
# [3, 4, 5, 6]
start_var = paddle.to_tensor([3])
out4 = paddle.arange(start_var, 7)
# [3, 4, 5, 6]

"""
if dtype is None:
Expand Down
Loading