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)
| 585 | |
| 586 | |
| 587 | def _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 | |
| 621 | def _to_name_str(var): |
nothing calls this directly
no test coverage detected