def test_get_suggestion(self, get_architecture, problem_type):
        # Return value (architectures) for the various trials.
        get_architecture.side_effect = [
            np.array([1, 2, 3, 4]),
            np.array([2, 3, 4, 1]),
            np.array([3, 4, 1, 2]),
            np.array([4, 1, 2, 3]),
            np.array([1, 1, 1, 1]),
            np.array([2, 2, 2, 2]),
            np.array([3, 3, 3, 3]),
            np.array([2, 3, 2, 3]),
            np.array([3, 4, 3, 4])
        ]
        algorithm = categorical_harmonica.Harmonica(test_utils.create_spec(
            problem_type,
            blocks_to_use=[
                "FIXED_CHANNEL_CONVOLUTION_16", "FIXED_CHANNEL_CONVOLUTION_32",
                "FIXED_CHANNEL_CONVOLUTION_64", "CONVOLUTION_3X3"
            ],
            min_depth=4),
                                                    num_random_samples=10,
                                                    seed=73)

        output_architecture, _ = algorithm.get_suggestion(
            _create_trials(), hp.HParams())
        expected_output = [3, 3, 3, 3]
        if problem_type == phoenix_spec_pb2.PhoenixSpec.CNN:
            expected_output += [34]
        self.assertAllEqual(expected_output, output_architecture)
 def test_translate_architecture_to_assignment(self):
     algorithm = categorical_harmonica.Harmonica(
         test_utils.create_spec(phoenix_spec_pb2.PhoenixSpec.DNN,
                                blocks_to_use=[
                                    "FIXED_CHANNEL_CONVOLUTION_16",
                                    "FIXED_CHANNEL_CONVOLUTION_32",
                                    "FIXED_CHANNEL_CONVOLUTION_64",
                                    "CONVOLUTION_3X3"
                                ],
                                min_depth=4))
     assignment = algorithm.translate_architecture_to_feature_assignment(
         np.array([2, 3, 4]))
     expected_output = [[
         1.00000000e+00, 6.12323400e-17, -1.00000000e+00, -1.83697020e-16,
         1.00000000e+00, -1.00000000e+00, 1.00000000e+00, -1.00000000e+00,
         1.00000000e+00, -1.83697020e-16, -1.00000000e+00, 5.51091060e-16,
         0.00000000e+00, -1.00000000e+00, 3.67394040e-16, 1.00000000e+00
     ],
                        [
                            0.00000000e+00, 1.00000000e+00, 1.22464680e-16,
                            -1.00000000e+00, 0.00000000e+00, 1.22464680e-16,
                            -2.44929360e-16, 3.67394040e-16, 0.00000000e+00,
                            -1.00000000e+00, 3.67394040e-16, 1.00000000e+00,
                            1.00000000e+00, -1.83697020e-16,
                            -1.00000000e+00, 5.51091060e-16
                        ]]
     for i in range(len(assignment)):
         self.assertAllAlmostEqual(expected_output[i], assignment[i])
 def test_polynomial_expansion(self):
     algorithm = categorical_harmonica.Harmonica(
         test_utils.create_spec(phoenix_spec_pb2.PhoenixSpec.DNN))
     feature_extender = PolynomialFeatures(2, interaction_only=True)
     self.assertAllEqual([[1, 0, 1, 0], [1, 0.5, 1, 0.5]],
                         algorithm._get_polynomial_expansion(
                             feature_extender, np.array([[0, 1], [0.5,
                                                                  1]])))
 def test_get_good_architecture_with_relevant_variables(self):
     algorithm = categorical_harmonica.Harmonica(test_utils.create_spec(
         phoenix_spec_pb2.PhoenixSpec.CNN,
         blocks_to_use=[
             "FIXED_CHANNEL_CONVOLUTION_16", "FIXED_CHANNEL_CONVOLUTION_32",
             "FIXED_CHANNEL_CONVOLUTION_64", "CONVOLUTION_3X3"
         ],
         min_depth=2),
                                                 degree=2,
                                                 seed=73)
     expected_output = [3, 1, 34]
     feature_extender = PolynomialFeatures(2, interaction_only=True)
     self.assertAllEqual(
         expected_output,
         algorithm._get_good_architecture(
             feature_extender, 20, np.array([0, 0.5, 0.6, -0.2] + [0] * 33),
             set([3, 3, 3, 3])))
 def test_get_good_architecture(self, problem_type):
     algorithm = categorical_harmonica.Harmonica(test_utils.create_spec(
         problem_type,
         blocks_to_use=[
             "FIXED_CHANNEL_CONVOLUTION_16", "FIXED_CHANNEL_CONVOLUTION_32",
             "FIXED_CHANNEL_CONVOLUTION_64", "CONVOLUTION_3X3"
         ],
         min_depth=2),
                                                 degree=2,
                                                 seed=73)
     expected_output = [3, 3]
     if problem_type == phoenix_spec_pb2.PhoenixSpec.CNN:
         expected_output += [34]
     feature_extender = PolynomialFeatures(2, interaction_only=True)
     self.assertAllEqual(
         expected_output,
         algorithm._get_good_architecture(
             feature_extender, 10, np.array([0, 0.5, 0.6, -0.2] + [0] * 33),
             None))
 def test_extract_relevant_variables_indices(self):
     algorithm = categorical_harmonica.Harmonica(test_utils.create_spec(
         phoenix_spec_pb2.PhoenixSpec.CNN,
         blocks_to_use=[
             "FIXED_CHANNEL_CONVOLUTION_16", "FIXED_CHANNEL_CONVOLUTION_32",
             "FIXED_CHANNEL_CONVOLUTION_64", "CONVOLUTION_3X3"
         ],
         min_depth=4),
                                                 degree=2)
     feature_extender = PolynomialFeatures(2, interaction_only=True)
     algorithm._get_polynomial_expansion(feature_extender,
                                         np.array([[1, 2, 3, 4]]))
     output = algorithm._extract_relevant_variables_indices(
         feature_extender, np.array([1, 1, 1] + [0] * 7 + [1]))
     logging.info(output)
     self.assertAllEqual(output, set([0, 1, 2, 3]))
     output = algorithm._extract_relevant_variables_indices(
         feature_extender, np.array([1, 0, 0] + [0] * 7 + [1]))
     self.assertAllEqual(output, set([2, 3]))
     output = algorithm._extract_relevant_variables_indices(
         feature_extender, np.array([1, 0, 0] + [0] * 7 + [0]))
     self.assertEmpty(output)
 def test_batch_sample(self, get_architecture):
     algorithm = categorical_harmonica.Harmonica(test_utils.create_spec(
         phoenix_spec_pb2.PhoenixSpec.DNN,
         blocks_to_use=[
             "FIXED_CHANNEL_CONVOLUTION_16", "FIXED_CHANNEL_CONVOLUTION_32",
             "FIXED_CHANNEL_CONVOLUTION_64", "CONVOLUTION_3X3"
         ],
         min_depth=4),
                                                 degree=1)
     get_architecture.side_effect = [
         np.array([1, 2, 3, 4]),
         np.array([2, 3, 4, 1]),
         np.array([3, 4, 1, 2]),
         # Adding 34 (flatten) to see that it is ignored
         np.array([4, 1, 2, 34, 3]),
         np.array([1, 1, 1, 1]),
         np.array([2, 2, 2, 2]),
         np.array([3, 3, 3, 3]),
         np.array([2, 3, 2, 3]),
         np.array([3, 4, 3, 4])
     ]
     x, y = algorithm.batch_sample(_create_trials())
     # Check first and last assignments
     self.assertAllAlmostEqual([
         1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
         1.00000000e+00, 6.12323400e-17, -1.00000000e+00, -1.83697020e-16,
         1.00000000e+00, -1.00000000e+00, 1.00000000e+00, -1.00000000e+00,
         1.00000000e+00, -1.83697020e-16, -1.00000000e+00, 5.51091060e-16
     ], x[0])
     self.assertAllAlmostEqual([
         0.00000000e+00, 1.22464680e-16, -2.44929360e-16, 3.67394040e-16,
         0.00000000e+00, -1.00000000e+00, 3.67394040e-16, 1.00000000e+00,
         0.00000000e+00, 1.22464680e-16, -2.44929360e-16, 3.67394040e-16,
         0.00000000e+00, -1.00000000e+00, 3.67394040e-16, 1.00000000e+00
     ], x[17])
     self.assertAllEqual([
         0.97, 0., 0.94, 0., 0.7, 0., 0.72, 0., 0.3, 0., 0.79, 0., 0.39, 0.,
         0.9, 0., 0.19, 0.
     ], y)
 def test_not_enough_data_get_suggestion(self, spec, initial_architecture,
                                         expected_architecture):
     algorithm = categorical_harmonica.Harmonica(spec)
     output_architecture, _ = algorithm.get_suggestion(
         [], hp.HParams(initial_architecture=initial_architecture))
     self.assertAllEqual(expected_architecture, output_architecture)