(self)
| 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): |
| 446 | self.skipTest("Only run on GPU.") |
| 447 | |
| 448 | epsilon = 1e-3 |
| 449 | bn = normalization_layers.BatchNormalization( |
| 450 | axis=2, epsilon=epsilon, momentum=0.9) |
| 451 | inputs = variables.Variable( |
| 452 | np.random.random((5, 4, 3, 6)) + 100, dtype=dtypes.float32) |
| 453 | training = array_ops.placeholder(dtype='bool') |
| 454 | outputs = bn.apply(inputs, training=training) |
| 455 | |
| 456 | with self.cached_session() as sess: |
| 457 | # Test training with placeholder learning phase. |
| 458 | self.evaluate(variables.global_variables_initializer()) |
| 459 | np_gamma, np_beta = self.evaluate([bn.gamma, bn.beta]) |
| 460 | np_gamma = np.reshape(np_gamma, (1, 1, 3, 1)) |
| 461 | np_beta = np.reshape(np_beta, (1, 1, 3, 1)) |
| 462 | for _ in range(100): |
| 463 | np_output, _, _ = sess.run([outputs] + bn.updates, |
| 464 | feed_dict={training: True}) |
| 465 | # Verify that the axis is normalized during training. |
| 466 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 467 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 468 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 469 | |
| 470 | # Verify that the statistics are updated during training. |
| 471 | moving_mean, moving_var = self.evaluate( |
| 472 | [bn.moving_mean, bn.moving_variance]) |
| 473 | np_inputs = self.evaluate(inputs) |
| 474 | mean = np.mean(np_inputs, axis=(0, 1, 3)) |
| 475 | std = np.std(np_inputs, axis=(0, 1, 3)) |
| 476 | variance = np.square(std) |
| 477 | self.assertAllClose(mean, moving_mean, atol=1e-2) |
| 478 | self.assertAllClose(variance, moving_var, atol=1e-2) |
| 479 | |
| 480 | # Test inference with placeholder learning phase. |
| 481 | np_output = sess.run(outputs, feed_dict={training: False}) |
| 482 | |
| 483 | # Verify that the axis is normalized during inference. |
| 484 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 485 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 486 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 487 | |
| 488 | def test4DInputAxis3(self): |
| 489 | epsilon = 1e-3 |
nothing calls this directly
no test coverage detected