scope_guard

paddle.fluid.executor.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.fluid as fluid
import numpy

new_scope = fluid.Scope()
with fluid.scope_guard(new_scope):
     fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace())
numpy.array(new_scope.find_var("data").get_tensor())