Exemple #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
Exemple #2
0
def disable_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 1.x.

  User can call this function to disable 2.x behavior during complex migrations.
  """
    tf2.disable()  # Switches TensorArrayV2 and control flow V2
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
    variable_scope.disable_resource_variables()
Exemple #3
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()  # Switches TensorArrayV2 and control flow V2
  ops.disable_eager_execution()
  tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
  variable_scope.disable_resource_variables()
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
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
Exemple #6
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))
Exemple #7
0
 def set_v2_tensorshape(self, v2):
   if v2:
     tensor_shape.enable_v2_tensorshape()
   else:
     tensor_shape.disable_v2_tensorshape()