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

Method set_value

python/paddle/base/framework.py:2931–3037  ·  view source on GitHub ↗

Set the value to the tensor in given scope. Args: value(Tensor/ndarray) : The value to be set. scope(Scope, optional) : If `scope` is None, it will be set to global scope obtained through 'paddle.static.global_scope()'. Otherwise, use `scope

(self, value, scope=None)

Source from the content-addressed store, hash-verified

2929 return t
2930
2931 def set_value(self, value, scope=None):
2932 """
2933
2934 Set the value to the tensor in given scope.
2935
2936 Args:
2937 value(Tensor/ndarray) : The value to be set.
2938 scope(Scope, optional) : If `scope` is None, it will be set to global scope
2939 obtained through 'paddle.static.global_scope()'. Otherwise, use `scope`.
2940 Default: None
2941
2942 Returns:
2943 None
2944
2945 Examples:
2946 .. code-block:: pycon
2947
2948 >>> import paddle
2949 >>> import paddle.static as static
2950 >>> import numpy as np
2951
2952 >>> paddle.enable_static()
2953 >>> with paddle.pir_utils.OldIrGuard():
2954 ... x = static.data(name="x", shape=[10, 10], dtype="float32")
2955 ... y = static.nn.fc(x, 10, name="fc")
2956 ... place = paddle.CPUPlace()
2957 ... exe = static.Executor(place)
2958 ... prog = paddle.static.default_main_program()
2959 ... exe.run(static.default_startup_program())
2960 ... inputs = np.ones((10, 10), dtype="float32")
2961 ... exe.run(
2962 ... prog,
2963 ... feed={"x": inputs},
2964 ... fetch_list=[y],
2965 ... )
2966 ... path = "temp/tensor_"
2967 ... for var in prog.list_vars():
2968 ... if var.persistable:
2969 ... t = var.get_value()
2970 ... paddle.save(t, path + var.name + ".pdtensor")
2971
2972 ... for var in prog.list_vars():
2973 ... if var.persistable:
2974 ... t_load = paddle.load(path + var.name + ".pdtensor")
2975 ... var.set_value(t_load)
2976
2977 """
2978
2979 # The 'framework' is a low-level module, and 'executor'
2980 # can not be imported at the beginning of this file.
2981 # Therefore, the above two modules are dynamically imported.
2982 from .executor import global_scope
2983
2984 if not (isinstance(value, np.ndarray) or hasattr(value, "__array__")):
2985 raise TypeError(
2986 f"`value` should be `numpy.ndarray` or `DenseTensor`, but received {type(value)}."
2987 )
2988

Callers 15

_fuse_conv_bn_evalFunction · 0.45
_fuse_linear_bn_evalFunction · 0.45
loadMethod · 0.45
prune_model_by_layerMethod · 0.45
set_state_dictMethod · 0.45
set_state_dictMethod · 0.45
__init__Method · 0.45
__init__Method · 0.45
_quant_weightsMethod · 0.45
forwardMethod · 0.45
forwardMethod · 0.45

Calls 15

TypeErrorClass · 0.85
global_scopeFunction · 0.85
ValueErrorClass · 0.85
listFunction · 0.85
CPUPlaceMethod · 0.80
XPUPinnedPlaceMethod · 0.80
XPUPlaceMethod · 0.80
CustomPlaceMethod · 0.80
CUDAPlaceMethod · 0.80
typeFunction · 0.50
get_tensorMethod · 0.45
shapeMethod · 0.45

Tested by

no test coverage detected