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

Method state_dict

python/paddle/base/framework.py:7511–7610  ·  view source on GitHub ↗

Get parameters and persistable buffers of program as a dict. The key is the name of the parameter or the name of the buffer. The value is the tensor of this variable in the given scope. .. note:: This function MUST called after run start_up_program Args

(self, mode="all", scope=None)

Source from the content-addressed store, hash-verified

7509 return parameters
7510
7511 def state_dict(self, mode="all", scope=None):
7512 """
7513 Get parameters and persistable buffers of program as a dict. The key is the name of the parameter or the name of the buffer.
7514 The value is the tensor of this variable in the given scope.
7515
7516 .. note::
7517 This function MUST called after run start_up_program
7518
7519 Args:
7520 mode(str, optional): Source of the obtained parameters and buffers.
7521 'opt' : The return value only contains the variable in the optimizer.
7522 'param' : The return value only contains the variable in the network, not the variable in the optimizer.
7523 'all' : The return value contains the variable in the network and optimizer.
7524 Default: 'all'
7525 scope(Scope, optional) : If scope is None, state_dict will be set to global scope
7526 obtained through 'paddle.static.global_scope()'. Otherwise, value will be set to scope.
7527 Default: None
7528
7529 Returns:
7530 dict: a dict contains the parameters and persistable buffers.
7531
7532 Examples:
7533 .. code-block:: pycon
7534
7535 >>> import paddle
7536 >>> import paddle.static as static
7537
7538 >>> paddle.enable_static()
7539
7540 >>> x = static.data(name="x", shape=[10, 10], dtype='float32')
7541 >>> y = static.nn.fc(x, 10)
7542 >>> z = static.nn.fc(y, 10)
7543
7544 >>> place = paddle.CPUPlace()
7545 >>> exe = static.Executor(place)
7546 >>> exe.run(static.default_startup_program())
7547 >>> prog = static.default_main_program()
7548
7549 >>> path = "./temp/model.pdparams"
7550 >>> paddle.save(prog.state_dict(), path)
7551 """
7552 # The 'framework' is a low-level module, and 'executor'
7553 # can not be imported at the beginning of this file.
7554 # Therefore, the above two modules are dynamically imported.
7555 from .executor import global_scope
7556
7557 if scope is not None and not isinstance(scope, core._Scope):
7558 raise TypeError(
7559 f"`scope` should be None or `paddle.static.Scope'` type, but received {type(scope)}."
7560 )
7561
7562 if scope is None:
7563 scope = global_scope()
7564
7565 if not isinstance(mode, str):
7566 raise TypeError(
7567 f"Type of `mode` should be string, but received {type(mode)}."
7568 )

Callers

nothing calls this directly

Calls 7

list_varsMethod · 0.95
TypeErrorClass · 0.85
global_scopeFunction · 0.85
filterFunction · 0.85
ValueErrorClass · 0.85
typeFunction · 0.50
get_tensorMethod · 0.45

Tested by

no test coverage detected