Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion python/paddle/distributed/fleet/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,6 @@ def amp_init(
Examples:
.. code-block:: python

import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_static()
Expand Down
1 change: 0 additions & 1 deletion python/paddle/incubate/nn/functional/fused_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,6 @@ def fused_multi_transformer(
# required: gpu
import paddle
import paddle.incubate.nn.functional as F
import numpy as np

# input: [batch_size, seq_len, embed_dim]
x = paddle.rand(shape=(2, 4, 128), dtype="float32")
Expand Down
7 changes: 3 additions & 4 deletions python/paddle/incubate/optimizer/lookahead.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ def step(self):
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))
inp = paddle.rand([1,10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down Expand Up @@ -280,8 +279,8 @@ def minimize(
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))

inp = paddle.rand([1, 10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down
12 changes: 4 additions & 8 deletions python/paddle/incubate/optimizer/modelaverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ def minimize(
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))
inp = paddle.rand([1, 10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down Expand Up @@ -378,8 +377,7 @@ def step(self):
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))
inp = paddle.rand([1, 10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down Expand Up @@ -425,8 +423,7 @@ def apply(self, executor=None, need_restore=True):
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))
inp = paddle.rand([1, 10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down Expand Up @@ -500,8 +497,7 @@ def restore(self, executor=None):
.. code-block:: python

import paddle
import numpy as np
inp = paddle.to_tensor(np.random.random([1, 10]).astype('float32'))
inp = paddle.rand([1, 10], dtype="float32")
linear = paddle.nn.Linear(10, 1)
out = linear(inp)
loss = paddle.mean(out)
Expand Down
40 changes: 25 additions & 15 deletions python/paddle/nn/functional/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,12 @@ def softshrink(x, threshold=0.5, name=None):

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

x = paddle.to_tensor(np.array([-0.9, -0.2, 0.1, 0.8]))
out = F.softshrink(x) # [-0.4, 0, 0, 0.3]
x = paddle.to_tensor([-0.9, -0.2, 0.1, 0.8])
out = F.softshrink(x)
print(out)
# Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-0.39999998, 0. , 0. , 0.30000001])
"""
if threshold < 0:
raise ValueError(
Expand Down Expand Up @@ -1365,10 +1367,12 @@ def softsign(x, name=None):

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

x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.softsign(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = F.softsign(x)
print(out)
# Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-0.28571430, -0.16666666, 0.09090909, 0.23076925])
"""
if in_dygraph_mode():
return _C_ops.softsign(x)
Expand Down Expand Up @@ -1405,10 +1409,12 @@ def swish(x, name=None):

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

x = paddle.to_tensor(np.array([-2., 0., 1.]))
out = F.swish(x) # [-0.238406, 0., 0.731059]
x = paddle.to_tensor([-2., 0., 1.])
out = F.swish(x)
print(out)
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-0.23840584, 0. , 0.73105854])
"""
if in_dygraph_mode():
return _C_ops.swish(x, 1.0)
Expand Down Expand Up @@ -1487,10 +1493,12 @@ def tanhshrink(x, name=None):

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

x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.tanhshrink(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = F.tanhshrink(x)
print(out)
# Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-0.02005106, -0.00262468, 0.00033200, 0.00868741])
"""
if in_dygraph_mode():
return _C_ops.tanh_shrink(x)
Expand Down Expand Up @@ -1536,10 +1544,12 @@ def thresholded_relu(x, threshold=1.0, name=None):

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

x = paddle.to_tensor(np.array([2., 0., 1.]))
out = F.thresholded_relu(x) # [2., 0., 0.]
x = paddle.to_tensor([2., 0., 1.])
out = F.thresholded_relu(x)
print(out)
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [2., 0., 0.])
"""

if in_dygraph_mode():
Expand Down
16 changes: 7 additions & 9 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,18 +1965,16 @@ def label_smooth(label, prior_dist=None, epsilon=0.1, name=None):
.. code-block:: python

import paddle
import numpy as np

x_data = np.array([[[0, 1, 0],
[ 1, 0, 1]]]).astype("float32")
print(x_data.shape)
paddle.disable_static()
x = paddle.to_tensor(x_data, stop_gradient=False)

x = paddle.to_tensor([[[0, 1, 0],
[ 1, 0, 1]]], dtype="float32", stop_gradient=False)

output = paddle.nn.functional.label_smooth(x)
print(output)

#[[[0.03333334 0.93333334 0.03333334]
# [0.93333334 0.03333334 0.93333334]]]
# Tensor(shape=[1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=False,
# [[[0.03333334, 0.93333334, 0.03333334],
# [0.93333334, 0.03333334, 0.93333334]]])
"""
if epsilon > 1.0 or epsilon < 0.0:
raise ValueError("The value of epsilon must be between 0 and 1.")
Expand Down
58 changes: 25 additions & 33 deletions python/paddle/nn/functional/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,22 @@ def conv1d(

import paddle
import paddle.nn.functional as F
import numpy as np
x = np.array([[[4, 8, 1, 9],
[7, 2, 0, 9],
[6, 9, 2, 6]]]).astype(np.float32)
w=np.array(
[[[9, 3, 4],
[0, 0, 7],
[2, 5, 6]],
[[0, 3, 4],
[2, 9, 7],
[5, 6, 8]]]).astype(np.float32)

x_var = paddle.to_tensor(x)
w_var = paddle.to_tensor(w)
y_var = F.conv1d(x_var, w_var)
y_np = y_var.numpy()
print(y_np)

# [[[133. 238.]
# [160. 211.]]]
x = paddle.to_tensor([[[4, 8, 1, 9],
[7, 2, 0, 9],
[6, 9, 2, 6]]], dtype="float32")
w = paddle.to_tensor([[[9, 3, 4],
[0, 0, 7],
[2, 5, 6]],
[[0, 3, 4],
[2, 9, 7],
[5, 6, 8]]], dtype="float32")

y = F.conv1d(x, w)
print(y)
# Tensor(shape=[1, 2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[[133., 238.],
# [160., 211.]]])
"""
cudnn_version = get_cudnn_version()
if cudnn_version is not None:
Expand Down Expand Up @@ -949,24 +945,20 @@ def conv1d_transpose(
Examples:
.. code-block:: python



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


# shape: (1, 2, 4)
x=np.array([[[4, 0, 9, 7],
[8, 0, 9, 2,]]]).astype(np.float32)
x = paddle.to_tensor([[[4, 0, 9, 7],
[8, 0, 9, 2,]]], dtype="float32")
# shape: (2, 1, 2)
w=np.array([[[7, 0]],
[[4, 2]]]).astype(np.float32)
x_var = paddle.to_tensor(x)
w_var = paddle.to_tensor(w)
y_var = F.conv1d_transpose(x_var, w_var)
print(y_var)

# [[[60. 16. 99. 75. 4.]]]
w = paddle.to_tensor([[[7, 0]],
[[4, 2]]], dtype="float32")

y = F.conv1d_transpose(x, w)
print(y)
# Tensor(shape=[1, 1, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[[60., 16., 99., 75., 4. ]]])
"""
cudnn_version = get_cudnn_version()
if cudnn_version is not None:
Expand Down
76 changes: 39 additions & 37 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,14 @@ def fluid_softmax_with_cross_entropy(
.. code-block:: python

import paddle
import numpy as np

data = np.random.rand(128).astype("float32")
label = np.random.rand(1).astype("int64")
data = paddle.to_tensor(data)
label = paddle.to_tensor(label)
linear = paddle.nn.Linear(128, 100)
x = linear(data)
out = paddle.nn.functional.softmax_with_cross_entropy(logits=x, label=label)
logits = paddle.to_tensor([0.4, 0.6, 0.9])
label = paddle.randint(high=2, shape=[1], dtype="int64")

out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label)
print(out)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [1.15328646])
"""
if _non_static_mode():
if core.is_compiled_with_npu():
Expand Down Expand Up @@ -1778,7 +1776,6 @@ def ctc_loss(

# declarative mode
import paddle.nn.functional as F
import numpy as np
import paddle

# length of the longest logit sequence
Expand All @@ -1790,8 +1787,7 @@ def ctc_loss(
# class num
class_num = 3

np.random.seed(1)
log_probs = np.array([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04],
log_probs = paddle.to_tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04],
[3.02332580e-01, 1.46755889e-01, 9.23385918e-02]],

[[1.86260208e-01, 3.45560730e-01, 3.96767467e-01],
Expand All @@ -1804,30 +1800,30 @@ def ctc_loss(
[9.68261600e-01, 3.13424170e-01, 6.92322612e-01]],

[[8.76389146e-01, 8.94606650e-01, 8.50442126e-02],
[3.90547849e-02, 1.69830427e-01, 8.78142476e-01]]]).astype("float32")
labels = np.array([[1, 2, 2],
[1, 2, 2]]).astype("int32")
input_lengths = np.array([5, 5]).astype("int64")
label_lengths = np.array([3, 3]).astype("int64")

log_probs = paddle.to_tensor(log_probs)
labels = paddle.to_tensor(labels)
input_lengths = paddle.to_tensor(input_lengths)
label_lengths = paddle.to_tensor(label_lengths)
[3.90547849e-02, 1.69830427e-01, 8.78142476e-01]]],
dtype="float32")
labels = paddle.to_tensor([[1, 2, 2],
[1, 2, 2]], dtype="int32")
input_lengths = paddle.to_tensor([5, 5], dtype="int64")
label_lengths = paddle.to_tensor([3, 3], dtype="int64")

loss = F.ctc_loss(log_probs, labels,
input_lengths,
label_lengths,
blank=0,
reduction='none')
print(loss) #[3.9179852 2.9076521]
print(loss)
# Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [3.91798496, 2.90765190])

loss = F.ctc_loss(log_probs, labels,
input_lengths,
label_lengths,
blank=0,
reduction='mean')
print(loss) #[1.1376063]
print(loss)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [1.13760614])

"""

Expand Down Expand Up @@ -2265,16 +2261,14 @@ def softmax_with_cross_entropy(
.. code-block:: python

import paddle
import numpy as np

data = np.random.rand(128).astype("float32")
label = np.random.rand(1).astype("int64")
data = paddle.to_tensor(data)
label = paddle.to_tensor(label)
linear = paddle.nn.Linear(128, 100)
x = linear(data)
out = paddle.nn.functional.softmax_with_cross_entropy(logits=x, label=label)
logits = paddle.to_tensor([0.4, 0.6, 0.9], dtype="float32")
label = paddle.to_tensor([1], dtype="int64")

out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label)
print(out)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [1.15328646])
"""
return fluid_softmax_with_cross_entropy(
logits,
Expand Down Expand Up @@ -3869,18 +3863,26 @@ def soft_margin_loss(input, label, reduction='mean', name=None):
.. code-block:: python

import paddle
import numpy as np

input = paddle.to_tensor([[0.5, 0.6, 0.7],[0.3, 0.5, 0.2]], 'float32')
label = paddle.to_tensor([[1.0, -1.0, 1.0],[-1.0, 1.0, 1.0]], 'float32')
output = paddle.nn.functional.soft_margin_loss(input, label)
print(output)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [0.64022040])

input = paddle.uniform(shape=(5, 5), dtype="float32", min=0.1, max=0.8)
label = paddle.randint(0, 2, shape=(5, 5), dtype="int64")
label[label==0]=-1

input_np = np.random.uniform(0.1, 0.8, size=(5, 5)).astype(np.float64)
label_np = np.random.randint(0, 2, size=(5, 5)).astype(np.int64)
label_np[label_np==0]=-1
input = paddle.to_tensor(input_np)
label = paddle.to_tensor(label_np)
output = paddle.nn.functional.soft_margin_loss(input, label, reduction='none')
print(output)
# Tensor(shape=[5, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[1.09917796, 0.52613139, 0.56263304, 0.82736146, 0.38776723],
# [1.07179427, 1.11924267, 0.49877715, 1.10026348, 0.46184641],
# [0.84367639, 0.74795729, 0.44629076, 0.55123353, 0.77659678],
# [0.39465919, 0.76651484, 0.54485321, 0.76609844, 0.77166790],
# [0.51283568, 0.84757161, 0.78913331, 1.05268764, 0.45318675]])
"""
if reduction not in ['sum', 'mean', 'none']:
raise ValueError(
Expand Down
Loading