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_array_ops.py 6.1 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. import numpy as np
  9. from tests.utils import CustomTestCase
  10. class Array_Op_Alphas_Test(CustomTestCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. b1 = tl.alphas([4, 3, 2, 1], 0.5431)
  14. b2 = tl.alphas([4, 3, 2], 5)
  15. b3 = tl.alphas([1, 2, 3, 4], -5)
  16. b4 = tl.alphas([2, 3, 4], True)
  17. with tf.Session() as sess:
  18. cls._b1, cls._b2, cls._b3, cls._b4 = sess.run([b1, b2, b3, b4])
  19. @classmethod
  20. def tearDownClass(cls):
  21. tf.reset_default_graph()
  22. def test_b1(self):
  23. self.assertEqual(self._b1.shape, (4, 3, 2, 1))
  24. b1 = np.array(
  25. [
  26. [
  27. [
  28. [0.5431],
  29. [0.5431],
  30. ],
  31. [
  32. [0.5431],
  33. [0.5431],
  34. ],
  35. [
  36. [0.5431],
  37. [0.5431],
  38. ],
  39. ], [
  40. [
  41. [0.5431],
  42. [0.5431],
  43. ],
  44. [
  45. [0.5431],
  46. [0.5431],
  47. ],
  48. [
  49. [0.5431],
  50. [0.5431],
  51. ],
  52. ], [
  53. [
  54. [0.5431],
  55. [0.5431],
  56. ],
  57. [
  58. [0.5431],
  59. [0.5431],
  60. ],
  61. [
  62. [0.5431],
  63. [0.5431],
  64. ],
  65. ], [
  66. [
  67. [0.5431],
  68. [0.5431],
  69. ],
  70. [
  71. [0.5431],
  72. [0.5431],
  73. ],
  74. [
  75. [0.5431],
  76. [0.5431],
  77. ],
  78. ]
  79. ]
  80. )
  81. np.array_equal(self._b1, b1)
  82. def test_b2(self):
  83. self.assertEqual(self._b2.shape, (4, 3, 2))
  84. b2 = np.array(
  85. [
  86. [
  87. [
  88. 5,
  89. 5,
  90. ],
  91. [
  92. 5,
  93. 5,
  94. ],
  95. [
  96. 5,
  97. 5,
  98. ],
  99. ], [
  100. [
  101. 5,
  102. 5,
  103. ],
  104. [
  105. 5,
  106. 5,
  107. ],
  108. [
  109. 5,
  110. 5,
  111. ],
  112. ], [
  113. [
  114. 5,
  115. 5,
  116. ],
  117. [
  118. 5,
  119. 5,
  120. ],
  121. [
  122. 5,
  123. 5,
  124. ],
  125. ], [
  126. [
  127. 5,
  128. 5,
  129. ],
  130. [
  131. 5,
  132. 5,
  133. ],
  134. [
  135. 5,
  136. 5,
  137. ],
  138. ]
  139. ]
  140. )
  141. np.array_equal(self._b2, b2)
  142. def test_b3(self):
  143. self.assertEqual(self._b3.shape, (1, 2, 3, 4))
  144. b3 = np.array(
  145. [
  146. [
  147. [[-5, -5, -5, -5], [-5, -5, -5, -5], [-5, -5, -5, -5]],
  148. [[-5, -5, -5, -5], [-5, -5, -5, -5], [-5, -5, -5, -5]],
  149. ]
  150. ]
  151. )
  152. np.array_equal(self._b3, b3)
  153. def test_b4(self):
  154. self.assertEqual(self._b4.shape, (2, 3, 4))
  155. b4 = np.array(
  156. [
  157. [[True, True, True, True], [True, True, True, True], [True, True, True, True]],
  158. [[True, True, True, True], [True, True, True, True], [True, True, True, True]],
  159. ]
  160. )
  161. np.array_equal(self._b4, b4)
  162. class Array_Op_Alphas_Like_Test(CustomTestCase):
  163. @classmethod
  164. def setUpClass(cls):
  165. a = tf.constant([[[4, 5, 6], [1, 2, 3]], [[4, 5, 6], [1, 2, 3]]])
  166. b1 = tl.alphas_like(a, 0.5431)
  167. b2 = tl.alphas_like(a, 5)
  168. b3 = tl.alphas_like(a, -5)
  169. b4 = tl.alphas_like(a, True)
  170. with tf.Session() as sess:
  171. cls._b1, cls._b2, cls._b3, cls._b4 = sess.run([b1, b2, b3, b4])
  172. @classmethod
  173. def tearDownClass(cls):
  174. tf.reset_default_graph()
  175. def test_b1(self):
  176. self.assertEqual(self._b1.shape, (2, 2, 3))
  177. b1 = np.array(
  178. [
  179. [[0.5431, 0.5431, 0.5431], [0.5431, 0.5431, 0.5431]],
  180. [[0.5431, 0.5431, 0.5431], [0.5431, 0.5431, 0.5431]]
  181. ]
  182. )
  183. np.array_equal(self._b1, b1)
  184. def test_b2(self):
  185. self.assertEqual(self._b2.shape, (2, 2, 3))
  186. b2 = np.array([[[5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5]]])
  187. np.array_equal(self._b2, b2)
  188. def test_b3(self):
  189. self.assertEqual(self._b3.shape, (2, 2, 3))
  190. b3 = np.array([[[-5, -5, -5], [-5, -5, -5]], [[-5, -5, -5], [-5, -5, -5]]])
  191. np.array_equal(self._b3, b3)
  192. def test_b4(self):
  193. self.assertEqual(self._b4.shape, (2, 2, 3))
  194. b4 = np.array([[[True, True, True], [True, True, True]], [[True, True, True], [True, True, True]]])
  195. np.array_equal(self._b4, b4)
  196. if __name__ == '__main__':
  197. tf.logging.set_verbosity(tf.logging.DEBUG)
  198. tl.logging.set_verbosity(tl.logging.DEBUG)
  199. unittest.main()

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