activation_test.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import math
  18. import unittest
  19. import numpy as np
  20. from federatedml.optim import activation
  21. class TestConvergeFunction(unittest.TestCase):
  22. def test_numeric_stability(self):
  23. x_list = np.linspace(-709, 709, 10000)
  24. # Original function
  25. # a = 1. / (1. + np.exp(-x))
  26. for x in x_list:
  27. a1 = 1. / (1. + np.exp(-x))
  28. a2 = activation.sigmoid(x)
  29. self.assertTrue(np.abs(a1 - a2) < 1e-5)
  30. if __name__ == '__main__':
  31. unittest.main()