Parameter

Ascend GPU CPU 入门

在线运行下载Notebook下载样例代码查看源文件

Parameter是变量张量,代表在训练网络时,需要被更新的参数,一般包括权重weight和偏置bias

例如,对于卷积算子nn.Conv2Dweight[out_channel, in_channel, kernel_size, kernel_size]大小的张量, bias[out_channel]大小的向量。而对于全连接操作nn.Denseweight[out_channel, in_channel]大小的张量, bias[out_channel]大小的向量。

import numpy as np
from mindspore import Tensor
from mindspore import nn

conv_net = nn.Conv2d(in_channels=120, out_channels=240, kernel_size=4, has_bias=True, weight_init='normal')
dense_net = nn.Dense(in_channels=3, out_channels=4, weight_init='normal', bias_init='zeros', has_bias=True)

print("conv_net, weight shape:", conv_net.weight.shape, ", bias shape:", conv_net.bias.shape)
print("dense_net, weight_shape:", dense_net.weight.shape, ", bias shape:", dense_net.bias.shape)
conv_net, weight shape: (240, 120, 4, 4) , bias shape:  (240,)
dense_net, weight_shape: (4, 3) , bias shape:  (4,)

nn下的算子接口中一般集成了权重和偏置,用户可以指定初始化方式来初始化这些Parameter

在网络训练过程中,优化器根据反向传播计算出的梯度不断更新Parameter的值,最优的Parameter最终会被保存下来供推理使用。

Parameter不支持稀疏矩阵,如mindspore.RowTensormindspore.SparseTensor。其初始化和更新的详细介绍可参考参数初始化参数更新