scope_guard¶
- paddle.static. scope_guard ( scope ) [source]
-
This function switches scope through python with statement. Scope records the mapping between variable names and variables ( Variable ), similar to brackets in programming languages. If this function is not invoked, all variables and variable names are recorded in the default global scope. When users need to create variables with the same name, they need to switch scopes through this function if they do not want the mapping of variables with the same name to be overwritten. After switching through the with statement, all variables created in the with block will be assigned to a new scope.
- Parameters
-
scope – The new scope.
- Returns
-
None
Examples
>>> import paddle >>> import numpy >>> paddle.enable_static() >>> new_scope = paddle.static.Scope() >>> with paddle.static.scope_guard(new_scope): ... paddle.static.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), paddle.CPUPlace()) >>> numpy.array(new_scope.find_var("data").get_tensor()) array([[1., 1.], [1., 1.]])