Beispiel #1
0
def disable_v2_behavior():
  """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.
  """
  tf2.disable()
  ops.disable_eager_execution()
  tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
  variable_scope.disable_resource_variables()
  ops.disable_tensor_equality()
  # Disables TensorArrayV2 and control flow V2.
  control_flow_v2_toggles.disable_control_flow_v2()
  # Make sure internal uses of tf.data symbols map to V1 versions.
  dataset_ops.Dataset = dataset_ops.DatasetV1
  readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
  readers.TFRecordDataset = readers.TFRecordDatasetV1
  readers.TextLineDataset = readers.TextLineDatasetV1
  counter.Counter = counter.CounterV1
  interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1
  interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1
  random_ops.RandomDataset = random_ops.RandomDatasetV1
  exp_readers.CsvDataset = exp_readers.CsvDatasetV1
  exp_readers.SqlDataset = exp_readers.SqlDatasetV1
  exp_readers.make_batched_features_dataset = (
      exp_readers.make_batched_features_dataset_v1)
  exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Beispiel #2
0
    def testEqualityBroadcast(self):
        default = ops.Tensor._USE_EQUALITY

        try:
            tf_a = constant_op.constant([1, 1])
            tf_b = constant_op.constant([1, 1])
            tf_c = constant_op.constant([[1, 1], [1, 1]])
            tf_d = constant_op.constant([[1, 2], [1, 2]])
            tf_e = constant_op.constant([1, 1, 1])
            np_a = np.array([1, 1])
            np_b = np.array([1, 1])
            np_c = np.array([[1, 1], [1, 1]])
            np_d = np.array([[1, 2], [1, 2]])
            np_e = np.array([1, 1, 1])

            ops.disable_tensor_equality()
            # We don't do element-wise comparison
            self.assertNotEqual(tf_a, tf_b)
            self.assertNotEqual(tf_a, tf_c)
            self.assertNotEqual(tf_a, tf_d)

            ops.enable_tensor_equality()
            # We do element-wise comparison but can't convert results array to bool
            with self.assertRaises(ValueError):
                bool(tf_a == tf_b)
            self.assertAllEqual(tf_a == tf_b, [True, True])
            with self.assertRaises(ValueError):
                bool(tf_a == tf_c)
            self.assertAllEqual(tf_a == tf_c, [[True, True], [True, True]])
            with self.assertRaises(ValueError):
                bool(tf_a == tf_d)
            self.assertAllEqual(tf_a == tf_d, [[True, False], [True, False]])
            if compat.forward_compatible(2019, 9, 25):
                self.assertFalse(bool(tf_a == tf_e))
                self.assertTrue(bool(tf_a != tf_e))
                self.assertNotAllEqual(tf_a, tf_e)
            else:
                with self.assertRaises(errors.InvalidArgumentError):
                    bool(tf_a != tf_e)

            with self.assertRaises(ValueError):
                bool(np_a == np_b)
            self.assertAllEqual(np_a == np_b, [True, True])
            with self.assertRaises(ValueError):
                bool(np_a == np_c)
            self.assertAllEqual(np_a == np_c, [[True, True], [True, True]])
            self.assertAllEqual(np_a == np_d, [[True, False], [True, False]])
            self.assertFalse(bool(np_a == np_e))
            self.assertTrue(bool(np_a != np_e))
            self.assertNotAllEqual(np_a, np_e)
        finally:
            if default:
                ops.enable_tensor_equality()
            else:
                ops.disable_tensor_equality()
