def _test_variable_update(self,
                              test_name,
                              num_workers,
                              num_ps,
                              params,
                              num_controllers=0):
        """Tests variables are updated correctly when the given params are used."""
        output_dir_path = os.path.join(test_name, 'variable_update')
        logs = _spawn_benchmark_processes(output_dir_path, num_workers, num_ps,
                                          num_controllers, params)
        actual_losses = []
        for worker_logs in logs:
            outputs = test_util.get_training_outputs_from_logs(
                worker_logs, params.print_training_accuracy)
            actual_losses.append([x.loss for x in outputs])

        inputs = test_util.get_fake_var_update_inputs()
        expected_losses = test_util.TestModel().manually_compute_losses(
            inputs, num_workers, params)
        if params.variable_update == 'distributed_all_reduce':
            # In distributed all reduce, each step, the controller outputs the average
            # of the loss from each worker. So we modify expected losses accordingly.
            # E.g, we change [[1, 2], [4, 5]] to [[2.5, 3.5]]
            expected_losses = [[
                sum(losses) / num_workers for losses in zip(*expected_losses)
            ]]
        rtol = 3e-2 if params.use_fp16 else 1e-5
        for worker_actual_losses, worker_expected_losses in zip(
                actual_losses, expected_losses):
            self.assertAllClose(
                worker_actual_losses[:len(worker_expected_losses)],
                worker_expected_losses,
                rtol=rtol,
                atol=0.)
示例#2
0
 def testLowAccuracy(self):
     params = test_util.get_params('testLowAccuracy')._replace(
         print_training_accuracy=True, batch_size=5, num_batches=10)
     # We force low accuracy by having each batch containing 10 identical images,
     # each with a different label. This guarantees a top-1 accuracy of exactly
     # 0.1 and a top-5 accuracy of exactly 0.5.
     images = np.zeros((10, 227, 227, 3), dtype=np.float32)
     labels = np.arange(10, dtype=np.int32)
     logs = self._run_benchmark_cnn_with_fake_images(params, images, labels)
     training_outputs = test_util.get_training_outputs_from_logs(
         logs, params.print_training_accuracy)
     last_output = training_outputs[-1]
     # TODO(reedwm): These should be assertEqual but for some reason,
     # occasionally the accuracies are lower (Running this test 500 times, these
     # asserts failed twice). Investigate this problem.
     self.assertLessEqual(last_output.top_1_accuracy, 0.1)
     self.assertLessEqual(last_output.top_5_accuracy, 0.5)
示例#3
0
  def _get_benchmark_cnn_losses(self, inputs, params):
    """Returns the losses of BenchmarkCNN on the given inputs and params."""
    logs = []
    model = test_util.TestCNNModel()
    with test_util.monkey_patch(benchmark_cnn,
                                log_fn=test_util.print_and_add_to_list(logs),
                                LOSS_AND_ACCURACY_DIGITS_TO_SHOW=15):
      bench = benchmark_cnn.BenchmarkCNN(
          params, dataset=test_util.TestDataSet(), model=model)
      # The test model does not use labels when computing loss, so the label
      # values do not matter as long as it's the right shape.
      labels = np.array([1] * inputs.shape[0])
      bench.image_preprocessor.set_fake_data(inputs, labels)
      bench.run()

    outputs = test_util.get_training_outputs_from_logs(
        logs, params.print_training_accuracy)
    return [x.loss for x in outputs]