(self)
| 359 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 360 | |
| 361 | def test3DInputAxis2(self): |
| 362 | epsilon = 1e-3 |
| 363 | bn = normalization_layers.BatchNormalization( |
| 364 | axis=2, epsilon=epsilon, momentum=0.9) |
| 365 | inputs = variables.Variable( |
| 366 | np.random.random((5, 4, 3)) + 100, dtype=dtypes.float32) |
| 367 | training = array_ops.placeholder(dtype='bool') |
| 368 | outputs = bn.apply(inputs, training=training) |
| 369 | |
| 370 | with self.cached_session() as sess: |
| 371 | # Test training with placeholder learning phase. |
| 372 | self.evaluate(variables.global_variables_initializer()) |
| 373 | np_gamma, np_beta = self.evaluate([bn.gamma, bn.beta]) |
| 374 | np_gamma = np.reshape(np_gamma, (1, 1, 3)) |
| 375 | np_beta = np.reshape(np_beta, (1, 1, 3)) |
| 376 | for _ in range(100): |
| 377 | np_output, _, _ = sess.run([outputs] + bn.updates, |
| 378 | feed_dict={training: True}) |
| 379 | # Verify that the axis is normalized during training. |
| 380 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 381 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 382 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 383 | |
| 384 | # Verify that the statistics are updated during training. |
| 385 | moving_mean, moving_var = self.evaluate( |
| 386 | [bn.moving_mean, bn.moving_variance]) |
| 387 | np_inputs = self.evaluate(inputs) |
| 388 | mean = np.mean(np_inputs, axis=(0, 1)) |
| 389 | std = np.std(np_inputs, axis=(0, 1)) |
| 390 | variance = np.square(std) |
| 391 | self.assertAllClose(mean, moving_mean, atol=1e-2) |
| 392 | self.assertAllClose(variance, moving_var, atol=1e-2) |
| 393 | |
| 394 | # Test inference with placeholder learning phase. |
| 395 | np_output = sess.run(outputs, feed_dict={training: False}) |
| 396 | |
| 397 | # Verify that the axis is normalized during inference. |
| 398 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 399 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 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): |
nothing calls this directly
no test coverage detected