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)
| 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 |
no test coverage detected