1# Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Switching v2 features on and off.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python import tf2 22from tensorflow.python.framework import ops 23from tensorflow.python.framework import tensor_shape 24from tensorflow.python.ops import variable_scope 25 26from tensorflow.python.util.tf_export import tf_export 27 28 29@tf_export(v1=["enable_v2_behavior"]) 30def enable_v2_behavior(): 31 """Enables TensorFlow 2.x behaviors. 32 33 This function can be called at the beginning of the program (before `Tensors`, 34 `Graphs` or other structures have been created, and before devices have been 35 initialized. It switches all global behaviors that are different between 36 TensorFlow 1.x and 2.x to behave as intended for 2.x. 37 38 This function is called in the main TensorFlow `__init__.py` file, user should 39 not need to call it, except during complex migrations. 40 """ 41 tf2.enable() # Switches TensorArrayV2 and control flow V2 42 ops.enable_eager_execution() 43 tensor_shape.enable_v2_tensorshape() # Also switched by tf2 44 variable_scope.enable_resource_variables() 45 46 47@tf_export(v1=["disable_v2_behavior"]) 48def disable_v2_behavior(): 49 """Disables TensorFlow 2.x behaviors. 50 51 This function can be called at the beginning of the program (before `Tensors`, 52 `Graphs` or other structures have been created, and before devices have been 53 initialized. It switches all global behaviors that are different between 54 TensorFlow 1.x and 2.x to behave as intended for 1.x. 55 56 User can call this function to disable 2.x behavior during complex migrations. 57 """ 58 tf2.disable() # Switches TensorArrayV2 and control flow V2 59 ops.disable_eager_execution() 60 tensor_shape.disable_v2_tensorshape() # Also switched by tf2 61 variable_scope.disable_resource_variables() 62