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
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() # Switches TensorArrayV2 and control flow V2 ops.enable_eager_execution() tensor_shape.enable_v2_tensorshape() # Also switched by tf2 variable_scope.enable_resource_variables()
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() # Enables TensorArrayV2 and control flow V2. control_flow_v2_toggles.enable_control_flow_v2()
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 set_v2_tensorshape(self, v2): if v2: tensor_shape.enable_v2_tensorshape() else: tensor_shape.disable_v2_tensorshape()
def test_mul(self): inp = layers.Input(shape=[None], ragged=True) out = inp * inp model = training.Model(inp, out) x = ragged_factory_ops.constant([[3, 4], [1, 2], [3, 5]]) self.assertAllEqual(model(x), x * x) def test_sub(self): inp = layers.Input(shape=[None], ragged=True) out = inp - inp model = training.Model(inp, out) x = ragged_factory_ops.constant([[3, 4], [1, 2], [3, 5]]) self.assertAllEqual(model(x), x - x) def test_div(self): inp = layers.Input(shape=[None], ragged=True) out = inp / inp model = training.Model(inp, out) x = ragged_factory_ops.constant([[3, 4], [1, 2], [3, 5]]) self.assertAllEqual(model(x), x / x) if __name__ == '__main__': ops.enable_eager_execution() tensor_shape.enable_v2_tensorshape() test.main()