You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_activation.py 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import numpy as np
  10. import pytest
  11. import megengine as mge
  12. from megengine.jit.tracing import set_symbolic_shape
  13. from megengine.module import LeakyReLU, PReLU
  14. def test_leaky_relu():
  15. data = np.array([-8, -12, 6, 10]).astype(np.float32)
  16. negative_slope = 0.1
  17. leaky_relu = LeakyReLU(negative_slope)
  18. output = leaky_relu(mge.tensor(data))
  19. np_output = np.maximum(0, data) + negative_slope * np.minimum(0, data)
  20. np.testing.assert_equal(output.numpy(), np_output)
  21. @pytest.mark.parametrize("shape", [(1, 64, 15, 15), (64,)])
  22. @pytest.mark.parametrize("use_symbolic", [False, True])
  23. def test_prelu(shape, use_symbolic):
  24. old_flag = set_symbolic_shape(use_symbolic)
  25. data = np.random.random(size=shape)
  26. num_channel = 1 if len(shape) == 1 else shape[1]
  27. prelu = PReLU(num_parameters=num_channel, init=0.25)
  28. output = prelu(mge.Tensor(data))
  29. np_output = np.maximum(data, 0) + prelu.weight.numpy() * np.minimum(data, 0)
  30. set_symbolic_shape(old_flag)
  31. np.testing.assert_allclose(output.numpy(), np_output, atol=1e-5)