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_activations.py 3.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import unittest
  5. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  6. import tensorflow as tf
  7. import tensorlayer as tl
  8. from tests.utils import CustomTestCase
  9. class Test_Leaky_ReLUs(CustomTestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. cls.alpha = 0.2
  13. cls.vmin = 0
  14. cls.vmax = 10
  15. @classmethod
  16. def tearDownClass(cls):
  17. pass
  18. def test_lrelu(self):
  19. for i in range(-5, 15):
  20. if i > 0:
  21. good_output = i
  22. else:
  23. good_output = self.alpha * i
  24. computed_output = tl.act.leaky_relu(float(i), alpha=self.alpha)
  25. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  26. net = tl.layers.Input([10, 2])
  27. net = tl.layers.Dense(n_units=100, act=lambda x: tl.act.lrelu(x, 0.2), name='dense')(net)
  28. print(net)
  29. def test_lrelu6(self):
  30. for i in range(-5, 15):
  31. if i < 0:
  32. good_output = self.alpha * i
  33. else:
  34. good_output = min(6, i)
  35. computed_output = tl.act.leaky_relu6(float(i), alpha=self.alpha)
  36. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  37. net = tl.layers.Input([10, 2])
  38. net = tl.layers.Dense(n_units=100, act=lambda x: tl.act.leaky_relu6(x, 0.2), name='dense')(net)
  39. print(net)
  40. def test_ltrelu6(self):
  41. for i in range(-5, 15):
  42. if i < 0:
  43. good_output = self.alpha * i
  44. elif i < 6:
  45. good_output = i
  46. else:
  47. good_output = 6 + (self.alpha * (i - 6))
  48. computed_output = tl.act.leaky_twice_relu6(float(i), alpha_low=self.alpha, alpha_high=self.alpha)
  49. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  50. net = tl.layers.Input([10, 200])
  51. net = tl.layers.Dense(n_units=100, act=lambda x: tl.act.leaky_twice_relu6(x, 0.2, 0.2), name='dense')(net)
  52. print(net)
  53. def test_ramp(self):
  54. for i in range(-5, 15):
  55. if i < self.vmin:
  56. good_output = self.vmin
  57. elif i > self.vmax:
  58. good_output = self.vmax
  59. else:
  60. good_output = i
  61. computed_output = tl.act.ramp(float(i), v_min=self.vmin, v_max=self.vmax)
  62. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  63. def test_sign(self):
  64. for i in range(-5, 15):
  65. if i < 0:
  66. good_output = -1
  67. elif i == 0:
  68. good_output = 0
  69. else:
  70. good_output = 1
  71. computed_output = tl.act.sign(float(i))
  72. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  73. def test_swish(self):
  74. import numpy as np
  75. for i in range(-5, 15):
  76. good_output = i / (1 + np.math.exp(-i))
  77. computed_output = tl.act.swish(float(i))
  78. self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)
  79. if __name__ == '__main__':
  80. unittest.main()

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.