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() 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 testConvertV2ResourceCase(self): """Tests that a v2 case() with resource variables converts properly.""" with ops.Graph().as_default(): with variable_scope.variable_scope("", use_resource=True): control_flow_v2_toggles.enable_control_flow_v2() x = variable_scope.get_variable("x", initializer=1.0) y = variable_scope.get_variable("y", initializer=2.0) _ = control_flow_ops.case([(gen_math_ops.less(x, y), lambda: x)], default=lambda: y) control_flow_v2_toggles.disable_control_flow_v2() with session_lib.Session() as sess: sess.run(variables.global_variables_initializer()) variable_graph_def = sess.graph.as_graph_def() constant_graph_def = ( convert_to_constants .convert_variables_to_constants_from_session_graph( sess, variable_graph_def, ["case/cond"])) self._assertGraphContains( constant_graph_def, """ node {name: "x" op: "Const"} node {name: "y" op: "Const"} node { name: "case/cond" op: "If" input: "Less" input: "x" input: "y" attr {key: "Tcond" value {type: DT_BOOL}} attr {key: "Tin" value {list {type: DT_FLOAT type: DT_FLOAT}}} attr {key: "Tout" value {list {type: DT_FLOAT}}} } library { function { signature { name: "case_cond_false_frozen_0" input_arg {name: "placeholder" type: DT_FLOAT} input_arg {name: "readvariableop_y" type: DT_FLOAT} output_arg {name: "readvariableop" type: DT_FLOAT} } } function { signature { name: "case_cond_true_frozen_0" input_arg {name: "placeholder" type: DT_FLOAT} input_arg {name: "readvariableop_x" type: DT_FLOAT} output_arg {name: "readvariableop" type: DT_FLOAT} } } }""")
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 setUp(self): self._enabled = control_flow_v2_toggles.control_flow_v2_enabled() control_flow_v2_toggles.enable_control_flow_v2() super(WhileV2Test, self).setUp()
def testConvertV2UnconvertedResourceNestedCase(self): """Tests unconverted variable propagation through nested functions.""" with ops.Graph().as_default(): with variable_scope.variable_scope("", use_resource=True): control_flow_v2_toggles.enable_control_flow_v2() x = variable_scope.get_variable("x", initializer=1.0) y = variable_scope.get_variable("y", initializer=2.0) z = variable_scope.get_variable("z", initializer=3.0) # pylint: disable=g-long-lambda _ = control_flow_ops.case( [(gen_math_ops.less(x, y), lambda: x)], default=lambda: control_flow_ops.case( [(gen_math_ops.less(z, y), lambda: z)], default=lambda: y)) # pylint: enable=g-long-lambda control_flow_v2_toggles.disable_control_flow_v2() with session_lib.Session() as sess: sess.run(variables.global_variables_initializer()) variable_graph_def = sess.graph.as_graph_def() constant_graph_def = ( convert_to_constants .convert_variables_to_constants_from_session_graph( sess, variable_graph_def, ["case/cond"], variable_names_denylist=["y"])) self._assertGraphContains( constant_graph_def, """ node {name: "x" op: "Const"} node {name: "y" op: "VarHandleOp"} node {name: "z" op: "Const"} node {name: "Less/ReadVariableOp" op: "Identity" input: "x"} node {name: "Less/ReadVariableOp_1" op: "ReadVariableOp" input: "y"} node { name: "case/cond" op: "If" input: "x" input: "z" input: "y" attr { key: "Tin" value {list {type: DT_FLOAT type: DT_FLOAT type: DT_RESOURCE}}} attr { key: "_read_only_resource_inputs" value {list {i: 1 i: 2 i: 3}}} attr {key: "then_branch" value {func {name: "case_cond_true_frozen_0"}}} attr {key: "else_branch" value {func {name: "case_cond_false_frozen_0"}}} attr {key: "output_shapes" value {list {shape {}}}} } library { function { signature { name: "case_cond_true_frozen_0" input_arg {name: "placeholder" type: DT_FLOAT} input_arg {name: "placeholder_1" type: DT_RESOURCE} input_arg {name: "readvariableop_x" type: DT_FLOAT} output_arg {name: "readvariableop" type: DT_FLOAT} is_stateful: true } node_def {name: "ReadVariableOp" op: "Identity" input: "readvariableop_x"}} function { signature { name: "case_cond_false_frozen_0" input_arg {name: "placeholder" type: DT_FLOAT} input_arg {name: "less_readvariableop_1_y" type: DT_RESOURCE} input_arg {name: "less_readvariableop_z" type: DT_FLOAT} output_arg {name: "case_cond_identity" type: DT_FLOAT} is_stateful: true } node_def {name: "Less/ReadVariableOp_1" op: "ReadVariableOp" input: "less_readvariableop_1_y"} node_def {name: "Less/ReadVariableOp" op: "Identity" input: "less_readvariableop_z"} node_def {name: "case/cond" op: "If" input: "less_readvariableop_z" input: "less_readvariableop_1_y" attr { key: "Tin" value {list {type: DT_FLOAT type: DT_RESOURCE}}} attr {key: "then_branch" value {func {name: "case_cond_true_frozen_1"}}} attr {key: "else_branch" value {func {name: "case_cond_false_frozen_1"}}} attr { key: "_read_only_resource_inputs" value {list {i: 1 i: 2}}}}} function { signature { name: "case_cond_false_frozen_1" input_arg {name: "placeholder" type: DT_FLOAT} input_arg {name: "readvariableop_y" type: DT_RESOURCE} output_arg {name: "readvariableop" type: DT_FLOAT} is_stateful: true } node_def {name: "ReadVariableOp" op: "ReadVariableOp" input: "readvariableop_y"}} function { signature { name: "case_cond_true_frozen_1" input_arg {name: "placeholder" type: DT_RESOURCE} input_arg {name: "readvariableop_z" type: DT_FLOAT} output_arg {name: "readvariableop" type: DT_FLOAT} is_stateful: true } node_def {name: "ReadVariableOp" op: "Identity" input: "readvariableop_z"}}}""")