11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Text ;
45
56namespace Tensorflow
@@ -16,7 +17,8 @@ public class PureVariableScope : IPython
1617 private _VariableScopeStore _var_scope_store ;
1718 private VariableScope variable_scope_object ;
1819 private VariableScope _cached_variable_scope_object ;
19-
20+ VariableScope _last_variable_scope_object ;
21+ Dictionary < string , int > _old_subscopes ;
2022 public PureVariableScope ( string name ,
2123 string old_name_scope = null ,
2224 TF_DataType dtype = TF_DataType . DtInvalid )
@@ -51,6 +53,7 @@ public void __enter__()
5153 if ( _scope != null )
5254 {
5355 _var_scope_store . open_variable_scope ( _new_name ) ;
56+ _old_subscopes = _var_scope_store . variable_scopes_count . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
5457 variable_scope_object = _cached_variable_scope_object ;
5558 }
5659 else
@@ -66,6 +69,7 @@ public void __enter__()
6669 _var_scope_store . open_variable_scope ( _new_name ) ;
6770 }
6871 _var_scope_store . current_scope = variable_scope_object ;
72+ _last_variable_scope_object = variable_scope_object ;
6973 }
7074
7175 public void Dispose ( )
@@ -75,7 +79,12 @@ public void Dispose()
7579
7680 public void __exit__ ( )
7781 {
78-
82+ // If jumping out from a non-prolonged scope, restore counts.
83+ if ( _scope != null )
84+ _var_scope_store . variable_scopes_count = _old_subscopes ;
85+ else
86+ _var_scope_store . close_variable_subscopes ( _new_name ) ;
87+ _var_scope_store . current_scope = _old ;
7988 }
8089
8190 public static implicit operator VariableScope ( PureVariableScope scope )
0 commit comments