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_layers_dense.py 4.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 tensorlayer as tl
  7. from tests.utils import CustomTestCase
  8. import numpy as np
  9. class Layer_BinaryDense_Test(CustomTestCase):
  10. @classmethod
  11. def setUpClass(self):
  12. print("-" * 20, "Layer_BinaryDense_Test", "-" * 20)
  13. self.batch_size = 4
  14. self.inputs_shape = [self.batch_size, 10]
  15. self.ni = tl.layers.Input(self.inputs_shape, name='input_layer')
  16. self.layer1 = tl.layers.BinaryDense(n_units=5)
  17. self.layer2 = tl.layers.BinaryDense(n_units=5, in_channels=10)
  18. self.n1 = self.layer1(self.ni)
  19. self.n2 = self.layer2(self.ni)
  20. @classmethod
  21. def tearDownClass(cls):
  22. pass
  23. def test_layer_n1(self):
  24. print(self.n1[0])
  25. self.assertEqual(tl.ops.ReduceSum()(self.n1).numpy() % 1, 0.0) # should be integer
  26. def test_layer_n2(self):
  27. print(self.n2[0])
  28. self.assertEqual(tl.ops.ReduceSum()(self.n2).numpy() % 1, 0.0) # should be integer
  29. class Layer_DorefaDense_Test(CustomTestCase):
  30. @classmethod
  31. def setUpClass(self):
  32. print("-" * 20, "Layer_DorefaDense_Test", "-" * 20)
  33. self.batch_size = 4
  34. self.inputs_shape = [self.batch_size, 10]
  35. self.ni = tl.layers.Input(self.inputs_shape, name='input_layer')
  36. self.layer1 = tl.layers.DorefaDense(n_units=5)
  37. self.layer2 = tl.layers.DorefaDense(n_units=5, in_channels=10)
  38. self.n1 = self.layer1(self.ni)
  39. self.n2 = self.layer2(self.ni)
  40. @classmethod
  41. def tearDownClass(cls):
  42. pass
  43. def test_layer_n1(self):
  44. print(self.n1[0])
  45. def test_layer_n2(self):
  46. print(self.n2[0])
  47. class Layer_DropconnectDense_Test(CustomTestCase):
  48. @classmethod
  49. def setUpClass(self):
  50. print("-" * 20, "Layer_DropconnectDense_Test", "-" * 20)
  51. self.batch_size = 4
  52. self.inputs_shape = [self.batch_size, 10]
  53. self.ni = tl.layers.Input(self.inputs_shape, name='input_layer')
  54. self.layer1 = tl.layers.DropconnectDense(n_units=5, keep=1.0)
  55. self.layer2 = tl.layers.DropconnectDense(n_units=5, in_channels=10, keep=0.01)
  56. self.n1 = self.layer1(self.ni)
  57. self.n2 = self.layer2(self.ni)
  58. @classmethod
  59. def tearDownClass(cls):
  60. pass
  61. def test_layer_n1(self):
  62. print(self.n1[0])
  63. def test_layer_n2(self):
  64. print(self.n2[0])
  65. class Layer_QuanDense_Test(CustomTestCase):
  66. @classmethod
  67. def setUpClass(self):
  68. print("-" * 20, "Layer_QuanDense_Test", "-" * 20)
  69. self.batch_size = 4
  70. self.inputs_shape = [self.batch_size, 10]
  71. self.ni = tl.layers.Input(self.inputs_shape, name='input_layer')
  72. self.layer1 = tl.layers.QuanDense(n_units=5)
  73. self.layer2 = tl.layers.QuanDense(n_units=5, in_channels=10)
  74. self.n1 = self.layer1(self.ni)
  75. self.n2 = self.layer2(self.ni)
  76. @classmethod
  77. def tearDownClass(cls):
  78. pass
  79. def test_layer_n1(self):
  80. print(self.n1[0])
  81. def test_layer_n2(self):
  82. print(self.n2[0])
  83. class Layer_QuanDenseWithBN_Test(CustomTestCase):
  84. @classmethod
  85. def setUpClass(self):
  86. print("-" * 20, "Layer_QuanDenseWithBN_Test", "-" * 20)
  87. self.batch_size = 4
  88. self.inputs_shape = [self.batch_size, 10]
  89. self.inputs = tl.initializers.TruncatedNormal()(shape=self.inputs_shape)
  90. self.layer1 = tl.layers.QuanDenseWithBN(n_units=5)
  91. self.layer2 = tl.layers.QuanDenseWithBN(n_units=5, in_channels=10)
  92. self.n1 = self.layer1(self.inputs)
  93. self.n2 = self.layer2(self.inputs)
  94. @classmethod
  95. def tearDownClass(cls):
  96. pass
  97. def test_layer_n1(self):
  98. print(self.n1[0])
  99. def test_layer_n2(self):
  100. print(self.n2[0])
  101. class Layer_TernaryDense_Test(CustomTestCase):
  102. @classmethod
  103. def setUpClass(self):
  104. print("-" * 20, "Layer_BinaryDense_Test", "-" * 20)
  105. self.batch_size = 4
  106. self.inputs_shape = [self.batch_size, 10]
  107. self.inputs = tl.layers.Input(self.inputs_shape, name='input_layer')
  108. self.layer1 = tl.layers.TernaryDense(n_units=5)
  109. self.layer2 = tl.layers.TernaryDense(n_units=5, in_channels=10)
  110. self.n1 = self.layer1(self.inputs)
  111. self.n2 = self.layer2(self.inputs)
  112. @classmethod
  113. def tearDownClass(cls):
  114. pass
  115. def test_layer_n1(self):
  116. print(np.unique(self.n1.numpy().reshape(-1)))
  117. print(self.n1[0])
  118. def test_layer_n2(self):
  119. print(np.unique(self.n2.numpy().reshape(-1)))
  120. print(self.n2[0])
  121. if __name__ == '__main__':
  122. tl.logging.set_verbosity(tl.logging.DEBUG)
  123. unittest.main()

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