def _check_target_program(self, benchmark): """Checks that a benchmark's target program is consistent with its examples. Args: benchmark: A Benchmark to verify. """ self.assertIsNotNone(benchmark.target_program) for example in benchmark.examples: # Turn inputs into constant tensors and assign them to variables using a # new global namespace. global_namespace = {'tf': tf} input_names_to_objects = value_search._input_names_to_objects( example.inputs) for input_name, input_object in input_names_to_objects.items(): input_value = value_module.InputValue(input_object, name='dummy_name') global_namespace[input_name] = input_value.value # Evaluate the target program, which uses the canonical variables. target_program_output = eval(benchmark.target_program, global_namespace) # pylint: disable=eval-used # Check that the two outputs have equal string representation. expected_output = tf_coder_utils.convert_to_tensor(example.output) self.assertEqual( tf_coder_utils.object_to_string(expected_output), tf_coder_utils.object_to_string(target_program_output))
def __init__(self, value, name, skip_tensor_conversion=False): """Initializes an InputValue to contain `value` with name `name`.""" if (not skip_tensor_conversion and not isinstance(value, tf_coder_utils.PRIMITIVE_TYPES)): try: value = tf_coder_utils.convert_to_tensor(value) except (TypeError, ValueError): pass super(InputValue, self).__init__(value) self.name = name
def test_convert_to_tensor_for_scalar(self): scalar = 1.23 tensor = tf_coder_utils.convert_to_tensor(scalar) self.assertIsInstance(tensor, tf.Tensor) self.assertAlmostEqual(scalar, tensor.numpy().tolist())
def test_convert_to_tensor_for_list(self): list_2d = [[1, 2, 3], [44, 55, 66]] tensor = tf_coder_utils.convert_to_tensor(list_2d) self.assertIsInstance(tensor, tf.Tensor) self.assertEqual(list_2d, tensor.numpy().tolist())
def test_convert_to_tensor_for_tensor(self): original_tensor = tf.constant([1, 2, 3]) tensor = tf_coder_utils.convert_to_tensor(original_tensor) self.assertIsInstance(tensor, tf.Tensor) self.assertEqual(original_tensor.numpy().tolist(), tensor.numpy().tolist())
def __init__(self, value): """Initializes an OutputValue to contain `value`.""" value = tf_coder_utils.convert_to_tensor(value) super(OutputValue, self).__init__(value)