(self)
| 486 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 487 | |
| 488 | def test4DInputAxis3(self): |
| 489 | epsilon = 1e-3 |
| 490 | bn = normalization_layers.BatchNormalization( |
| 491 | axis=3, epsilon=epsilon, momentum=0.9) |
| 492 | inputs = variables.Variable( |
| 493 | np.random.random((5, 4, 3, 6)) + 100, dtype=dtypes.float32) |
| 494 | training = array_ops.placeholder(dtype='bool') |
| 495 | outputs = bn.apply(inputs, training=training) |
| 496 | |
| 497 | with self.cached_session() as sess: |
| 498 | # Test training with placeholder learning phase. |
| 499 | self.evaluate(variables.global_variables_initializer()) |
| 500 | np_gamma, np_beta = self.evaluate([bn.gamma, bn.beta]) |
| 501 | np_gamma = np.reshape(np_gamma, (1, 1, 1, 6)) |
| 502 | np_beta = np.reshape(np_beta, (1, 1, 1, 6)) |
| 503 | for _ in range(100): |
| 504 | np_output, _, _ = sess.run([outputs] + bn.updates, |
| 505 | feed_dict={training: True}) |
| 506 | # Verify that the axis is normalized during training. |
| 507 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 508 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 509 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 510 | |
| 511 | # Verify that the statistics are updated during training. |
| 512 | moving_mean, moving_var = self.evaluate( |
| 513 | [bn.moving_mean, bn.moving_variance]) |
| 514 | np_inputs = self.evaluate(inputs) |
| 515 | mean = np.mean(np_inputs, axis=(0, 1, 2)) |
| 516 | std = np.std(np_inputs, axis=(0, 1, 2)) |
| 517 | variance = np.square(std) |
| 518 | self.assertAllClose(mean, moving_mean, atol=1e-2) |
| 519 | self.assertAllClose(variance, moving_var, atol=1e-2) |
| 520 | |
| 521 | # Test inference with placeholder learning phase. |
| 522 | np_output = sess.run(outputs, feed_dict={training: False}) |
| 523 | |
| 524 | # Verify that the axis is normalized during inference. |
| 525 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 526 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 527 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 528 | |
| 529 | def test4DInputAxis3Fused(self): |
| 530 | epsilon = 1e-3 |
nothing calls this directly
no test coverage detected