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.1 kB

12345678910111213141516171819202122232425262728293031323334
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. import megengine as mge
  5. from megengine.jit.tracing import set_symbolic_shape
  6. from megengine.module import LeakyReLU, PReLU
  7. def test_leaky_relu():
  8. data = np.array([-8, -12, 6, 10]).astype(np.float32)
  9. negative_slope = 0.1
  10. leaky_relu = LeakyReLU(negative_slope)
  11. output = leaky_relu(mge.tensor(data))
  12. np_output = np.maximum(0, data) + negative_slope * np.minimum(0, data)
  13. np.testing.assert_equal(output.numpy(), np_output)
  14. @pytest.mark.parametrize("shape", [(1, 64, 15, 15), (64,)])
  15. @pytest.mark.parametrize("use_symbolic", [False, True])
  16. def test_prelu(shape, use_symbolic):
  17. old_flag = set_symbolic_shape(use_symbolic)
  18. data = np.random.random(size=shape)
  19. num_channel = 1 if len(shape) == 1 else shape[1]
  20. prelu = PReLU(num_parameters=num_channel, init=0.25)
  21. output = prelu(mge.Tensor(data))
  22. np_output = np.maximum(data, 0) + prelu.weight.numpy() * np.minimum(data, 0)
  23. set_symbolic_shape(old_flag)
  24. np.testing.assert_allclose(output.numpy(), np_output, atol=1e-5)