(self)
| 400 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 401 | |
| 402 | def test4DInputAxis1(self): |
| 403 | if test.is_gpu_available(cuda_only=True): |
| 404 | epsilon = 1e-3 |
| 405 | bn = normalization_layers.BatchNormalization( |
| 406 | axis=1, epsilon=epsilon, momentum=0.9) |
| 407 | inputs = variables.Variable( |
| 408 | np.random.random((5, 4, 3, 6)) + 100, dtype=dtypes.float32) |
| 409 | training = array_ops.placeholder(dtype='bool') |
| 410 | outputs = bn.apply(inputs, training=training) |
| 411 | |
| 412 | with self.session(use_gpu=True) as sess: |
| 413 | # Test training with placeholder learning phase. |
| 414 | self.evaluate(variables.global_variables_initializer()) |
| 415 | np_gamma, np_beta = self.evaluate([bn.gamma, bn.beta]) |
| 416 | np_gamma = np.reshape(np_gamma, (1, 4, 1, 1)) |
| 417 | np_beta = np.reshape(np_beta, (1, 4, 1, 1)) |
| 418 | for _ in range(100): |
| 419 | np_output, _, _ = sess.run( |
| 420 | [outputs] + bn.updates, feed_dict={training: True}) |
| 421 | # Verify that the axis is normalized during training. |
| 422 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 423 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 424 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 425 | |
| 426 | # Verify that the statistics are updated during training. |
| 427 | moving_mean, moving_var = self.evaluate( |
| 428 | [bn.moving_mean, bn.moving_variance]) |
| 429 | np_inputs = self.evaluate(inputs) |
| 430 | mean = np.mean(np_inputs, axis=(0, 2, 3)) |
| 431 | std = np.std(np_inputs, axis=(0, 2, 3)) |
| 432 | variance = np.square(std) |
| 433 | self.assertAllClose(mean, moving_mean, atol=1e-2) |
| 434 | self.assertAllClose(variance, moving_var, atol=1e-2) |
| 435 | |
| 436 | # Test inference with placeholder learning phase. |
| 437 | np_output = sess.run(outputs, feed_dict={training: False}) |
| 438 | |
| 439 | # Verify that the axis is normalized during inference. |
| 440 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 441 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 442 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 443 | |
| 444 | def test4DInputAxis2(self): |
| 445 | if not test.is_gpu_available(cuda_only=True): |
nothing calls this directly
no test coverage detected