MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _fetch_var

Function _fetch_var

python/paddle/base/executor.py:587–618  ·  view source on GitHub ↗

Fetch the value of the variable with the given name from the given scope. Args: name(str): name of the variable. Typically, only persistable variables can be found in the scope used for running the program. scope(core._Scope|None): scope object. It should be

(name, scope=None, return_numpy=True)

Source from the content-addressed store, hash-verified

585
586
587def _fetch_var(name, scope=None, return_numpy=True):
588 """
589 Fetch the value of the variable with the given name from the
590 given scope.
591
592 Args:
593 name(str): name of the variable. Typically, only persistable variables
594 can be found in the scope used for running the program.
595 scope(core._Scope|None): scope object. It should be the scope where
596 you pass to Executor.run() when running your program.
597 If None, global_scope() will be used. Default None.
598 return_numpy(bool): whether convert the tensor to numpy.ndarray.
599 Default True.
600
601 Returns:
602 DenseTensor|numpy.ndarray
603 """
604 assert isinstance(name, str)
605 if scope is None:
606 scope = global_scope()
607 assert isinstance(scope, core._Scope)
608
609 var = scope.find_var(_to_name_str(name))
610 assert var is not None, (
611 "Cannot find " + name + " in scope. Perhaps you need to make the"
612 " variable persistable by using var.persistable = True in your"
613 " program."
614 )
615 tensor = var.get_tensor()
616 if return_numpy:
617 tensor = as_numpy(tensor, copy=True)
618 return tensor
619
620
621def _to_name_str(var):

Callers

nothing calls this directly

Calls 4

global_scopeFunction · 0.85
_to_name_strFunction · 0.85
as_numpyFunction · 0.85
get_tensorMethod · 0.45

Tested by

no test coverage detected