Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update PixelShuffle and PRelu api in vision.py and activation.py;test=document_fix #45423

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions python/paddle/nn/functional/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,17 @@ def prelu(x, weight, data_format="NCHW", name=None):

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

data = np.array([[[[-2.0, 3.0, -4.0, 5.0],
data = paddle.to_tensor([[[[-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 = paddle.to_tensor(data)
w = paddle.to_tensor(np.array([0.25]).astype('float32'))
out = F.prelu(x, w)
[ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32')

w = paddle.to_tensor([0.25], dtype='float32')
out = F.prelu(data, w)
out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print(out)

# [[[[-0.5 , 3. , -1. , 5. ],
# [ 3. , -1. , 5. , -1.5 ],
# [-1.75, -2. , 8. , 9. ]],
Expand Down Expand Up @@ -698,9 +698,8 @@ def relu(x, name=None):

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

x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32'))
x = paddle.to_tensor([-2, 0, 1], dtype='float32')
out = F.relu(x) # [0., 0., 1.]
"""

Expand Down Expand Up @@ -867,9 +866,8 @@ def relu6(x, name=None):

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

x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
x = paddle.to_tensor([-1, 0.3, 6.5])
out = F.relu6(x) # [0, 0.3, 6]
"""
threshold = 6.0
Expand Down Expand Up @@ -920,9 +918,8 @@ def selu(x,

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

x = paddle.to_tensor(np.array([[0.0, 1.0],[2.0, 3.0]]))
x = paddle.to_tensor([[0.0, 1.0],[2.0, 3.0]])
out = F.selu(x) # [[0, 1.050701],[2.101402, 3.152103]]
"""
if scale <= 1.0:
Expand Down
21 changes: 12 additions & 9 deletions python/paddle/nn/functional/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,27 @@ def pixel_shuffle(x, upscale_factor, data_format="NCHW", name=None):
"""
This API implements pixel shuffle operation.
See more details in :ref:`api_nn_vision_PixelShuffle` .


Parameters:
x(Tensor): 4-D tensor, the data type should be float32 or float64.
upscale_factor(int): factor to increase spatial resolution.
data_format (str): The data format of the input and output data. An optional string from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
data_format (str, optional): The data format of the input and output data. An optional string from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
name (str, optional): The default value is None. Normally there is no need for user to set this property.

Returns:
Out(tensor): Reshaped tensor according to the new dimension.
Raises:
ValueError: If the square of upscale_factor cannot divide the channels of input.

Examples:
.. code-block:: python

import paddle
import paddle.nn.functional as F
import numpy as np
x = np.random.randn(2, 9, 4, 4).astype(np.float32)
x_var = paddle.to_tensor(x)
out_var = F.pixel_shuffle(x_var, 3)

x = paddle.randn(shape=[2,9,4,4])
out_var = F.pixel_shuffle(x, 3)
out = out_var.numpy()
print(out.shape)
# (2, 1, 12, 12)
"""
if not isinstance(upscale_factor, int):
Expand Down Expand Up @@ -363,7 +365,7 @@ def pixel_unshuffle(x, downscale_factor, data_format="NCHW", name=None):
Parameters:
x (Tensor): 4-D tensor, the data type should be float32 or float64.
downscale_factor (int): Factor to decrease spatial resolution.
data_format (str): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
data_format (str, optional): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
name (str, optional): Name for the operation (optional, default is None). Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.

Returns:
Expand All @@ -376,7 +378,8 @@ def pixel_unshuffle(x, downscale_factor, data_format="NCHW", name=None):
import paddle.nn.functional as F
x = paddle.randn([2, 1, 12, 12])
out = F.pixel_unshuffle(x, 3)
# out.shape = [2, 9, 4, 4]
print(out.shape)
# [2, 9, 4, 4]
"""
if len(x.shape) != 4:
raise ValueError(
Expand Down
25 changes: 11 additions & 14 deletions python/paddle/nn/layer/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,18 @@ class PReLU(Layer):
.. code-block:: python

import paddle
import numpy as np

paddle.set_default_dtype("float64")

data = 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]]]], 'float64')
x = paddle.to_tensor(data)
data = paddle.to_tensor([[[[-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.PReLU(1, 0.25)
out = m(x)
out = m(data)
print(out)
# [[[[-0.5 , 3. , -1. , 5. ],
# [ 3. , -1. , 5. , -1.5 ],
# [-1.75, -2. , 8. , 9. ]],
Expand Down Expand Up @@ -582,9 +581,8 @@ class ReLU6(Layer):
.. code-block:: python

import paddle
import numpy as np

x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
x = paddle.to_tensor([-1., 0.3, 6.5])
m = paddle.nn.ReLU6()
out = m(x) # [0, 0.3, 6]
"""
Expand Down Expand Up @@ -629,9 +627,8 @@ class SELU(Layer):
.. code-block:: python

import paddle
import numpy as np

x = paddle.to_tensor(np.array([[0.0, 1.0],[2.0, 3.0]]))
x = paddle.to_tensor([[0.0, 1.0],[2.0, 3.0]])
m = paddle.nn.SELU()
out = m(x) # [[0, 1.050701],[2.101402, 3.152103]]
"""
Expand Down
19 changes: 9 additions & 10 deletions python/paddle/nn/layer/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PixelShuffle(Layer):

PixelShuffle Layer

This operator rearranges elements in a tensor of shape [N, C, H, W]
Rearranges elements in a tensor of shape [N, C, H, W]
to a tensor of shape [N, C/upscale_factor**2, H*upscale_factor, W*upscale_factor],
or from shape [N, H, W, C] to [N, H*upscale_factor, W*upscale_factor, C/upscale_factor**2].
This is useful for implementing efficient sub-pixel convolution
Expand All @@ -37,7 +37,7 @@ class PixelShuffle(Layer):
Parameters:

upscale_factor(int): factor to increase spatial resolution.
data_format (str): The data format of the input and output data. An optional string from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
data_format (str, optional): The data format of the input and output data. An optional string from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Shape:
Expand All @@ -50,14 +50,12 @@ class PixelShuffle(Layer):

import paddle
import paddle.nn as nn
import numpy as np

x = np.random.randn(2, 9, 4, 4).astype(np.float32)
x_var = paddle.to_tensor(x)
x = paddle.randn(shape=[2,9,4,4])
pixel_shuffle = nn.PixelShuffle(3)
out_var = pixel_shuffle(x_var)
out_var = pixel_shuffle(x)
out = out_var.numpy()
print(out.shape)
print(out.shape)
# (2, 1, 12, 12)

"""
Expand Down Expand Up @@ -91,7 +89,7 @@ def extra_repr(self):

class PixelUnshuffle(Layer):
"""
This operator rearranges elements in a tensor of shape :math:`[N, C, H, W]`
Rearranges elements in a tensor of shape :math:`[N, C, H, W]`
to a tensor of shape :math:`[N, r^2C, H/r, W/r]`, or from shape
:math:`[N, H, W, C]` to :math:`[N, H/r, W/r, r^2C]`, where :math:`r` is the
downscale factor. This operation is the reversion of PixelShuffle operation.
Expand All @@ -101,7 +99,7 @@ class PixelUnshuffle(Layer):

Parameters:
downscale_factor (int): Factor to decrease spatial resolution.
data_format (str): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
data_format (str, optional): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
name (str, optional): Name for the operation (optional, default is None). Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.

Shape:
Expand All @@ -117,7 +115,8 @@ class PixelUnshuffle(Layer):
x = paddle.randn([2, 1, 12, 12])
pixel_unshuffle = nn.PixelUnshuffle(3)
out = pixel_unshuffle(x)
# out.shape = [2, 9, 4, 4]
print(out.shape)
# [2, 9, 4, 4]

"""

Expand Down