コード例 #1
0
ファイル: v2_compat.py プロジェクト: wangjunbo2000/wangjb
def enable_v2_behavior():
  """Enables 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 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
  # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or
  # 2) the TF2_BEHAVIOR=1 environment variable is set.  In the latter case,
  # the modules below independently check if tf2.enabled().
  tf2.enable()
  ops.enable_eager_execution()
  tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
  variable_scope.enable_resource_variables()
  ops.enable_tensor_equality()
  # Enables TensorArrayV2 and control flow V2.
  control_flow_v2_toggles.enable_control_flow_v2()
  # Make sure internal uses of tf.data symbols map to V2 versions.
  dataset_ops.Dataset = dataset_ops.DatasetV2
  readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV2
  readers.TFRecordDataset = readers.TFRecordDatasetV2
  readers.TextLineDataset = readers.TextLineDatasetV2
  counter.Counter = counter.CounterV2
  interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v2
  interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v2
  random_ops.RandomDataset = random_ops.RandomDatasetV2
  exp_readers.CsvDataset = exp_readers.CsvDatasetV2
  exp_readers.SqlDataset = exp_readers.SqlDatasetV2
  exp_readers.make_batched_features_dataset = (
      exp_readers.make_batched_features_dataset_v2)
  exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v2
コード例 #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()
コード例 #3
0
ファイル: core_test.py プロジェクト: soongxueyong/tensorflow
  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()
コード例 #4
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()
コード例 #5
0
    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()
コード例 #6
0
ファイル: v2_compat.py プロジェクト: ecrawford-0/tensorflow
def enable_v2_behavior():
    """Enables 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 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
    tf2.enable()
    ops.enable_eager_execution()
    tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
    variable_scope.enable_resource_variables()
    ops.enable_tensor_equality()
    # Enables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.enable_control_flow_v2()
コード例 #7
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))
コード例 #8
0
ファイル: v2_compat.py プロジェクト: zzing0907/tensorflow
def enable_v2_behavior():
    """Enables 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 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
    # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or
    # 2) the TF2_BEHAVIOR=1 environment variable is set.  In the latter case,
    # the modules below independently check if tf2.enabled().
    tf2.enable()
    ops.enable_eager_execution()
    tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
    variable_scope.enable_resource_variables()
    ops.enable_tensor_equality()
    # Enables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.enable_control_flow_v2()
    # Make sure internal uses of the `dataset_ops.Dataset` map to DatasetV2.
    dataset_ops.Dataset = dataset_ops.DatasetV2
コード例 #9
0
    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()
コード例 #10
0
    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()
コード例 #11
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()
コード例 #12
0
    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()