addParameter
将参数添加到ONNXParameters
目的
句法
Description
params = addParameter(
添加由params
,,,,姓名
,,,,value
,,,,type
)姓名
,,,,value
,,,,andtype
至theONNXParameters
目的params
。The returnedparams
对象包含输入参数的模型参数params
至gether with the added parameter, stacked sequentially. The added parameter姓名
must be unique, nonempty, and different from the parameter names inparams
。
Examples
将参数添加到导入的ONNX模型功能
Import a network saved in the ONNX format as a function and modify the network parameters.
Import the pretrainedSimpleNet3fc.onnx
网络作为函数。SimpleNet3FC
is a simple convolutional neural network trained on digit image data. For more information on how to create a network similar toSimpleNet3FC
, 看创建简单的图像分类网络。
ImportSimpleNet3fc.onnx
使用experconnxfunction
,返回ONNXParameters
包含网络参数的对象。该函数还会在包含网络体系结构的当前文件夹中创建一个新的模型函数。将模型函数的名称指定为simplenetFcn
。
params = emumentOnnxFunction('simplenet3fc.onnx',,,,'simplenetFcn');
包含导入的ONNX网络的函数已保存到文件SimpleNetfcn.m。要了解如何使用此功能,请键入:帮助SimpleEnetFCN。
显示在培训期间更新的参数(params.learnables
)and the parameters that remain unchanged during training (参数
)。
params.learnables
ans =struct with fields:imageInput_mean:[1×1 dlarray] conv_w:[5×5×1×20 dlarray] conv_b:[20×1 dlarray] batchnorm_scale:[20×1 dlarray] batchnorm_b:[20×1 dlarray] fc_1_w:[24×24×24×20×20 dlarray] fc_1_b:[20×1 dlarray] fc_2_w:[1×1×1×20×20 dlarray] fc_2_b:[20×1 dlarray] fc_3_w:[1×1×1×1×20×10×10×10 dlarray]×1 dlarray]
参数
ans =struct with fields:ConvStride1004: [2×1 dlarray] ConvDilationFactor1005: [2×1 dlarray] ConvPadding1006: [4×1 dlarray] ConvStride1007: [2×1 dlarray] ConvDilationFactor1008: [2×1 dlarray] ConvPadding1009: [4×1 dlarray] ConvStride1010:[2×1 dlarray] ConvDilationFactor1011: [2×1 dlarray] ConvPadding1012: [4×1 dlarray] ConvStride1013: [2×1 dlarray] ConvDilationFactor1014: [2×1 dlarray] ConvPadding1015: [4×1 dlarray]
The network has parameters that represent three fully connected layers. You can add a fully connected layer in the original parametersparams
在两层之间fc_2
andfc_3
。The new layer might increase the classification accuracy.
To see the parameters of the convolutional layersfc_2
andfc_3
,打开模型功能simplenetFcn
。
打开simplenetFcn
Scroll down to the layer definitions in the functionsimplenetFcn
。The code below shows the definitions for layersfc_2
andfc_3
。
%cons:[weights, bias, stride, dilationFactor, padding, dataFormat, NumDims.fc_2] = prepareConvArgs(Vars.fc_2_W, Vars.fc_2_B, Vars.ConvStride1010, Vars.ConvDilationFactor1011, Vars.ConvPadding1012, 1, NumDims.fc_1, NumDims.fc_2_W); Vars.fc_2 = dlconv(Vars.fc_1, weights, bias,“大步”,,,,stride,'DilationFactor',,,,dilationFactor,'Padding',,,,padding,'DataFormat',,,,dataFormat);%cons:[权重,偏见,大步,扩张量,填充,填充,dataFormat,numdims.fc_3] = PrepareConvargs(vars.fc_3_w,vars.fc_3_b,vars.convstride1013,vars.convdilationfactor1014;vars.fc_3 = dlconv(vars.fc_2,striges,bias,“大步”,,,,stride,'DilationFactor',,,,dilationFactor,'Padding',,,,padding,'DataFormat',,,,dataFormat);
命名新层fc_4
,,,,because each added parameter name must be unique. TheaddParameter
function always adds a new parameter sequentially to theparams.learnables
or参数
structure. The order of the layers in the model functionsimplenetFcn
determines the order in which the network layers are executed. The names and order of the parameters do not influence the execution order.
Add a new fully connected layerfc_4
具有与fc_2
。
params = addParameter(params,'fc_4_W',,,,params.learnables。fc_2_W,“可学习”);params = addParameter(params,'fc_4_B',,,,params.learnables。fc_2_B,“可学习”);params = addParameter(params,'fc_4_stride',,,,参数。ConvStride1010,“不可检测”);params = addParameter(params,'fc_4_DilationFactor',,,,参数。ConvDilationFactor1011,“不可检测”);params = addParameter(params,'fc_4_padding',,,,参数。ConvPadding1012,“不可检测”);
显示更新的可学习和不可学习的参数。
params.learnables
ans =struct with fields:imageInput_mean:[1×1 dlarray] conv_w:[5×5×1×20 dlarray] conv_b:[20×1 dlarray] batchnorm_scale:[20×1 dlarray] batchnorm_b:[20×1 dlarray] fc_1_w:[24×24×24×20×20 dlarray] fc_1_b:[20×1 dlarray] fc_2_w:[1×1×1×20×20 dlarray] fc_2_b:[20×1 dlarray] fc_3_w:[1×1×1×1×20×10×10×10 dlarray]×1 dlarray] fc_4_w:[1×1×20×20 dlarray] fc_4_b:[20×1 dlarray]
参数
ans =struct with fields:ConvStride1004: [2×1 dlarray] ConvDilationFactor1005: [2×1 dlarray] ConvPadding1006: [4×1 dlarray] ConvStride1007: [2×1 dlarray] ConvDilationFactor1008: [2×1 dlarray] ConvPadding1009: [4×1 dlarray] ConvStride1010:[2×1 dlarray] ConvdilationFactor1011:[2×1 dlarray] Convpadding1012:[4×1 dlarray] Convstride1013:[2×1 dlarray] ConvdilationFactor1014:[2×1 Dlarray] Corvpadding1115:[4×1 Dlarray:[4×1 Dlarray] fc_4;×1 dlarray] fc_4_dilationFactor:[2×1 dlarray] fc_4_padding:[4×1 dlarray]
修改模型功能的体系结构以反映params
因此,您可以将网络使用新参数进行预测或重新训练网络。打开模型功能simplenetFcn
。然后,添加完全连接的层fc_4
在两层之间fc_2
andfc_3
,并更改卷积操作的输入数据dlconv
对于层fc_3
至Vars.fc_4
。
打开simplenetFcn
The code below shows the new layerfc_4
在其位置和层fc_2
andfc_3
。
%cons:[weights, bias, stride, dilationFactor, padding, dataFormat, NumDims.fc_2] = prepareConvArgs(Vars.fc_2_W, Vars.fc_2_B, Vars.ConvStride1010, Vars.ConvDilationFactor1011, Vars.ConvPadding1012, 1, NumDims.fc_1, NumDims.fc_2_W); Vars.fc_2 = dlconv(Vars.fc_1, weights, bias,“大步”,,,,stride,'DilationFactor',,,,dilationFactor,'Padding',,,,padding,'DataFormat',,,,dataFormat);% Conv[weights, bias, stride, dilationFactor, padding, dataFormat, NumDims.fc_4] = prepareConvArgs(Vars.fc_4_W, Vars.fc_4_B, Vars.fc_4_Stride, Vars.fc_4_DilationFactor, Vars.fc_4_Padding, 1, NumDims.fc_2, NumDims.fc_4_W); Vars.fc_4 = dlconv(Vars.fc_2, weights, bias,“大步”,,,,stride,'DilationFactor',,,,dilationFactor,'Padding',,,,padding,'DataFormat',,,,dataFormat);%cons:[权重,偏见,大步,扩张性,填充,填充,dataFormat,numdims.fc_3] = PrepareConvargs(vars.fc_3_w,vars.fc_3_b,vars.convstride1013,vars.convdilationfactor1014;vars.fc_3 = dlconv(vars.fc_4,strige,bias,“大步”,,,,stride,'DilationFactor',,,,dilationFactor,'Padding',,,,padding,'DataFormat',,,,dataFormat);
Input Arguments
params
-网络参数
ONNXParameters
目的
网络参数,指定为ONNXParameters
目的。params
包含导入ONNX™模型的网络参数。
姓名
-Name of parameter
character vector|字符串标量
参数的名称,指定为字符向量或字符串标量。
Example:'conv2_w'
Example:“ conv2_padding'
value
-Value of parameter
数字阵列|character vector|字符串标量
参数的值,指定为数字阵列,字符向量或字符串标量。复制现有网络层(存储在params
),复制网络层的参数值。
Example:params.learnables。conv1_W
Example:参数。conv1_Padding
Data Types:单身的
|double
|char
|细绳
type
-参数类型
“可学习”
|“不可检测”
|'状态'
参数类型,,,,specified as“可学习”
,,,,“不可检测”
, 或者'状态'
。
价值
“可学习”
指定一个参数,及更新rk during training (for example, weights and bias of convolution).价值
“不可检测”
specifies a parameter that remains unchanged during network training (for example, padding).价值
'状态'
指定一个参数,其中包含迭代之间网络记住的信息,并在多个培训批次上进行更新。
Data Types:char
|细绳
数值
-每个参数的尺寸数量
structure
每个参数的尺寸数量,,,,specified as a structure.数值
包括尾随的单身尺寸。
Example:params.numdimensions.conv1_w
Example:4
输出参数
params
-网络参数
ONNXParameters
目的
网络参数,返回ONNXParameters
目的。params
包含由addParameter
。
版本历史记录
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
选择一个网站以获取可用的翻译内容,并查看本地事件和优惠。根据您的位置,我们建议您选择:。
You can also select a web site from the following list:
如何获得最佳网站性能
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
Americas
- América Latina(Español)
- Canada(English)
- United States(English)