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.

nn_test.py 47 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for miscellaneous functionality in tensorflow.ops.nn."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import math
  20. from absl.testing import parameterized
  21. import numpy as np
  22. from six.moves import xrange # pylint: disable=redefined-builtin
  23. from tensorflow.python.framework import constant_op
  24. from tensorflow.python.framework import dtypes
  25. from tensorflow.python.framework import ops
  26. from tensorflow.python.framework import test_util
  27. from tensorflow.python.ops import array_ops
  28. from tensorflow.python.ops import gradient_checker
  29. from tensorflow.python.ops import math_ops
  30. from tensorflow.python.ops import nn_impl
  31. from tensorflow.python.ops import nn_ops
  32. from tensorflow.python.ops import partitioned_variables
  33. from tensorflow.python.ops import variable_scope
  34. from tensorflow.python.ops import variables
  35. import tensorflow.python.ops.nn_grad # pylint: disable=unused-import
  36. from tensorflow.python.ops.nn_impl import _compute_sampled_logits
  37. from tensorflow.python.platform import test as test_lib
  38. class ZeroFractionTest(test_lib.TestCase):
  39. def _ZeroFraction(self, x):
  40. assert x.shape
  41. total_elements = np.prod(x.shape)
  42. nonzeros = np.count_nonzero(x.flatten())
  43. return 1.0 - nonzeros / total_elements
  44. @test_util.run_deprecated_v1
  45. def testZeroFraction(self):
  46. x_shape = [5, 17]
  47. x_np = np.random.randint(0, 2, size=x_shape).astype(np.float32)
  48. y_np = self._ZeroFraction(x_np)
  49. x_tf = constant_op.constant(x_np)
  50. x_tf.set_shape(x_shape)
  51. y_tf = nn_impl.zero_fraction(x_tf)
  52. y_tf_np = self.evaluate(y_tf)
  53. eps = 1e-8
  54. self.assertAllClose(y_tf_np, y_np, eps)
  55. @test_util.run_deprecated_v1
  56. def testZeroFractionEmpty(self):
  57. x = np.zeros(0)
  58. y = self.evaluate(nn_impl.zero_fraction(x))
  59. self.assertTrue(np.isnan(y))
  60. @test_util.run_deprecated_v1
  61. def testZeroFraction2_27Zeros(self):
  62. sparsity = nn_impl.zero_fraction(
  63. array_ops.zeros([int(2**27 * 1.01)], dtype=dtypes.int8))
  64. self.assertAllClose(1.0, self.evaluate(sparsity))
  65. @test_util.run_deprecated_v1
  66. def testZeroFraction2_27Ones(self):
  67. sparsity = nn_impl.zero_fraction(
  68. array_ops.ones([int(2**27 * 1.01)], dtype=dtypes.int8))
  69. self.assertAllClose(0.0, self.evaluate(sparsity))
  70. @test_util.run_deprecated_v1
  71. def testUnknownSize(self):
  72. value = array_ops.placeholder(dtype=dtypes.float32)
  73. sparsity = nn_impl.zero_fraction(value)
  74. with self.cached_session() as sess:
  75. self.assertAllClose(
  76. 0.25,
  77. sess.run(sparsity, {value: [[0., 1.], [0.3, 2.]]}))
  78. class SoftmaxTest(test_lib.TestCase, parameterized.TestCase):
  79. def _softmax(self, x):
  80. assert len(x.shape) == 2
  81. m = x.max(1)[:, np.newaxis]
  82. u = np.exp(x - m)
  83. z = u.sum(1)[:, np.newaxis]
  84. return u / z
  85. @test_util.run_in_graph_and_eager_modes
  86. def testSoftmax(self):
  87. x_shape = [5, 10]
  88. x_np = np.random.randn(*x_shape).astype(np.float32)
  89. y_np = self._softmax(x_np)
  90. x_tf = constant_op.constant(x_np)
  91. y_tf = nn_ops.softmax_v2(x_tf)
  92. y_tf_last_dim = nn_ops.softmax_v2(x_tf, 1)
  93. y_tf_np = self.evaluate(y_tf)
  94. y_tf_last_dim_np = self.evaluate(y_tf_last_dim)
  95. eps = 1e-3
  96. self.assertAllClose(y_tf_np, y_np, eps)
  97. self.assertAllClose(y_tf_last_dim_np, y_np, eps)
  98. def testSoftmaxAxes(self):
  99. arr = np.linspace(0., 1, 12).reshape(3, 4)
  100. x_neg_axis = nn_ops.softmax_v2(arr, axis=-2)
  101. y_pos_axis = nn_ops.softmax_v2(arr, axis=0)
  102. z_gt_axis = nn_ops.softmax_v2(arr, axis=0)
  103. x_neg_axis_tf = self.evaluate(x_neg_axis)
  104. y_pos_axis_tf = self.evaluate(y_pos_axis)
  105. z_gt_axis_tf = self.evaluate(z_gt_axis)
  106. eps = 1e-3
  107. self.assertAllClose(x_neg_axis_tf, y_pos_axis_tf, eps)
  108. self.assertAllClose(y_pos_axis_tf, z_gt_axis_tf, eps)
  109. @parameterized.parameters(((5, 10),), ((2, 3, 4),))
  110. @test_util.run_deprecated_v1
  111. def testGradient(self, x_shape):
  112. x_np = np.random.randn(*x_shape).astype(np.float64)
  113. with self.cached_session():
  114. x_tf = constant_op.constant(x_np)
  115. y_tf = nn_ops.softmax_v2(x_tf)
  116. err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf,
  117. x_shape)
  118. eps = 2e-8
  119. self.assertLess(err, eps)
  120. class LogPoissonLossTest(test_lib.TestCase):
  121. def _log_poisson_loss(self, x, z, compute_full_loss=False):
  122. lpl = np.exp(x) - z * x
  123. if compute_full_loss:
  124. stirling_approx = z * np.log(z) - z + 0.5 * np.log(2. * np.pi * z)
  125. lpl += np.ma.masked_array(stirling_approx, mask=(z <= 1)).filled(0.)
  126. return lpl
  127. @test_util.run_in_graph_and_eager_modes
  128. def testLogPoissonLoss(self):
  129. x_shape = [5, 10]
  130. x_np = np.random.randn(*x_shape).astype(np.float32)
  131. z_np = np.random.randint(0, 5, size=x_shape).astype(np.float32)
  132. y_np = self._log_poisson_loss(x_np, z_np, compute_full_loss=False)
  133. y_np_stirling = self._log_poisson_loss(x_np, z_np, compute_full_loss=True)
  134. y_tf = nn_impl.log_poisson_loss(z_np, x_np, compute_full_loss=False)
  135. y_tf_stirling = nn_impl.log_poisson_loss(z_np, x_np, compute_full_loss=True)
  136. y_tf_np = self.evaluate(y_tf)
  137. y_tf_np_stirling = self.evaluate(y_tf_stirling)
  138. eps = 1e-3
  139. self.assertAllClose(y_tf_np, y_np, eps)
  140. self.assertAllClose(y_tf_np_stirling, y_np_stirling, eps)
  141. @test_util.run_deprecated_v1
  142. def testGradient(self):
  143. x_shape = [5, 10]
  144. x_np = np.random.randn(*x_shape).astype(np.float64)
  145. z_np = np.random.randint(0, 5, size=x_shape).astype(np.float64)
  146. with self.cached_session():
  147. x_tf = constant_op.constant(x_np)
  148. y_tf = nn_impl.log_poisson_loss(z_np, x_tf, compute_full_loss=False)
  149. y_tf_stirling = nn_impl.log_poisson_loss(
  150. z_np, x_tf, compute_full_loss=True)
  151. err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf,
  152. x_shape)
  153. err_stirling = gradient_checker.compute_gradient_error(
  154. x_tf, x_shape, y_tf_stirling, x_shape)
  155. eps = 1e-6
  156. self.assertLess(err, eps)
  157. self.assertLess(err_stirling, eps)
  158. class LogSoftmaxTest(test_lib.TestCase, parameterized.TestCase):
  159. def _log_softmax(self, x):
  160. assert len(x.shape) == 2
  161. m = x.max(1)[:, np.newaxis]
  162. u = x - m
  163. return u - np.log(np.sum(np.exp(u), 1, keepdims=True))
  164. @test_util.run_in_graph_and_eager_modes
  165. def testLogSoftmax(self):
  166. x_shape = [5, 10]
  167. x_np = np.random.randn(*x_shape).astype(np.float32)
  168. y_np = self._log_softmax(x_np)
  169. x_tf = constant_op.constant(x_np)
  170. y_tf = nn_ops.log_softmax_v2(x_tf)
  171. y_tf_np = self.evaluate(y_tf)
  172. eps = 1e-3
  173. self.assertAllClose(y_tf_np, y_np, eps)
  174. def testLogSoftmaxAxes(self):
  175. arr = np.linspace(0., 1, 12).reshape(3, 4)
  176. x_neg_axis = nn_ops.log_softmax_v2(arr, axis=-2)
  177. y_pos_axis = nn_ops.log_softmax_v2(arr, axis=0)
  178. z_gt_axis = nn_ops.log_softmax_v2(arr, axis=0)
  179. x_neg_axis_tf = self.evaluate(x_neg_axis)
  180. y_pos_axis_tf = self.evaluate(y_pos_axis)
  181. z_gt_axis_tf = self.evaluate(z_gt_axis)
  182. eps = 1e-3
  183. self.assertAllClose(x_neg_axis_tf, y_pos_axis_tf, eps)
  184. self.assertAllClose(y_pos_axis_tf, z_gt_axis_tf, eps)
  185. @parameterized.parameters(((5, 10),), ((2, 3, 4),))
  186. @test_util.run_deprecated_v1
  187. def testGradient(self, x_shape):
  188. x_np = np.random.randn(*x_shape).astype(np.float64)
  189. with self.cached_session():
  190. x_tf = constant_op.constant(x_np)
  191. y_tf = nn_ops.log_softmax_v2(x_tf)
  192. err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf,
  193. x_shape)
  194. eps = 1e-7
  195. self.assertLess(err, eps)
  196. class L2LossTest(test_lib.TestCase):
  197. @test_util.run_in_graph_and_eager_modes
  198. def testL2Loss(self):
  199. for dtype in [dtypes.float32, dtypes.float64]:
  200. x = constant_op.constant(
  201. [1.0, 0.0, 3.0, 2.0], shape=[2, 2], name="x", dtype=dtype)
  202. l2loss = nn_ops.l2_loss(x)
  203. value = self.evaluate(l2loss)
  204. self.assertAllClose(7.0, value)
  205. @test_util.run_deprecated_v1
  206. def testGradient(self):
  207. x_shape = [20, 7, 3]
  208. np.random.seed(1) # Make it reproducible.
  209. x_val = np.random.random_sample(x_shape).astype(np.float64)
  210. with self.cached_session():
  211. x = constant_op.constant(x_val, name="x")
  212. output = nn_ops.l2_loss(x)
  213. err = gradient_checker.compute_gradient_error(x, x_shape, output, [1])
  214. print("L2Loss gradient err = %g " % err)
  215. err_tolerance = 1e-10
  216. self.assertLess(err, err_tolerance)
  217. class L2NormalizeTest(test_lib.TestCase):
  218. def _l2Normalize(self, x, dim):
  219. if isinstance(dim, list):
  220. norm = np.linalg.norm(x, axis=tuple(dim))
  221. for d in dim:
  222. norm = np.expand_dims(norm, d)
  223. return x / norm
  224. else:
  225. norm = np.apply_along_axis(np.linalg.norm, dim, x)
  226. return x / np.expand_dims(norm, dim)
  227. @test_util.run_in_graph_and_eager_modes
  228. def testL2Normalize(self):
  229. x_shape = [20, 7, 3]
  230. np.random.seed(1)
  231. x_np = np.random.random_sample(x_shape).astype(np.float32)
  232. for dim in range(len(x_shape)):
  233. y_np = self._l2Normalize(x_np, dim)
  234. x_tf = constant_op.constant(x_np, name="x")
  235. y_tf = nn_impl.l2_normalize_v2(x_tf, dim)
  236. self.assertAllClose(y_np, self.evaluate(y_tf))
  237. @test_util.run_in_graph_and_eager_modes
  238. def testL2NormalizeDimArray(self):
  239. x_shape = [20, 7, 3]
  240. np.random.seed(1)
  241. x_np = np.random.random_sample(x_shape).astype(np.float32)
  242. dim = [1, 2]
  243. y_np = self._l2Normalize(x_np, dim)
  244. x_tf = constant_op.constant(x_np, name="x")
  245. y_tf = nn_impl.l2_normalize_v2(x_tf, dim)
  246. self.assertAllClose(y_np, self.evaluate(y_tf))
  247. @test_util.run_deprecated_v1
  248. def testL2NormalizeGradient(self):
  249. x_shape = [20, 7, 3]
  250. np.random.seed(1)
  251. x_np = np.random.random_sample(x_shape).astype(np.float64)
  252. for dim in range(len(x_shape)):
  253. with self.cached_session():
  254. x_tf = constant_op.constant(x_np, name="x")
  255. y_tf = nn_impl.l2_normalize_v2(x_tf, dim)
  256. err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf,
  257. x_shape)
  258. print("L2Normalize gradient err = %g " % err)
  259. self.assertLess(err, 1e-4)
  260. class DropoutTest(test_lib.TestCase):
  261. def testDropout(self):
  262. # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
  263. # that it is producing approximately the right number of ones over a large
  264. # number of samples, based on the keep probability.
  265. x_dim = 40
  266. y_dim = 30
  267. num_iter = 10
  268. for keep_prob in [0.1, 0.5, 0.8]:
  269. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  270. dropout = nn_ops.dropout(t, keep_prob)
  271. final_count = 0
  272. self.assertEqual([x_dim, y_dim], dropout.get_shape())
  273. for _ in xrange(0, num_iter):
  274. value = self.evaluate(dropout)
  275. final_count += np.count_nonzero(value)
  276. # Verifies that there are only two values: 0 and 1/keep_prob.
  277. sorted_value = np.unique(np.sort(value))
  278. self.assertEqual(0, sorted_value[0])
  279. self.assertAllClose(1 / keep_prob, sorted_value[1])
  280. # Check that we are in the 15% error range
  281. expected_count = x_dim * y_dim * keep_prob * num_iter
  282. rel_error = math.fabs(final_count - expected_count) / expected_count
  283. print(rel_error)
  284. self.assertTrue(rel_error < 0.15)
  285. def testShapedDropout(self):
  286. # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
  287. # that it is producing approximately the right number of ones over a large
  288. # number of samples, based on the keep probability. This time with shaped
  289. # noise.
  290. x_dim = 40 * 30
  291. y_dim = 3
  292. num_iter = 10
  293. for keep_prob in [0.1, 0.5, 0.8]:
  294. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  295. dropout = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim, 1])
  296. self.assertEqual([x_dim, y_dim], dropout.get_shape())
  297. final_count = 0
  298. for _ in xrange(0, num_iter):
  299. value = self.evaluate(dropout)
  300. final_count += np.count_nonzero(value)
  301. # Verifies that there are only two values: 0 and 1/keep_prob.
  302. sorted_value = np.unique(np.sort(value))
  303. self.assertEqual(0, sorted_value[0])
  304. self.assertAllClose(1 / keep_prob, sorted_value[1])
  305. # Check that we are in the 15% error range
  306. expected_count = x_dim * y_dim * keep_prob * num_iter
  307. rel_error = math.fabs(final_count - expected_count) / expected_count
  308. print(rel_error)
  309. self.assertTrue(rel_error < 0.15)
  310. def testShapedDropoutCorrelation(self):
  311. # Runs a shaped dropout and tests that the correlations are correct.
  312. x_dim = 40
  313. y_dim = 30
  314. num_iter = 10
  315. for keep_prob in [0.1, 0.5, 0.8]:
  316. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  317. dropout = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim, 1])
  318. self.assertEqual([x_dim, y_dim], dropout.get_shape())
  319. for _ in xrange(0, num_iter):
  320. value = self.evaluate(dropout)
  321. # Verifies that each y column as only one type of activation.
  322. for i in xrange(x_dim):
  323. sorted_value = np.unique(np.sort(value[i, :]))
  324. self.assertEqual(sorted_value.size, 1)
  325. @test_util.run_deprecated_v1
  326. def testDropoutPlaceholderKeepProb(self):
  327. # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
  328. # that it is producing approximately the right number of ones over a large
  329. # number of samples, based on the keep probability.
  330. x_dim = 40
  331. y_dim = 30
  332. num_iter = 10
  333. for keep_prob in [0.1, 0.5, 0.8]:
  334. with self.cached_session():
  335. t = constant_op.constant(
  336. 1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  337. keep_prob_placeholder = array_ops.placeholder(dtypes.float32)
  338. dropout = nn_ops.dropout(t, keep_prob_placeholder)
  339. final_count = 0
  340. self.assertEqual([x_dim, y_dim], dropout.get_shape())
  341. for _ in xrange(0, num_iter):
  342. value = dropout.eval(feed_dict={keep_prob_placeholder: keep_prob})
  343. final_count += np.count_nonzero(value)
  344. # Verifies that there are only two values: 0 and 1/keep_prob.
  345. sorted_value = np.unique(np.sort(value))
  346. self.assertEqual(0, sorted_value[0])
  347. self.assertAllClose(1 / keep_prob, sorted_value[1])
  348. # Check that we are in the 15% error range
  349. expected_count = x_dim * y_dim * keep_prob * num_iter
  350. rel_error = math.fabs(final_count - expected_count) / expected_count
  351. print(rel_error)
  352. self.assertTrue(rel_error < 0.15)
  353. @test_util.run_deprecated_v1
  354. def testShapedDropoutUnknownShape(self):
  355. x_dim = 40
  356. y_dim = 30
  357. keep_prob = 0.5
  358. x = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  359. dropout_x = nn_ops.dropout(
  360. x, keep_prob, noise_shape=array_ops.placeholder(dtypes.int32))
  361. self.assertEqual(x.get_shape(), dropout_x.get_shape())
  362. def testPartialShapedDropout(self):
  363. x_dim = 40 * 30
  364. y_dim = 3
  365. num_iter = 10
  366. for keep_prob in [0.1, 0.5, 0.8]:
  367. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  368. # Set noise_shape=[None, 1] which means [x_dim, 1].
  369. dropout = nn_ops.dropout(t, keep_prob, noise_shape=[None, 1])
  370. self.assertEqual([x_dim, y_dim], dropout.get_shape())
  371. final_count = 0
  372. for _ in xrange(0, num_iter):
  373. value = self.evaluate(dropout)
  374. final_count += np.count_nonzero(value)
  375. # Verifies that there are only two values: 0 and 1/keep_prob.
  376. sorted_value = np.unique(np.sort(value))
  377. self.assertEqual(0, sorted_value[0])
  378. self.assertAllClose(1 / keep_prob, sorted_value[1])
  379. # Check that we are in the 15% error range
  380. expected_count = x_dim * y_dim * keep_prob * num_iter
  381. rel_error = math.fabs(final_count - expected_count) / expected_count
  382. print(rel_error)
  383. self.assertTrue(rel_error < 0.15)
  384. @test_util.run_deprecated_v1
  385. def testInvalidKeepProb(self):
  386. x_dim = 40
  387. y_dim = 30
  388. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  389. with self.assertRaises(ValueError):
  390. nn_ops.dropout(t, -1.0)
  391. with self.assertRaises(ValueError):
  392. nn_ops.dropout(t, 1.1)
  393. with self.assertRaises(ValueError):
  394. nn_ops.dropout(t, [0.0, 1.0])
  395. with self.assertRaises(ValueError):
  396. nn_ops.dropout(t, array_ops.placeholder(dtypes.float64))
  397. with self.assertRaises(ValueError):
  398. nn_ops.dropout(t, array_ops.placeholder(dtypes.float32, shape=[2]))
  399. @test_util.run_deprecated_v1
  400. def testInvalidRate(self):
  401. x_dim = 40
  402. y_dim = 30
  403. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  404. with self.assertRaises(ValueError):
  405. nn_ops.dropout_v2(t, -1.0)
  406. with self.assertRaises(ValueError):
  407. nn_ops.dropout_v2(t, 1.1)
  408. with self.assertRaises(ValueError):
  409. nn_ops.dropout_v2(t, [0.0, 1.0])
  410. @test_util.run_deprecated_v1
  411. def testShapedDropoutShapeError(self):
  412. # Runs shaped dropout and verifies an error is thrown on misshapen noise.
  413. x_dim = 40
  414. y_dim = 30
  415. keep_prob = 0.5
  416. t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
  417. with self.assertRaises(ValueError):
  418. _ = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim, y_dim + 10])
  419. with self.assertRaises(ValueError):
  420. _ = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim, y_dim, 5])
  421. with self.assertRaises(ValueError):
  422. _ = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim + 3])
  423. with self.assertRaises(ValueError):
  424. _ = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim])
  425. # test that broadcasting proceeds
  426. _ = nn_ops.dropout(t, keep_prob, noise_shape=[y_dim])
  427. _ = nn_ops.dropout(t, keep_prob, noise_shape=[1, y_dim])
  428. _ = nn_ops.dropout(t, keep_prob, noise_shape=[x_dim, 1])
  429. _ = nn_ops.dropout(t, keep_prob, noise_shape=[1, 1])
  430. def testNoDropoutFast(self):
  431. x = array_ops.zeros((5,))
  432. y = nn_ops.dropout(x, keep_prob=1)
  433. self.assertTrue(x is y)
  434. y = nn_ops.dropout_v2(x, rate=0)
  435. self.assertTrue(x is y)
  436. def testDropoutWithIntegerInputs(self):
  437. x = constant_op.constant([1, 1, 1, 1, 1])
  438. with self.assertRaises(ValueError):
  439. _ = nn_ops.dropout(x, 0.5)
  440. class ComputeSampledLogitsTest(test_lib.TestCase):
  441. def setUp(self):
  442. self._eps = 1e-3
  443. def _GenerateTestData(self, num_classes, dim, batch_size, num_true, labels,
  444. sampled, subtract_log_q):
  445. """Randomly generates input/output data for a single test case.
  446. This function returns numpy constants for use in a test case.
  447. Args:
  448. num_classes: An int. The number of embedding classes in the test case.
  449. dim: An int. The dimension of the embedding.
  450. batch_size: An int. The batch size.
  451. num_true: An int. The number of target classes per training example.
  452. labels: A list of batch_size * num_true ints. The target classes.
  453. sampled: A list of indices in [0, num_classes).
  454. subtract_log_q: A bool corresponding to the parameter in
  455. _compute_sampled_logits().
  456. Returns:
  457. weights: Embedding weights to use as test input. It is a numpy array
  458. of shape [num_classes, dim]
  459. biases: Embedding biases to use as test input. It is a numpy array
  460. of shape [num_classes].
  461. hidden_acts: Forward activations of the network to use as test input.
  462. It is a numpy array of shape [batch_size, dim].
  463. sampled_vals: A tuple based on `sampled` to use as test input in the
  464. format returned by a *_candidate_sampler function.
  465. exp_logits: The output logits expected from _compute_sampled_logits().
  466. It is a numpy array of shape [batch_size, num_true + len(sampled)].
  467. exp_labels: The output labels expected from _compute_sampled_logits().
  468. It is a numpy array of shape [batch_size, num_true + len(sampled)].
  469. """
  470. weights = np.random.randn(num_classes, dim).astype(np.float32)
  471. biases = np.random.randn(num_classes).astype(np.float32)
  472. hidden_acts = np.random.randn(batch_size, dim).astype(np.float32)
  473. true_exp = np.full([batch_size, 1], fill_value=0.5, dtype=np.float32)
  474. sampled_exp = np.full([len(sampled)], fill_value=0.5, dtype=np.float32)
  475. sampled_vals = (sampled, true_exp, sampled_exp)
  476. sampled_w, sampled_b = weights[sampled], biases[sampled]
  477. true_w, true_b = weights[labels], biases[labels]
  478. true_logits = np.sum(
  479. hidden_acts.reshape((batch_size, 1, dim)) * true_w.reshape(
  480. (batch_size, num_true, dim)),
  481. axis=2)
  482. true_b = true_b.reshape((batch_size, num_true))
  483. true_logits += true_b
  484. sampled_logits = np.dot(hidden_acts, sampled_w.T) + sampled_b
  485. if subtract_log_q:
  486. true_logits -= np.log(true_exp)
  487. sampled_logits -= np.log(sampled_exp[np.newaxis, :])
  488. exp_logits = np.concatenate([true_logits, sampled_logits], axis=1)
  489. exp_labels = np.hstack((np.ones_like(true_logits) / num_true,
  490. np.zeros_like(sampled_logits)))
  491. return weights, biases, hidden_acts, sampled_vals, exp_logits, exp_labels
  492. def _ShardTestEmbeddings(self, weights, biases, num_shards):
  493. """Shards the weights and biases returned by _GenerateTestData.
  494. Args:
  495. weights: The weights returned by _GenerateTestData.
  496. biases: The biases returned by _GenerateTestData.
  497. num_shards: The number of shards to create.
  498. Returns:
  499. sharded_weights: A list of size `num_shards` containing all the weights.
  500. sharded_biases: A list of size `num_shards` containing all the biases.
  501. """
  502. with ops.Graph().as_default() as g:
  503. sharded_weights = variable_scope.get_variable(
  504. "w",
  505. partitioner=partitioned_variables.fixed_size_partitioner(num_shards),
  506. initializer=constant_op.constant(weights))
  507. sharded_biases = variable_scope.get_variable(
  508. "b",
  509. partitioner=partitioned_variables.fixed_size_partitioner(num_shards),
  510. initializer=constant_op.constant(biases))
  511. with self.session(graph=g) as sess:
  512. variables.global_variables_initializer().run()
  513. return self.evaluate([list(sharded_weights), list(sharded_biases)])
  514. def testShapes(self):
  515. np.random.seed(0)
  516. num_classes = 5
  517. batch_size = 3
  518. for num_true in range(1, 5):
  519. labels = np.random.randint(
  520. low=0, high=num_classes, size=batch_size * num_true)
  521. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  522. exp_labels) = self._GenerateTestData(
  523. num_classes=num_classes,
  524. dim=10,
  525. batch_size=batch_size,
  526. num_true=num_true,
  527. labels=labels,
  528. sampled=[1, 0, 2, 3],
  529. subtract_log_q=False)
  530. logits_tensor, labels_tensor = _compute_sampled_logits(
  531. weights=constant_op.constant(weights),
  532. biases=constant_op.constant(biases),
  533. labels=constant_op.constant(
  534. labels, dtype=dtypes.int64, shape=(batch_size, num_true)),
  535. inputs=constant_op.constant(hidden_acts),
  536. num_sampled=4,
  537. num_classes=num_classes,
  538. num_true=num_true,
  539. sampled_values=sampled_vals,
  540. subtract_log_q=False,
  541. remove_accidental_hits=False,
  542. partition_strategy="div",
  543. name="sampled_logits_basic_num_true_%d" % num_true)
  544. got_logits, got_labels = self.evaluate([logits_tensor, labels_tensor])
  545. self.assertEqual(exp_logits.shape, got_logits.shape, self._eps)
  546. self.assertEqual(exp_labels.shape, got_labels.shape, self._eps)
  547. def testBasic(self):
  548. """Without accidental hit removal or subtract_log_q."""
  549. np.random.seed(0)
  550. num_classes = 5
  551. batch_size = 3
  552. for num_true in range(1, 5):
  553. labels = np.random.randint(
  554. low=0, high=num_classes, size=batch_size * num_true)
  555. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  556. exp_labels) = self._GenerateTestData(
  557. num_classes=num_classes,
  558. dim=10,
  559. batch_size=batch_size,
  560. num_true=num_true,
  561. labels=labels,
  562. sampled=[1, 0, 2, 3],
  563. subtract_log_q=False)
  564. logits_tensor, labels_tensor = _compute_sampled_logits(
  565. weights=constant_op.constant(weights),
  566. biases=constant_op.constant(biases),
  567. labels=constant_op.constant(
  568. labels, dtype=dtypes.int64, shape=(batch_size, num_true)),
  569. inputs=constant_op.constant(hidden_acts),
  570. num_sampled=4,
  571. num_classes=num_classes,
  572. num_true=num_true,
  573. sampled_values=sampled_vals,
  574. subtract_log_q=False,
  575. remove_accidental_hits=False,
  576. partition_strategy="div",
  577. name="sampled_logits_basic_num_true_%d" % num_true)
  578. got_logits, got_labels = self.evaluate([logits_tensor, labels_tensor])
  579. self.assertAllClose(exp_logits, got_logits, self._eps)
  580. self.assertAllClose(exp_labels, got_labels, self._eps)
  581. def testAccidentalHitRemoval(self):
  582. """With accidental hit removal, no subtract_log_q."""
  583. np.random.seed(0)
  584. num_classes = 5
  585. batch_size = 3
  586. sampled = [1, 0, 2, 3]
  587. for num_true in range(1, 5):
  588. labels = np.random.randint(
  589. low=0, high=num_classes, size=batch_size * num_true)
  590. (weights, biases, hidden_acts, sampled_vals, _,
  591. _) = self._GenerateTestData(
  592. num_classes=num_classes,
  593. dim=10,
  594. batch_size=batch_size,
  595. num_true=num_true,
  596. labels=labels,
  597. sampled=sampled,
  598. subtract_log_q=False)
  599. logits_tensor, _ = _compute_sampled_logits(
  600. weights=constant_op.constant(weights),
  601. biases=constant_op.constant(biases),
  602. labels=constant_op.constant(
  603. labels, dtype=dtypes.int64, shape=(batch_size, num_true)),
  604. inputs=constant_op.constant(hidden_acts),
  605. num_sampled=len(sampled),
  606. num_classes=num_classes,
  607. num_true=num_true,
  608. sampled_values=sampled_vals,
  609. subtract_log_q=False,
  610. remove_accidental_hits=True,
  611. partition_strategy="div",
  612. name="sampled_logits_accidental_hit_removal_num_true_%d" % num_true)
  613. # Test that the exponentiated logits of accidental hits are near 0.
  614. # First we need to find the hits in this random test run:
  615. labels_reshape = labels.reshape((batch_size, num_true))
  616. got_logits = self.evaluate(logits_tensor)
  617. for row in xrange(batch_size):
  618. row_labels = labels_reshape[row, :]
  619. for col in xrange(len(sampled)):
  620. if sampled[col] in row_labels:
  621. # We need to add the num_true_test offset into logits_*
  622. self.assertNear(
  623. np.exp(got_logits[row, col + num_true]), 0., self._eps)
  624. def testSubtractLogQ(self):
  625. """With subtract_log_q, no accidental hit removal."""
  626. np.random.seed(0)
  627. num_classes = 5
  628. batch_size = 3
  629. for num_true in range(1, 5):
  630. labels = np.random.randint(
  631. low=0, high=num_classes, size=batch_size * num_true)
  632. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  633. exp_labels) = self._GenerateTestData(
  634. num_classes=num_classes,
  635. dim=10,
  636. batch_size=batch_size,
  637. num_true=num_true,
  638. labels=labels,
  639. sampled=[1, 0, 2, 3],
  640. subtract_log_q=True)
  641. logits_tensor, labels_tensor = _compute_sampled_logits(
  642. weights=constant_op.constant(weights),
  643. biases=constant_op.constant(biases),
  644. labels=constant_op.constant(
  645. labels, dtype=dtypes.int64, shape=(batch_size, num_true)),
  646. inputs=constant_op.constant(hidden_acts),
  647. num_sampled=4,
  648. num_classes=num_classes,
  649. num_true=num_true,
  650. sampled_values=sampled_vals,
  651. subtract_log_q=True,
  652. remove_accidental_hits=False,
  653. partition_strategy="div",
  654. name="sampled_logits_subtract_log_q_num_true_%d" % num_true)
  655. got_logits, got_labels = self.evaluate([logits_tensor, labels_tensor])
  656. self.assertAllClose(exp_logits, got_logits, self._eps)
  657. self.assertAllClose(exp_labels, got_labels, self._eps)
  658. def testSharded(self):
  659. """With sharded weights and sharded biases."""
  660. np.random.seed(0)
  661. num_classes = 5
  662. batch_size = 3
  663. for num_true in range(1, 5):
  664. labels = np.random.randint(
  665. low=0, high=num_classes, size=batch_size * num_true)
  666. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  667. exp_labels) = self._GenerateTestData(
  668. num_classes=num_classes,
  669. dim=10,
  670. batch_size=batch_size,
  671. num_true=num_true,
  672. labels=labels,
  673. sampled=[1, 0, 2, 3],
  674. subtract_log_q=False)
  675. weight_shards, bias_shards = self._ShardTestEmbeddings(
  676. weights, biases, num_shards=3)
  677. logits_tensor, labels_tensor = _compute_sampled_logits(
  678. weights=[constant_op.constant(shard) for shard in weight_shards],
  679. biases=[constant_op.constant(shard) for shard in bias_shards],
  680. labels=constant_op.constant(
  681. labels, dtype=dtypes.int64, shape=(batch_size, num_true)),
  682. inputs=constant_op.constant(hidden_acts),
  683. num_sampled=4,
  684. num_classes=num_classes,
  685. num_true=num_true,
  686. sampled_values=sampled_vals,
  687. subtract_log_q=False,
  688. remove_accidental_hits=False,
  689. partition_strategy="div",
  690. name="sampled_logits_sharded_num_true_%d" % num_true)
  691. got_logits, got_labels = self.evaluate([logits_tensor, labels_tensor])
  692. self.assertAllClose(exp_logits, got_logits, self._eps)
  693. self.assertAllClose(exp_labels, got_labels, self._eps)
  694. def testNCELoss(self):
  695. # A simple test to verify the numerics.
  696. def _SigmoidCrossEntropyWithLogits(logits, targets):
  697. # logits, targets: float arrays of the same shape.
  698. assert logits.shape == targets.shape
  699. pred = 1. / (1. + np.exp(-logits))
  700. eps = 0.0001
  701. pred = np.minimum(np.maximum(pred, eps), 1 - eps)
  702. return -targets * np.log(pred) - (1. - targets) * np.log(1. - pred)
  703. np.random.seed(0)
  704. num_classes = 5
  705. batch_size = 3
  706. labels = [0, 1, 2]
  707. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  708. exp_labels) = self._GenerateTestData(
  709. num_classes=num_classes,
  710. dim=10,
  711. batch_size=batch_size,
  712. num_true=1,
  713. labels=labels,
  714. sampled=[1, 0, 2, 3],
  715. subtract_log_q=True)
  716. exp_nce_loss = np.sum(
  717. _SigmoidCrossEntropyWithLogits(exp_logits, exp_labels), 1)
  718. got_nce_loss = nn_impl.nce_loss_v2(
  719. weights=constant_op.constant(weights),
  720. biases=constant_op.constant(biases),
  721. labels=constant_op.constant(labels, shape=(batch_size, 1)),
  722. inputs=constant_op.constant(hidden_acts),
  723. num_sampled=4,
  724. num_classes=num_classes,
  725. num_true=1,
  726. sampled_values=sampled_vals)
  727. self.assertAllClose(exp_nce_loss, self.evaluate(got_nce_loss), 1e-4)
  728. # Test with sharded weights and sharded biases.
  729. weight_shards, bias_shards = self._ShardTestEmbeddings(
  730. weights, biases, num_shards=3)
  731. got_nce_loss = nn_impl.nce_loss_v2(
  732. weights=[constant_op.constant(shard) for shard in weight_shards],
  733. biases=[constant_op.constant(shard) for shard in bias_shards],
  734. labels=constant_op.constant(labels, shape=(batch_size, 1)),
  735. inputs=constant_op.constant(hidden_acts),
  736. num_sampled=4,
  737. num_classes=num_classes,
  738. num_true=1,
  739. sampled_values=sampled_vals)
  740. self.assertAllClose(exp_nce_loss, self.evaluate(got_nce_loss), 1e-4)
  741. def testSampledSoftmaxLoss(self):
  742. # A simple test to verify the numerics.
  743. def _SoftmaxCrossEntropyWithLogits(logits, targets):
  744. # logits, targets: float arrays of the same shape.
  745. assert logits.shape == targets.shape
  746. stable_exp_logits = np.exp(
  747. logits - np.amax(logits, axis=1, keepdims=True))
  748. pred = stable_exp_logits / np.sum(stable_exp_logits, 1, keepdims=True)
  749. return -np.sum(targets * np.log(pred + 1.0e-20), axis=1)
  750. np.random.seed(0)
  751. num_classes = 5
  752. batch_size = 3
  753. labels = [0, 1, 2]
  754. (weights, biases, hidden_acts, sampled_vals, exp_logits,
  755. exp_labels) = self._GenerateTestData(
  756. num_classes=num_classes,
  757. dim=10,
  758. batch_size=batch_size,
  759. num_true=1,
  760. labels=labels,
  761. sampled=[1, 0, 2, 3],
  762. subtract_log_q=True)
  763. exp_sampled_softmax_loss = _SoftmaxCrossEntropyWithLogits(
  764. exp_logits, exp_labels)
  765. got_sampled_softmax_loss = nn_impl.sampled_softmax_loss_v2(
  766. weights=constant_op.constant(weights),
  767. biases=constant_op.constant(biases),
  768. labels=constant_op.constant(labels, shape=(batch_size, 1)),
  769. inputs=constant_op.constant(hidden_acts),
  770. num_sampled=4,
  771. num_classes=num_classes,
  772. num_true=1,
  773. sampled_values=sampled_vals,
  774. remove_accidental_hits=False)
  775. self.assertAllClose(exp_sampled_softmax_loss,
  776. self.evaluate(got_sampled_softmax_loss), 1e-4)
  777. # Test with sharded weights and sharded biases.
  778. weight_shards, bias_shards = self._ShardTestEmbeddings(
  779. weights, biases, num_shards=3)
  780. got_sampled_softmax_loss = nn_impl.sampled_softmax_loss_v2(
  781. weights=[constant_op.constant(shard) for shard in weight_shards],
  782. biases=[constant_op.constant(shard) for shard in bias_shards],
  783. labels=constant_op.constant(labels, shape=(batch_size, 1)),
  784. inputs=constant_op.constant(hidden_acts),
  785. num_sampled=4,
  786. num_classes=num_classes,
  787. num_true=1,
  788. sampled_values=sampled_vals,
  789. remove_accidental_hits=False)
  790. self.assertAllClose(exp_sampled_softmax_loss,
  791. self.evaluate(got_sampled_softmax_loss), 1e-4)
  792. def testSampledSoftmaxLossBf16(self):
  793. # A simple test to verify the numerics for bfloat16.
  794. def _SoftmaxCrossEntropyWithLogits(logits, targets):
  795. # logits, targets: float arrays of the same shape.
  796. assert logits.shape == targets.shape
  797. stable_exp_logits = np.exp(
  798. logits - np.amax(logits, axis=1, keepdims=True))
  799. pred = stable_exp_logits / np.sum(stable_exp_logits, 1, keepdims=True)
  800. return -np.sum(targets * np.log(pred + 1.0e-20), axis=1)
  801. np.random.seed(0)
  802. num_classes = 5
  803. batch_size = 3
  804. labels = [0, 1, 2]
  805. sampled = [1, 0, 2, 3]
  806. (weights, biases, hidden_acts, _, exp_logits,
  807. exp_labels) = self._GenerateTestData(
  808. num_classes=num_classes,
  809. dim=10,
  810. batch_size=batch_size,
  811. num_true=1,
  812. labels=labels,
  813. sampled=sampled,
  814. subtract_log_q=True)
  815. exp_sampled_softmax_loss = _SoftmaxCrossEntropyWithLogits(
  816. exp_logits, exp_labels)
  817. true_exp_bf16 = np.full([batch_size, 1],
  818. fill_value=0.5,
  819. dtype=dtypes.bfloat16.as_numpy_dtype)
  820. sampled_exp_bf16 = np.full([len(sampled)],
  821. fill_value=0.5,
  822. dtype=dtypes.bfloat16.as_numpy_dtype)
  823. sampled_vals_bf16 = (sampled, true_exp_bf16, sampled_exp_bf16)
  824. got_sampled_softmax_loss = math_ops.cast(
  825. nn_impl.sampled_softmax_loss_v2(
  826. weights=constant_op.constant(weights, dtype=dtypes.bfloat16),
  827. biases=constant_op.constant(biases, dtype=dtypes.bfloat16),
  828. labels=constant_op.constant(
  829. labels, shape=(batch_size, 1), dtype=dtypes.bfloat16),
  830. inputs=constant_op.constant(hidden_acts, dtype=dtypes.bfloat16),
  831. num_sampled=4,
  832. num_classes=num_classes,
  833. num_true=1,
  834. sampled_values=sampled_vals_bf16,
  835. remove_accidental_hits=False), dtypes.float32)
  836. self.assertAllClose(exp_sampled_softmax_loss,
  837. self.evaluate(got_sampled_softmax_loss), 1e-1)
  838. class CReluTest(test_lib.TestCase):
  839. def test(self):
  840. np.random.seed(1) # Make it reproducible.
  841. x = np.random.randn(3, 4).astype(np.float32)
  842. y = np.concatenate([x * (x > 0), -x * (x < 0)], axis=1)
  843. z = self.evaluate(nn_ops.crelu(constant_op.constant(x)))
  844. self.assertAllClose(y, z, 1e-4)
  845. class ReluTest(test_lib.TestCase):
  846. def test(self):
  847. np.random.seed(1) # Make it reproducible.
  848. x = np.random.randn(3, 4).astype(np.float32)
  849. y = np.maximum(x, 0.0)
  850. z = self.evaluate(nn_ops.relu(constant_op.constant(x)))
  851. self.assertAllEqual(y, z)
  852. @test_util.run_deprecated_v1
  853. def testNaNs(self):
  854. # Test that relu(nan) = nan for various sizes.
  855. for i in range(18):
  856. x = np.zeros(i) + np.nan
  857. with self.cached_session():
  858. z = nn_ops.relu(constant_op.constant(x)).eval()
  859. self.assertTrue(np.isnan(z).all())
  860. class LeakyReluTest(test_lib.TestCase):
  861. def testRange(self):
  862. batch_size = 3
  863. height, width = 4, 4
  864. np.random.seed(1) # Make it reproducible.
  865. inputs = np.random.uniform(size=(batch_size, height, width, 3)).astype(
  866. np.float32)
  867. inputs = constant_op.constant(inputs)
  868. outputs = nn_ops.leaky_relu(inputs)
  869. self.assertEquals(inputs.shape, outputs.shape)
  870. inputs, outputs = self.evaluate([inputs, outputs])
  871. self.assertGreaterEqual(outputs.min(), 0.0)
  872. self.assertLessEqual(outputs.max(), 1.0)
  873. self.assertAllClose(inputs, outputs)
  874. @test_util.run_deprecated_v1
  875. def testValues(self):
  876. for dtype in [np.int32, np.int64, np.float16, np.float32, np.float64]:
  877. np_values = np.array([-2, -1, 0, 1, 2], dtype=dtype)
  878. outputs = nn_ops.leaky_relu(constant_op.constant(np_values))
  879. outputs = self.evaluate(outputs)
  880. tol = 2e-3 if dtype == np.float16 else 1e-6
  881. self.assertAllClose(
  882. outputs, [-0.4, -0.2, 0.0, 1.0, 2.0], rtol=tol, atol=tol)
  883. @test_util.run_deprecated_v1
  884. def testName(self):
  885. np_values = np.array([-2, -1, 0, 1, 2], dtype=np.float64)
  886. outputs_with_name_set = nn_ops.leaky_relu(
  887. constant_op.constant(np_values),
  888. name='test_relu_op')
  889. self.assertEqual(outputs_with_name_set.name, 'test_relu_op:0')
  890. outputs_without_name_set = nn_ops.leaky_relu(
  891. constant_op.constant(np_values))
  892. self.assertEqual(outputs_without_name_set.name, 'LeakyRelu:0')
  893. class SwishTest(test_lib.TestCase):
  894. @test_util.run_deprecated_v1
  895. def testValues(self):
  896. np_values = np.array(
  897. [np.linspace(-10.0, 0.0, 100),
  898. np.linspace(0.0, 10.0, 100)],
  899. dtype=np.float32)
  900. tf_values = constant_op.constant(np_values)
  901. actual_tf_outputs = nn_impl.swish(tf_values)
  902. expected_tf_outputs = tf_values * math_ops.sigmoid(tf_values)
  903. actual_outputs, expected_outputs = self.evaluate(
  904. [actual_tf_outputs, expected_tf_outputs])
  905. self.assertAllClose(actual_outputs, expected_outputs)
  906. @test_util.run_deprecated_v1
  907. def testGradients(self):
  908. shape = [5, 3, 4]
  909. sigma = 5
  910. input_values = np.random.randn(*shape) * sigma
  911. x_tf = constant_op.constant(input_values)
  912. y_tf = nn_impl.swish(x_tf)
  913. with self.cached_session():
  914. err = gradient_checker.compute_gradient_error(x_tf, shape, y_tf, shape)
  915. self.assertLess(err, 1e-4)
  916. class MomentsTest(test_lib.TestCase):
  917. def doOutputTest(self,
  918. input_shape,
  919. moments_axes,
  920. tol=1e-4,
  921. check_gradients=False):
  922. for mu in [0.0, 1.0, 1e3]:
  923. for sigma in [1.0, 0.1]:
  924. for keep_dims in [True, False]:
  925. input_values = np.random.rand(*input_shape) * sigma + mu
  926. expected_mean = np.mean(
  927. input_values, axis=moments_axes, keepdims=keep_dims)
  928. expected_var = np.var(
  929. input_values, axis=moments_axes, keepdims=keep_dims)
  930. with ops.Graph().as_default() as g:
  931. with self.session(graph=g) as sess:
  932. inputs = constant_op.constant(
  933. input_values, shape=input_shape, dtype=dtypes.float32)
  934. mean, variance = nn_impl.moments_v2(
  935. inputs, moments_axes, keepdims=keep_dims)
  936. if check_gradients:
  937. err = gradient_checker.compute_gradient_error(
  938. inputs, input_shape, mean, mean.shape.as_list())
  939. self.assertLess(err, 1e-3)
  940. err = gradient_checker.compute_gradient_error(
  941. inputs, input_shape, variance, variance.shape.as_list())
  942. self.assertLess(err, 1e-3)
  943. # Evaluate.
  944. [mean, variance] = self.evaluate([mean, variance])
  945. # Make sure that there are no NaNs
  946. self.assertFalse(np.isnan(mean).any())
  947. self.assertFalse(np.isnan(variance).any())
  948. self.assertAllClose(mean, expected_mean, rtol=tol, atol=tol)
  949. self.assertAllClose(variance, expected_var, rtol=tol, atol=tol)
  950. def testOutputAndGradient2DInput0(self):
  951. self.doOutputTest((10, 10), (0,), check_gradients=True)
  952. def testOutputAndGradient2DInput01(self):
  953. self.doOutputTest((10, 10), (0, 1), check_gradients=True)
  954. def testOutput2DInput0(self):
  955. self.doOutputTest((10, 300), (0,))
  956. def testOutput2DInput1(self):
  957. self.doOutputTest((10, 300), (1,))
  958. def testOutput2DInput01(self):
  959. self.doOutputTest((10, 300), (0, 1))
  960. def testOutput4DInput0(self):
  961. self.doOutputTest((10, 10, 10, 30), (0,))
  962. def testOutput4DInput1(self):
  963. self.doOutputTest((10, 10, 10, 30), (1,))
  964. def testOutput4DInput3(self):
  965. self.doOutputTest((10, 10, 10, 30), (3,))
  966. def testOutput4DInput012(self):
  967. self.doOutputTest((10, 10, 10, 30), (0, 1, 2))
  968. def testOutput4DInput123(self):
  969. self.doOutputTest((10, 10, 10, 30), (1, 2, 3))
  970. class DataFormatDimMapTest(test_lib.TestCase):
  971. def _test(self, x_val, y_val_expected):
  972. x = constant_op.constant(x_val)
  973. y = nn_ops.data_format_dim_map(x)
  974. y_val = self.evaluate(y)
  975. self.assertAllEqual(y_val, y_val_expected)
  976. def test(self):
  977. self._test(0, 0)
  978. self._test(1, 2)
  979. self._test(2, 3)
  980. self._test(3, 1)
  981. self._test(-1, 1)
  982. self._test(-2, 3)
  983. self._test(-3, 2)
  984. self._test(-4, 0)
  985. self._test([1, 3], [2, 1])
  986. self._test([1, 3, -2], [2, 1, 3])
  987. self._test([1, -3, -2], [2, 2, 3])
  988. self._test([[1, -3], [1, -1]], [[2, 2], [2, 1]])
  989. def testNHWCtoNCHW(self):
  990. x_val = [1, -3, -2]
  991. y_val_expected = [2, 2, 3]
  992. x = constant_op.constant(x_val)
  993. y = nn_ops.data_format_dim_map(x, src_format="NHWC", dst_format="NCHW")
  994. with test_util.use_gpu():
  995. y_val = self.evaluate(y)
  996. self.assertAllEqual(y_val, y_val_expected)
  997. def testNHWCtoHWNC(self):
  998. x_val = [-4, -3, -2, -1, 0, 1, 2, 3]
  999. y_val_expected = [2, 0, 1, 3, 2, 0, 1, 3]
  1000. x = constant_op.constant(x_val)
  1001. y = nn_ops.data_format_dim_map(x, src_format="NHWC", dst_format="HWNC")
  1002. with test_util.use_gpu():
  1003. y_val = self.evaluate(y)
  1004. self.assertAllEqual(y_val, y_val_expected)
  1005. def testNHWCtoWHCN(self):
  1006. x_val = [-4, -3, -2, -1, 0, 1, 2, 3]
  1007. y_val_expected = [3, 1, 0, 2, 3, 1, 0, 2]
  1008. x = constant_op.constant(x_val)
  1009. y = nn_ops.data_format_dim_map(x, src_format="NHWC", dst_format="WHCN")
  1010. with test_util.use_gpu():
  1011. y_val = self.evaluate(y)
  1012. self.assertAllEqual(y_val, y_val_expected)
  1013. def testArbitraryASCII(self):
  1014. x_val = [-4, -3, -2, -1, 0, 1, 2, 3]
  1015. y_val_expected = [3, 2, 1, 0, 3, 2, 1, 0]
  1016. x = constant_op.constant(x_val)
  1017. y = nn_ops.data_format_dim_map(x, src_format="qwer", dst_format="rewq")
  1018. with test_util.use_gpu():
  1019. y_val = self.evaluate(y)
  1020. self.assertAllEqual(y_val, y_val_expected)
  1021. class DataFormatVectorPermuteTest(test_lib.TestCase):
  1022. def testNHWCToNCHW(self):
  1023. x_val = [7, 4, 9, 3]
  1024. x = constant_op.constant(x_val)
  1025. y = nn_ops.data_format_vec_permute(x)
  1026. with test_util.use_gpu():
  1027. y_val = self.evaluate(y)
  1028. self.assertAllEqual(y_val, [7, 3, 4, 9])
  1029. def testNCHWToNHWC(self):
  1030. x_val = [7, 4, 9, 3]
  1031. x = constant_op.constant(x_val)
  1032. y = nn_ops.data_format_vec_permute(x, src_format="NCHW", dst_format="NHWC")
  1033. with test_util.use_gpu():
  1034. y_val = self.evaluate(y)
  1035. self.assertAllEqual(y_val, [7, 9, 3, 4])
  1036. def testNHWCToHWNC(self):
  1037. x_val = [7, 4, 9, 3]
  1038. x = constant_op.constant(x_val)
  1039. y = nn_ops.data_format_vec_permute(x, src_format="NHWC", dst_format="HWNC")
  1040. with test_util.use_gpu():
  1041. y_val = self.evaluate(y)
  1042. self.assertAllEqual(y_val, [4, 9, 7, 3])
  1043. def testHWNCToNHWC(self):
  1044. x_val = [7, 4, 9, 3]
  1045. x = constant_op.constant(x_val)
  1046. y = nn_ops.data_format_vec_permute(x, src_format="HWNC", dst_format="NHWC")
  1047. with test_util.use_gpu():
  1048. y_val = self.evaluate(y)
  1049. self.assertAllEqual(y_val, [9, 7, 4, 3])
  1050. def testNHWCToNCHW2D(self):
  1051. x_val = [[7, 4], [9, 3], [4, 5], [5, 1]]
  1052. x = constant_op.constant(x_val)
  1053. y = nn_ops.data_format_vec_permute(x)
  1054. with test_util.use_gpu():
  1055. y_val = self.evaluate(y)
  1056. self.assertAllEqual(y_val, [[7, 4], [5, 1], [9, 3], [4, 5]])
  1057. def testNHWCToHWNC2D(self):
  1058. x_val = [[7, 4], [9, 3], [4, 5], [5, 1]]
  1059. x = constant_op.constant(x_val)
  1060. y = nn_ops.data_format_vec_permute(x, src_format="NHWC", dst_format="HWNC")
  1061. with test_util.use_gpu():
  1062. y_val = self.evaluate(y)
  1063. self.assertAllEqual(y_val, [[9, 3], [4, 5], [7, 4], [5, 1]])
  1064. def testHWNCToNHWC2D(self):
  1065. x_val = [[7, 4], [9, 3], [4, 5], [5, 1]]
  1066. x = constant_op.constant(x_val)
  1067. y = nn_ops.data_format_vec_permute(x, src_format="HWNC", dst_format="NHWC")
  1068. with test_util.use_gpu():
  1069. y_val = self.evaluate(y)
  1070. self.assertAllEqual(y_val, [[4, 5], [7, 4], [9, 3], [5, 1]])
  1071. def testNCHWToNHWC2D(self):
  1072. x_val = [[7, 4], [9, 3], [4, 5], [5, 1]]
  1073. x = constant_op.constant(x_val)
  1074. y = nn_ops.data_format_vec_permute(x, src_format="NCHW", dst_format="NHWC")
  1075. with test_util.use_gpu():
  1076. y_val = self.evaluate(y)
  1077. self.assertAllEqual(y_val, [[7, 4], [4, 5], [5, 1], [9, 3]])
  1078. if __name__ == "__main__":
  1079. test_lib.main()