Beispiel #3
0
  def testEqualityBroadcast(self):
    default = ops.Tensor._USE_EQUALITY

    try:
      tf_a = constant_op.constant([1, 1])
      tf_b = constant_op.constant([1, 1])
      tf_c = constant_op.constant([[1, 1], [1, 1]])
      tf_d = constant_op.constant([[1, 2], [1, 2]])
      tf_e = constant_op.constant([1, 1, 1])
      np_a = np.array([1, 1])
      np_b = np.array([1, 1])
      np_c = np.array([[1, 1], [1, 1]])
      np_d = np.array([[1, 2], [1, 2]])
      np_e = np.array([1, 1, 1])

      ops.disable_tensor_equality()
      # We don't do element-wise comparison
      self.assertNotEqual(tf_a, tf_b)
      self.assertNotEqual(tf_a, tf_c)
      self.assertNotEqual(tf_a, tf_d)

      ops.enable_tensor_equality()
      # We do element-wise comparison but can't convert results array to bool
      with self.assertRaises(ValueError):
        bool(tf_a == tf_b)
      self.assertAllEqual(tf_a == tf_b, [True, True])
      with self.assertRaises(ValueError):
        bool(tf_a == tf_c)
      self.assertAllEqual(tf_a == tf_c, [[True, True], [True, True]])
      with self.assertRaises(ValueError):
        bool(tf_a == tf_d)
      self.assertAllEqual(tf_a == tf_d, [[True, False], [True, False]])

      # TODO(b/207402791): re-enable once incompatible shapes supported by XLA.
      if not test_util.is_xla_enabled():
        self.assertFalse(bool(tf_a == tf_e))
        self.assertTrue(bool(tf_a != tf_e))

      self.assertNotAllEqual(tf_a, tf_e)

      with self.assertRaises(ValueError):
        bool(np_a == np_b)
      self.assertAllEqual(np_a == np_b, [True, True])
      with self.assertRaises(ValueError):
        bool(np_a == np_c)
      self.assertAllEqual(np_a == np_c, [[True, True], [True, True]])
      self.assertAllEqual(np_a == np_d, [[True, False], [True, False]])
      self.assertFalse(bool(np_a == np_e))
      self.assertTrue(bool(np_a != np_e))
      self.assertNotAllEqual(np_a, np_e)
    finally:
      if default:
        ops.enable_tensor_equality()
      else:
        ops.disable_tensor_equality()
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

    This function can be called at the beginning of the program
    (before `Tensors`, `Graphs` or other structures have been created,
    and before devices have been initialized. It switches all global
    behaviors that are different between TensorFlow 1.x and 2.x to
    behave as intended for 1.x.

    User can call this function to disable 2.x behavior during complex
    migrations.
    """

    # _v2_behavior_usage_gauge.get_cell("disable").set(True)
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2

    # FIXME[bug]:
    #   Warning: disable_resource_variables (from
    #   tensorflow.python.ops.variable_scope) is deprecated and will be
    #   removed in a future version.
    #   Instructions for updating:
    #     non-resource variables are not supported in the long term
    #
    # The function tf.compat.v1.disable_resource_variables() is
    # depreciated instead you can mention use_resource=False in
    # tf.get_variable() which will be forced to true when eager excecution
    # is enabled by default in Tensorflow 2.x.
    # variable_scope.disable_resource_variables()

    ops.disable_tensor_equality()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
    # Make sure internal uses of tf.data symbols map to V1 versions.
    dataset_ops.Dataset = dataset_ops.DatasetV1
    readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
    readers.TFRecordDataset = readers.TFRecordDatasetV1
    readers.TextLineDataset = readers.TextLineDatasetV1
    counter.Counter = counter.CounterV1
    interleave_ops.choose_from_datasets = \
        interleave_ops.choose_from_datasets_v1
    interleave_ops.sample_from_datasets = \
        interleave_ops.sample_from_datasets_v1
    random_ops.RandomDataset = random_ops.RandomDatasetV1
    exp_readers.CsvDataset = exp_readers.CsvDatasetV1
    exp_readers.SqlDataset = exp_readers.SqlDatasetV1
    exp_readers.make_batched_features_dataset = (
        exp_readers.make_batched_features_dataset_v1)
    exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Beispiel #5
0
    def testEqualityBroadcast(self):
        default = ops.Tensor._USE_EQUALITY

        try:
            tf_a = constant_op.constant([1, 1])
            tf_b = constant_op.constant([1, 1])
            tf_c = constant_op.constant([[1, 1], [1, 1]])
            tf_d = constant_op.constant([[1, 2], [1, 2]])
            tf_e = constant_op.constant([1, 1, 1])
            np_a = np.array([1, 1])
            np_b = np.array([1, 1])
            np_c = np.array([[1, 1], [1, 1]])
            np_d = np.array([[1, 2], [1, 2]])
            np_e = np.array([1, 1, 1])

            ops.disable_tensor_equality()
            # We don't do element-wise comparison
            self.assertNotEqual(tf_a, tf_b)
            self.assertNotEqual(tf_a, tf_c)
            self.assertNotEqual(tf_a, tf_d)

            ops.enable_tensor_equality()
            # We do element-wise comparison but can't convert results array to bool
            with self.assertRaises(ValueError):
                bool(tf_a == tf_b)
            self.assertAllEqual(tf_a == tf_b, [True, True])
            with self.assertRaises(ValueError):
                bool(tf_a == tf_c)
            self.assertAllEqual(tf_a == tf_c, [[True, True], [True, True]])
            with self.assertRaises(ValueError):
                bool(tf_a == tf_d)
            self.assertAllEqual(tf_a == tf_d, [[True, False], [True, False]])
            # TODO(b/120678848): If shapes do not match we should instead return False
            with self.assertRaises(errors.InvalidArgumentError):
                bool(tf_a != tf_e)

            with self.assertRaises(ValueError):
                bool(np_a == np_b)
            self.assertAllEqual(np_a == np_b, [True, True])
            with self.assertRaises(ValueError):
                bool(np_a == np_c)
            self.assertAllEqual(np_a == np_c, [[True, True], [True, True]])
            self.assertAllEqual(np_a == np_d, [[True, False], [True, False]])
            bool(np_a != np_e)
        finally:
            if default:
                ops.enable_tensor_equality()
            else:
                ops.disable_tensor_equality()
    def test_deprecated_arg_values_when_value_is_none(self, mock_warning):
        @deprecation.deprecated_arg_values("2016-07-04",
                                           "This is how you update...",
                                           warn_once=True,
                                           arg0=None)
        def _fn(arg0):  # pylint: disable=unused-argument
            pass

        ops.enable_tensor_equality()
        initial_count = mock_warning.call_count
        # Check that we avoid error from explicit `var == None` check.
        _fn(arg0=variables.Variable(0))
        self.assertEqual(initial_count, mock_warning.call_count)
        _fn(arg0=None)
        self.assertEqual(initial_count + 1, mock_warning.call_count)
        ops.disable_tensor_equality()
Beispiel #7
0
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.
  """
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
    variable_scope.disable_resource_variables()
    ops.disable_tensor_equality()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.

  @compatibility(TF2)
  Using this function indicates that your software is not compatible
  with eager execution and `tf.function` in TF2.

  To migrate to TF2, rewrite your code to be compatible with eager execution.
  Please refer to the [migration guide]
  (https://www.tensorflow.org/guide/migrate) for additional resource on the
  topic.
  @end_compatibility
  """
    _v2_behavior_usage_gauge.get_cell("disable").set(True)
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
    variable_scope.disable_resource_variables()
    ops.disable_tensor_equality()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
    # Make sure internal uses of tf.data symbols map to V1 versions.
    dataset_ops.Dataset = dataset_ops.DatasetV1
    readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
    readers.TFRecordDataset = readers.TFRecordDatasetV1
    readers.TextLineDataset = readers.TextLineDatasetV1
    counter.Counter = counter.CounterV1
    interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1
    interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1
    random_ops.RandomDataset = random_ops.RandomDatasetV1
    exp_readers.CsvDataset = exp_readers.CsvDatasetV1
    exp_readers.SqlDataset = exp_readers.SqlDatasetV1
    exp_readers.make_batched_features_dataset = (
        exp_readers.make_batched_features_dataset_v1)
    exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Beispiel #9
0
  def testV2BehaviorLogging(self):
    with self.assertLogs(level='INFO') as logs:
      try:
        ops.enable_eager_execution()
      # Ignore this exception to test log output successfully
      except ValueError as e:
        if 'must be called at program startup' not in str(e):
          raise e

    self.assertIn('Enabling eager execution', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_eager_execution()
    self.assertIn('Disabling eager execution', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      tensor_shape.enable_v2_tensorshape()
    self.assertIn('Enabling v2 tensorshape', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      tensor_shape.disable_v2_tensorshape()
    self.assertIn('Disabling v2 tensorshape', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      variable_scope.enable_resource_variables()
    self.assertIn('Enabling resource variables', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      variable_scope.disable_resource_variables()
    self.assertIn('Disabling resource variables', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      ops.enable_tensor_equality()
    self.assertIn('Enabling tensor equality', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_tensor_equality()
    self.assertIn('Disabling tensor equality', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.enable_control_flow_v2()
    self.assertIn('Enabling control flow v2', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.disable_control_flow_v2()
    self.assertIn('Disabling control flow v2', ''.join(logs.output))
    def testEquality(self):
        default = ops.Tensor._USE_EQUALITY

        try:

            def _v1_check(a, b):
                self.assertEqual(a, a)
                self.assertIs(a, a)
                self.assertNotEqual(a, 1.0)
                self.assertIsNot(a, 1.0)
                self.assertNotEqual(a, b)
                self.assertIsNot(a, b)

            def _v2_check(a, b):
                self.assertEqual(a, a)
                self.assertIs(a, a)
                self.assertEqual(a, 1.0)
                self.assertIsNot(a, 1.0)
                self.assertEqual(a, b)
                self.assertIsNot(a, b)

            constant_a = constant_op.constant(1.0)
            constant_b = constant_op.constant(1.0)

            ops.disable_tensor_equality()
            self._test_hashable(constant_a, constant_b, True)
            _v1_check(constant_a, constant_b)
            ops.enable_tensor_equality()
            _v2_check(constant_a, constant_b)
            self._test_hashable(constant_a, constant_b, False)

            variable_a = variables.Variable(1.0)
            variable_b = variables.Variable(1.0)

            ops.disable_tensor_equality()
            _v1_check(variable_a, variable_b)
            self._test_hashable(variable_a, variable_b, True)
            ops.enable_tensor_equality()
            _v2_check(variable_a, variable_b)
            self._test_hashable(variable_a, variable_b, False)

            # We only test numpy behaviour in v2 mode since we'd like to match that.
            numpy_a = np.array(1.0)
            numpy_b = np.array(1.0)
            _v2_check(numpy_a, numpy_b)
            self._test_hashable(numpy_a, numpy_b, False)
        finally:
            if default:
                ops.enable_tensor_equality()
            else:
                ops.disable_tensor_equality()
    def testEqualityNan(self):
        default = ops.Tensor._USE_EQUALITY

        try:

            def _v1_check(a, b):
                self.assertEqual(a, a)
                self.assertIs(a, a)
                self.assertNotEqual(a, float('nan'))
                self.assertIsNot(a, float('nan'))
                self.assertNotEqual(a, b)
                self.assertIsNot(a, b)

            def _v2_check(a, b):
                self.assertNotEqual(a, a)
                self.assertIs(a, a)
                self.assertNotEqual(a, float('nan'))
                self.assertIsNot(a, float('nan'))
                self.assertNotEqual(a, b)
                self.assertIsNot(a, b)

            constant_a = constant_op.constant(float('nan'))
            constant_b = constant_op.constant(float('nan'))

            ops.disable_tensor_equality()
            self._test_hashable(constant_a, constant_b, True)
            _v1_check(constant_a, constant_b)
            ops.enable_tensor_equality()
            _v2_check(constant_a, constant_b)
            self._test_hashable(constant_a, constant_b, False)

            variable_a = variables.Variable(float('nan'))
            variable_b = variables.Variable(float('nan'))

            ops.disable_tensor_equality()
            _v1_check(variable_a, variable_b)
            self._test_hashable(variable_a, variable_b, True)
            ops.enable_tensor_equality()
            _v2_check(variable_a, variable_b)
            self._test_hashable(variable_a, variable_b, False)

            numpy_a = np.array(float('nan'))
            numpy_b = np.array(float('nan'))
            _v2_check(numpy_a, numpy_b)
            self._test_hashable(numpy_a, numpy_b, False)
        finally:
            if default:
                ops.enable_tensor_equality()
            else:
                ops.disable_tensor_equality()
Beispiel #12
0
  def test_binary_cwise_ops(self):
    # Enable tensor equality to test `equal` and `not_equal` ops below.
    default_equality = framework_ops.Tensor._USE_EQUALITY
    framework_ops.enable_tensor_equality()
    try:
      logical_ops = [
          math_ops.logical_and, math_ops.logical_or, math_ops.logical_xor
      ]

      # Wrapper functions restricting the range of inputs of zeta and polygamma.
      def safe_polygamma(x, y):
        return math_ops.polygamma(
            math_ops.round(clip_ops.clip_by_value(y, 1, 10)), x * x + 1)

      def safe_zeta(x, y):
        return math_ops.zeta(x * x + 1, y * y)

      float_ops = [
          math_ops.add,
          math_ops.add_v2,
          math_ops.atan2,
          math_ops.complex,
          math_ops.div,
          math_ops.divide,
          math_ops.div_no_nan,
          math_ops.equal,
          lambda x, y: framework_ops.convert_to_tensor(x == y),
          lambda x, y: framework_ops.convert_to_tensor(x != y),
          math_ops.floor_mod,
          math_ops.greater,
          math_ops.greater_equal,
          math_ops.igamma,
          math_ops.igammac,
          math_ops.igamma_grad_a,
          math_ops.less,
          math_ops.less_equal,
          math_ops.maximum,
          math_ops.minimum,
          math_ops.mod,
          math_ops.multiply,
          math_ops.not_equal,
          math_ops.pow,
          math_ops.squared_difference,
          math_ops.subtract,
          math_ops.truncate_mod,
          safe_polygamma,
          safe_zeta,
      ]
      # FloorDiv fails on XLA due floor's discontinuities exacerbating small
      # division differences.
      if not test_util.is_xla_enabled():
        float_ops += [math_ops.floor_div]
      for op in logical_ops + float_ops:
        x = random_ops.random_uniform([7, 3, 5])
        y = random_ops.random_uniform([3, 5])
        if op in logical_ops:
          x = x > 0
          y = y > 0

        output_dtypes = []

        # pylint: disable=cell-var-from-loop
        def loop_fn(i):
          x1 = array_ops.gather(x, i)
          y1 = array_ops.gather(y, i)
          outputs = [op(x, y), op(x1, y), op(x, y1), op(x1, y1), op(x1, x1)]
          del output_dtypes[:]
          output_dtypes.extend(t.dtype for t in outputs)
          return outputs

        # pylint: enable=cell-var-from-loop

        self._test_loop_fn(loop_fn, 3)
    finally:
      if not default_equality:
        framework_ops.disable_tensor_equality()
    def testEqualityCompare(self):
        default = ops.Tensor._USE_EQUALITY

        try:
            tf_a = constant_op.constant([1, 2])
            tf_b = constant_op.constant([1, 2])
            tf_c = constant_op.constant([1, 1])
            np_a = np.array([1, 2])
            np_b = np.array([1, 2])
            np_c = np.array([1, 1])

            ops.disable_tensor_equality()
            # We don't do element-wise comparison
            self.assertNotEqual(tf_a, tf_b)
            self.assertNotEqual(tf_a, tf_c)

            # We can compare list of tensors
            self.assertEqual([tf_a, tf_b], [tf_a, tf_b])
            self.assertNotEqual([tf_a, tf_b], [tf_b, tf_b])

            # We can compare existence in a list
            self.assertIn(tf_a, [tf_a, tf_b])
            self.assertIn(tf_a, [tf_b, tf_a])
            self.assertNotIn(tf_a, [tf_b, tf_c])

            ops.enable_tensor_equality()
            # We do element-wise comparison but can't convert results array to bool
            with self.assertRaises(ValueError):
                bool(tf_a == tf_b)
            self.assertAllEqual(tf_a == tf_b, [True, True])
            with self.assertRaises(ValueError):
                bool(tf_a == tf_c)
            self.assertAllEqual(tf_a == tf_c, [True, False])
            self.assertNotAllEqual(tf_a, tf_c)
            with self.assertRaises(ValueError):
                bool(np_a == np_b)
            self.assertAllEqual(np_a == np_b, [True, True])
            with self.assertRaises(ValueError):
                bool(np_a == np_c)
            self.assertAllEqual(np_a == np_c, [True, False])
            self.assertNotAllEqual(np_a, np_c)

            # Warning even though we technically shouldn't be able to compare here,
            # since the id is the same both TF & numpy will handle lists with the same
            # value without raising an error
            self.assertEqual([tf_a, tf_b], [tf_a, tf_b])
            with self.assertRaises(ValueError):
                bool([tf_a, tf_b] == [tf_b, tf_b])
            self.assertEqual([np_a, np_b], [np_a, np_b])
            with self.assertRaises(ValueError):
                bool([np_a, np_b] == [np_b, np_b])

            # Similar to lists we shouldn't be able to do a `in` check such as
            # `if a in [a,b]`. However if `a` is the first element, it works due to
            # short circuiting
            self.assertIn(tf_a, [tf_a, tf_b])
            with self.assertRaises(ValueError):
                bool(tf_a in [tf_b, tf_a])
            with self.assertRaises(ValueError):
                bool(tf_a in [tf_b, tf_c])
            self.assertIn(np_a, [np_a, np_b])
            with self.assertRaises(ValueError):
                bool(np_a in [np_b, np_a])
            with self.assertRaises(ValueError):
                bool(np_a in [np_b, np_c])

            # rank 0
            self.assertAllEqual(
                constant_op.constant(1) == constant_op.constant(1), True)
            self.assertAllEqual(
                constant_op.constant(1) == constant_op.constant(2), False)
            self.assertAllEqual(np.array(1) == np.array(1), True)
            self.assertAllEqual(np.array(1) == np.array(2), False)
        finally:
            if default:
                ops.enable_tensor_equality()
            else:
                ops.disable_tensor_equality()
Beispiel #14
0
from tensorflow_probability import distributions as tfd

from unification import var, unify

from kanren import run, eq, lall
from kanren.graph import walko
from kanren.assoccomm import eq_comm, commutative

from symbolic_pymc.meta import enable_lvar_defaults
from symbolic_pymc.tensorflow.meta import mt
from symbolic_pymc.tensorflow.graph import normalize_tf_graph

from tests.tensorflow import run_in_graph_mode
from tests.tensorflow.utils import mt_normal_log_prob

disable_tensor_equality()


@run_in_graph_mode
def test_walko():
    with enable_lvar_defaults("names"):
        add_1_mt = mt(1) + mt(2)

    def walk_rel(x, y):
        return lall(eq(x, mt(1)), eq(y, mt(3)))

    q = var()
    (res, ) = run(1, q, walko(walk_rel, add_1_mt, q))

    # The easiest way to check whether or not two arbitrary TF meta graphs are
    # (structurally) equivalent is to confirm that they unify.  This avoids