256 lines
8.5 KiB
Python
256 lines
8.5 KiB
Python
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""
|
|
激活函数层单元测试 / Activation Function Layer Unit Tests
|
|
|
|
测试目标 / Test Target:
|
|
paddle.nn 中的激活函数层 (各激活函数覆盖率~61-82%)
|
|
|
|
覆盖的模块 / Covered Modules:
|
|
- paddle.nn.ReLU, ReLU6, LeakyReLU: ReLU族激活函数
|
|
- paddle.nn.GELU: 高斯误差线性单元
|
|
- paddle.nn.Sigmoid, Tanh: 基础激活函数
|
|
- paddle.nn.Softmax, LogSoftmax: Softmax类
|
|
- paddle.nn.ELU, SELU, CELU: 指数线性单元族
|
|
- paddle.nn.Mish, Swish, Hardswish: 高级激活函数
|
|
- paddle.nn.Hardsigmoid, Hardtanh: 硬激活函数
|
|
|
|
作用 / Purpose:
|
|
覆盖各类激活函数层的正向传播代码路径,补充激活函数的边界情况测试。
|
|
"""
|
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
|
|
import paddle
|
|
from paddle import nn
|
|
|
|
paddle.disable_static()
|
|
|
|
|
|
class TestReLUFamily(unittest.TestCase):
|
|
"""测试ReLU族激活函数 / Test ReLU family activation functions"""
|
|
|
|
def test_relu(self):
|
|
"""测试ReLU激活函数 / Test ReLU"""
|
|
relu = nn.ReLU()
|
|
x = paddle.to_tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
|
|
y = relu(x)
|
|
expected = np.array([0.0, 0.0, 0.0, 1.0, 2.0])
|
|
np.testing.assert_allclose(y.numpy(), expected, rtol=1e-5)
|
|
|
|
def test_relu6(self):
|
|
"""测试ReLU6激活函数 / Test ReLU6"""
|
|
relu6 = nn.ReLU6()
|
|
x = paddle.to_tensor([-1.0, 0.0, 3.0, 6.0, 10.0])
|
|
y = relu6(x)
|
|
expected = np.array([0.0, 0.0, 3.0, 6.0, 6.0])
|
|
np.testing.assert_allclose(y.numpy(), expected, rtol=1e-5)
|
|
|
|
def test_leaky_relu(self):
|
|
"""测试LeakyReLU激活函数 / Test LeakyReLU"""
|
|
leaky_relu = nn.LeakyReLU(negative_slope=0.1)
|
|
x = paddle.to_tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
|
|
y = leaky_relu(x)
|
|
expected = np.array([-0.2, -0.1, 0.0, 1.0, 2.0])
|
|
np.testing.assert_allclose(y.numpy(), expected, rtol=1e-5)
|
|
|
|
def test_prelu(self):
|
|
"""测试PReLU激活函数 / Test PReLU"""
|
|
prelu = nn.PReLU(num_parameters=1, init=0.25)
|
|
x = paddle.randn([4, 10])
|
|
y = prelu(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
def test_rrelu(self):
|
|
"""测试RReLU激活函数 / Test RReLU"""
|
|
rrelu = nn.RReLU(lower=0.1, upper=0.3)
|
|
x = paddle.randn([4, 10])
|
|
y = rrelu(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
|
|
class TestGELUSilu(unittest.TestCase):
|
|
"""测试GELU和Silu激活函数 / Test GELU and Silu"""
|
|
|
|
def test_gelu(self):
|
|
"""测试GELU激活函数 / Test GELU"""
|
|
gelu = nn.GELU()
|
|
x = paddle.to_tensor([0.0, 1.0, -1.0])
|
|
y = gelu(x)
|
|
self.assertEqual(y.shape, [3])
|
|
|
|
def test_gelu_approximate(self):
|
|
"""测试近似GELU / Test approximate GELU"""
|
|
gelu = nn.GELU(approximate=True)
|
|
x = paddle.randn([4, 10])
|
|
y = gelu(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
def test_silu(self):
|
|
"""测试Silu激活函数 / Test Silu (Swish)"""
|
|
silu = nn.Silu()
|
|
x = paddle.randn([4, 10])
|
|
y = silu(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
|
|
class TestSigmoidTanh(unittest.TestCase):
|
|
"""测试Sigmoid和Tanh激活函数 / Test Sigmoid and Tanh"""
|
|
|
|
def test_sigmoid(self):
|
|
"""测试Sigmoid / Test Sigmoid"""
|
|
sigmoid = nn.Sigmoid()
|
|
x = paddle.to_tensor([0.0, 1.0, -1.0])
|
|
y = sigmoid(x)
|
|
self.assertTrue(paddle.all(y > 0).item())
|
|
self.assertTrue(paddle.all(y < 1).item())
|
|
self.assertAlmostEqual(float(y[0].numpy()), 0.5, places=5)
|
|
|
|
def test_tanh(self):
|
|
"""测试Tanh / Test Tanh"""
|
|
tanh = nn.Tanh()
|
|
x = paddle.to_tensor([0.0, 1.0, -1.0])
|
|
y = tanh(x)
|
|
self.assertTrue(paddle.all(y > -1).item())
|
|
self.assertTrue(paddle.all(y < 1).item())
|
|
self.assertAlmostEqual(float(y[0].numpy()), 0.0, places=5)
|
|
|
|
def test_hardsigmoid(self):
|
|
"""测试Hardsigmoid / Test Hardsigmoid"""
|
|
hardsigmoid = nn.Hardsigmoid()
|
|
x = paddle.to_tensor([-4.0, 0.0, 4.0])
|
|
y = hardsigmoid(x)
|
|
self.assertAlmostEqual(float(y[0].numpy()), 0.0, places=5)
|
|
self.assertAlmostEqual(float(y[2].numpy()), 1.0, places=5)
|
|
|
|
def test_hardtanh(self):
|
|
"""测试Hardtanh / Test Hardtanh"""
|
|
hardtanh = nn.Hardtanh(min=-1.0, max=1.0)
|
|
x = paddle.to_tensor([-2.0, 0.0, 2.0])
|
|
y = hardtanh(x)
|
|
expected = np.array([-1.0, 0.0, 1.0])
|
|
np.testing.assert_allclose(y.numpy(), expected, rtol=1e-5)
|
|
|
|
|
|
class TestSoftmaxFamily(unittest.TestCase):
|
|
"""测试Softmax族激活函数 / Test Softmax family"""
|
|
|
|
def test_softmax(self):
|
|
"""测试Softmax / Test Softmax"""
|
|
softmax = nn.Softmax(axis=-1)
|
|
x = paddle.to_tensor([[1.0, 2.0, 3.0]])
|
|
y = softmax(x)
|
|
self.assertAlmostEqual(float(y.sum().numpy()), 1.0, places=5)
|
|
|
|
def test_log_softmax(self):
|
|
"""测试LogSoftmax / Test LogSoftmax"""
|
|
log_softmax = nn.LogSoftmax(axis=-1)
|
|
x = paddle.randn([4, 5])
|
|
y = log_softmax(x)
|
|
# LogSoftmax values should be <= 0
|
|
self.assertTrue(paddle.all(y <= 0).item())
|
|
|
|
def test_softmax_2d(self):
|
|
"""测试2D Softmax / Test 2D Softmax"""
|
|
softmax = nn.Softmax(axis=1)
|
|
x = paddle.randn([4, 5, 3])
|
|
y = softmax(x)
|
|
# Sum along axis 1 should be 1
|
|
sums = y.sum(axis=1)
|
|
np.testing.assert_allclose(
|
|
sums.numpy(), np.ones([4, 3], dtype='float32'), rtol=1e-5
|
|
)
|
|
|
|
|
|
class TestELUFamily(unittest.TestCase):
|
|
"""测试ELU族激活函数 / Test ELU family"""
|
|
|
|
def test_elu(self):
|
|
"""测试ELU / Test ELU"""
|
|
elu = nn.ELU(alpha=1.0)
|
|
x = paddle.to_tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
|
|
y = elu(x)
|
|
# Positive values unchanged, negative values: alpha*(exp(x)-1)
|
|
self.assertAlmostEqual(float(y[3].numpy()), 1.0, places=5)
|
|
self.assertAlmostEqual(float(y[4].numpy()), 2.0, places=5)
|
|
|
|
def test_selu(self):
|
|
"""测试SELU / Test SELU"""
|
|
selu = nn.SELU()
|
|
x = paddle.randn([4, 10])
|
|
y = selu(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
def test_celu(self):
|
|
"""测试CELU / Test CELU"""
|
|
celu = nn.CELU(alpha=1.0)
|
|
x = paddle.to_tensor([-2.0, 0.0, 2.0])
|
|
y = celu(x)
|
|
self.assertEqual(y.shape, [3])
|
|
|
|
|
|
class TestMishHardswish(unittest.TestCase):
|
|
"""测试Mish和Hardswish激活函数 / Test Mish and Hardswish"""
|
|
|
|
def test_mish(self):
|
|
"""测试Mish激活函数 / Test Mish"""
|
|
mish = nn.Mish()
|
|
x = paddle.randn([4, 10])
|
|
y = mish(x)
|
|
self.assertEqual(y.shape, [4, 10])
|
|
|
|
def test_hardswish(self):
|
|
"""测试Hardswish激活函数 / Test Hardswish"""
|
|
hardswish = nn.Hardswish()
|
|
x = paddle.to_tensor([-4.0, -2.0, 0.0, 2.0, 4.0])
|
|
y = hardswish(x)
|
|
self.assertEqual(y.shape, [5])
|
|
# At x=-4: y=0, at x=4: y=4
|
|
self.assertAlmostEqual(float(y[0].numpy()), 0.0, places=5)
|
|
self.assertAlmostEqual(float(y[4].numpy()), 4.0, places=5)
|
|
|
|
def test_softplus(self):
|
|
"""测试Softplus激活函数 / Test Softplus"""
|
|
softplus = nn.Softplus(beta=1, threshold=20)
|
|
x = paddle.randn([4, 10])
|
|
y = softplus(x)
|
|
# Softplus always > 0
|
|
self.assertTrue(paddle.all(y > 0).item())
|
|
|
|
def test_softsign(self):
|
|
"""测试Softsign激活函数 / Test Softsign"""
|
|
softsign = nn.Softsign()
|
|
x = paddle.to_tensor([-1.0, 0.0, 1.0])
|
|
y = softsign(x)
|
|
# softsign(x) = x / (1 + |x|)
|
|
self.assertAlmostEqual(float(y[1].numpy()), 0.0, places=5)
|
|
self.assertAlmostEqual(float(y[2].numpy()), 0.5, places=5)
|
|
|
|
def test_activation_gradient(self):
|
|
"""测试激活函数梯度 / Test activation gradient"""
|
|
relu = nn.ReLU()
|
|
x = paddle.to_tensor([1.0, -1.0, 2.0])
|
|
x.stop_gradient = False
|
|
y = relu(x)
|
|
y.sum().backward()
|
|
expected = np.array([1.0, 0.0, 1.0])
|
|
np.testing.assert_allclose(x.grad.numpy(), expected)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|