(self)
| 313 | bn.trainable_variables) |
| 314 | |
| 315 | def test3DInputAxis1(self): |
| 316 | if not test.is_gpu_available(cuda_only=True): |
| 317 | self.skipTest("Only run on GPU.") |
| 318 | |
| 319 | epsilon = 1e-3 |
| 320 | bn = normalization_layers.BatchNormalization( |
| 321 | axis=1, epsilon=epsilon, momentum=0.9) |
| 322 | inputs = variables.Variable( |
| 323 | np.random.random((5, 4, 3)) + 100, dtype=dtypes.float32) |
| 324 | training = array_ops.placeholder(dtype='bool') |
| 325 | outputs = bn.apply(inputs, training=training) |
| 326 | |
| 327 | with self.cached_session() as sess: |
| 328 | # Test training with placeholder learning phase. |
| 329 | self.evaluate(variables.global_variables_initializer()) |
| 330 | |
| 331 | np_gamma, np_beta = self.evaluate([bn.gamma, bn.beta]) |
| 332 | np_gamma = np.reshape(np_gamma, (1, 4, 1)) |
| 333 | np_beta = np.reshape(np_beta, (1, 4, 1)) |
| 334 | |
| 335 | for _ in range(100): |
| 336 | np_output, _, _ = sess.run([outputs] + bn.updates, |
| 337 | feed_dict={training: True}) |
| 338 | # Verify that the axis is normalized during training. |
| 339 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 340 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 341 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 342 | |
| 343 | # Verify that the statistics are updated during training. |
| 344 | moving_mean, moving_var = self.evaluate( |
| 345 | [bn.moving_mean, bn.moving_variance]) |
| 346 | np_inputs = self.evaluate(inputs) |
| 347 | mean = np.mean(np_inputs, axis=(0, 2)) |
| 348 | std = np.std(np_inputs, axis=(0, 2)) |
| 349 | variance = np.square(std) |
| 350 | self.assertAllClose(mean, moving_mean, atol=1e-2) |
| 351 | self.assertAllClose(variance, moving_var, atol=1e-2) |
| 352 | |
| 353 | # Test inference with placeholder learning phase. |
| 354 | np_output = sess.run(outputs, feed_dict={training: False}) |
| 355 | |
| 356 | # Verify that the axis is normalized during inference. |
| 357 | normed_np_output = ((np_output - epsilon) * np_gamma) + np_beta |
| 358 | self.assertAlmostEqual(np.mean(normed_np_output), 0., places=1) |
| 359 | self.assertAlmostEqual(np.std(normed_np_output), 1., places=1) |
| 360 | |
| 361 | def test3DInputAxis2(self): |
| 362 | epsilon = 1e-3 |
nothing calls this directly
no test coverage detected