Note: This API is only avaliable in [Static Graph] mode
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.
scope – The new scope.
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())