diff --git a/tensorflow/python/keras/legacy_tf_layers/base.py b/tensorflow/python/keras/legacy_tf_layers/base.py index dce676ef743..c1020dd1205 100644 --- a/tensorflow/python/keras/legacy_tf_layers/base.py +++ b/tensorflow/python/keras/legacy_tf_layers/base.py @@ -527,12 +527,20 @@ class Layer(base_layer.Layer): # rather than initializing to None we check for an AttributeError. scope_context_manager = self._always_reuse_variable_scope except AttributeError: + scope_context_manager = None + + if scope_context_manager is None: # From this point we will always set reuse=True, so create a "final" # variable scope with this setting. We avoid re-creating variable scopes # after this point as an optimization. - self._always_reuse_variable_scope = vs.variable_scope( + scope_context_manager = vs.variable_scope( self._scope, reuse=True, auxiliary_name_scope=False) - scope_context_manager = self._always_reuse_variable_scope + + # Do not cache variable scopes if Eager mode is enabled. If Eager mode + # is enabled then we don't want to reuse scopes because the cached scope + # might be from a FuncGraph or Eager scope we are no longer in. + if not ops.executing_eagerly_outside_functions(): + self._always_reuse_variable_scope = scope_context_manager else: scope_context_manager = vs.variable_scope( self._scope, reuse=self._reuse, auxiliary_name_scope=False) diff --git a/tensorflow/tools/ci_build/ci_sanity.sh b/tensorflow/tools/ci_build/ci_sanity.sh index f11b694d9f3..d6df299cdff 100755 --- a/tensorflow/tools/ci_build/ci_sanity.sh +++ b/tensorflow/tools/ci_build/ci_sanity.sh @@ -112,6 +112,7 @@ do_pylint() { "^tensorflow/python/keras/engine/base_layer.py.*\[E1102.*not-callable "\ "^tensorflow/python/keras/layers/preprocessing/.*\[E1102.*not-callable "\ "^tensorflow/python/keras/layers/recurrent\.py.*\[E0203.*access-member-before-definition "\ +"^tensorflow/python/keras/legacy_tf_layers/base\.py.*\[E0203.*access-member-before-definition "\ "^tensorflow/python/kernel_tests/constant_op_eager_test.py.*\[E0303.*invalid-length-returned "\ "^tensorflow/python/keras/utils/data_utils.py.*\[E1102.*not-callable "\ "^tensorflow/python/autograph/.*_py3_test\.py.*\[E0001.*syntax-error "\