Create parameters for this layer. Parameters: shape(list): Shape of the parameter. The data type in the list must be int. attr(ParamAttr, optional): Parameter attribute of weight. Please refer to :ref:`api_paddle_ParamAttr`. Default: None. dtype(str, opti
(
self,
shape: ShapeLike,
attr: ParamAttrLike | None = None,
dtype: DTypeLike | None = None,
is_bias: bool = False,
default_initializer: Initializer | None = None,
device: PlaceLike | None = None,
)
| 1010 | return hook_remove_helper |
| 1011 | |
| 1012 | def create_parameter( |
| 1013 | self, |
| 1014 | shape: ShapeLike, |
| 1015 | attr: ParamAttrLike | None = None, |
| 1016 | dtype: DTypeLike | None = None, |
| 1017 | is_bias: bool = False, |
| 1018 | default_initializer: Initializer | None = None, |
| 1019 | device: PlaceLike | None = None, |
| 1020 | ) -> Tensor: |
| 1021 | """Create parameters for this layer. |
| 1022 | |
| 1023 | Parameters: |
| 1024 | shape(list): Shape of the parameter. The data type in the list must be int. |
| 1025 | attr(ParamAttr, optional): Parameter attribute of weight. Please refer to :ref:`api_paddle_ParamAttr`. Default: None. |
| 1026 | dtype(str, optional): Data type of this parameter. |
| 1027 | If set str, it can be "bool", "float16", "float32", "float64", |
| 1028 | "int8", "int16", "int32", "int64", "uint8" or "uint16". Default: "float32". |
| 1029 | is_bias(bool, optional): if this is a bias parameter. Default: False. |
| 1030 | default_initializer(Initializer, optional): the default initializer for this parameter. |
| 1031 | If set None, default initializer will be set to paddle.nn.initializer.Xavier and paddle.nn.initializer.Constant |
| 1032 | for non-bias and bias parameter, respectively. Default: None. |
| 1033 | device(PlaceLike, optional): the device place for the parameter. Default: None. |
| 1034 | |
| 1035 | Returns: |
| 1036 | :Tensor, created parameter. |
| 1037 | |
| 1038 | Examples: |
| 1039 | .. code-block:: pycon |
| 1040 | |
| 1041 | >>> import paddle |
| 1042 | >>> paddle.seed(2023) |
| 1043 | |
| 1044 | >>> class MyLayer(paddle.nn.Layer): |
| 1045 | ... def __init__(self): |
| 1046 | ... super().__init__() |
| 1047 | ... self._linear = paddle.nn.Linear(1, 1) |
| 1048 | ... w_tmp = self.create_parameter([1, 1]) |
| 1049 | ... self.add_parameter("w_tmp", w_tmp) |
| 1050 | ... |
| 1051 | ... def forward(self, input): |
| 1052 | ... return self._linear(input) |
| 1053 | >>> mylayer = MyLayer() |
| 1054 | >>> for name, param in mylayer.named_parameters(): |
| 1055 | ... print(name, param) # will print w_tmp,_linear.weight,_linear.bias |
| 1056 | w_tmp Parameter containing: |
| 1057 | Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=False, |
| 1058 | [[0.06979191]]) |
| 1059 | _linear.weight Parameter containing: |
| 1060 | Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=False, |
| 1061 | [[1.26729357]]) |
| 1062 | _linear.bias Parameter containing: |
| 1063 | Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=False, |
| 1064 | [0.]) |
| 1065 | """ |
| 1066 | temp_attr = copy.deepcopy(attr) |
| 1067 | if isinstance(temp_attr, str) and temp_attr == "": |
| 1068 | temp_attr = None |
| 1069 | return self._helper.create_parameter( |