删除参数
Remove parameter fromONNXParameters
object
描述
params = removeParameter(
删除由params
,name
)name
来自ONNXParameters
objectparams
。
例子
Remove Parameters from Imported ONNX Model Function
导入以ONNX格式保存的网络作为函数,并修改网络参数。
进口预处理simplenet3fc.onnx
network as a function.simplenet3fc
是一个简单的卷积神经网络,该神经网络对数字图像数据进行了训练。有关如何创建类似于类似的网络的更多信息simplenet3fc
, seeCreate Simple Image Classification Network。
Importsimplenet3fc.onnx
usingimportONNXFunction
, which returns anONNXParameters
object that contains the network parameters. The function also creates a new model function in the current folder that contains the network architecture. Specify the name of the model function asSimpleEnetFCN
。
params = importONNXFunction('SimpleNet3fc.onnx','simplenetFcn');
A function containing the imported ONNX network has been saved to the file simplenetFcn.m. To learn how to use this function, type: help simplenetFcn.
Display the parameters that are updated during training (params.learnables
) and the parameters that remain unchanged during training (params.Nonlearnables
).
params.learnables
ans =带有字段的结构: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]
params.Nonlearnables
ans =带有字段的结构: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] Corvpadding1015:[4×1 Dlarray:[4×1 Dlarray]
The network has parameters that represent three fully connected layers. To see the parameters of the convolutional layersfc_1
,fc_2
, 和fc_3
, open the model functionSimpleEnetFCN
。
openSimpleEnetFCN
向下滚动到功能中的图层定义SimpleEnetFCN
。下面的代码显示了层的定义fc_1
,fc_2
, 和fc_3
。
% Conv:[weights, bias, stride, dilationFactor, padding, dataFormat, NumDims.fc_1] = prepareConvArgs(Vars.fc_1_W, Vars.fc_1_B, Vars.ConvStride1007, Vars.ConvDilationFactor1008, Vars.ConvPadding1009, 1, NumDims.relu1001, NumDims.fc_1_W); Vars.fc_1 = dlconv(Vars.relu1001, weights, bias,'Stride',大步“扩张量”, dilationFactor,'Padding', padding,“ dataformat',dataformat);% Conv:[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,striges,bias,'Stride',大步“扩张量”, dilationFactor,'Padding', padding,“ dataformat',dataformat);% Conv:[权重,偏见,大步,扩张量,填充,填充,dataFormat,numdims.fc_3] = PrepareConvargs(vars.fc_3_w,vars.fc_3_b,vars.convstride1013,vars.convdilationfactor1014;Vars.fc_3 = dlconv(Vars.fc_2, weights, bias,'Stride',大步“扩张量”, dilationFactor,'Padding', padding,“ dataformat',dataformat);
You can remove the parameters of the fully connected layerfc_2
降低计算复杂性。检查上一层的输出尺寸和后续层的输入维度params
。In this case, the output size of the previous layerfc_1
为20,后续层的输入大小fc_3
也是20。
Remove the parameters of layerfc_2
通过使用删除参数
。
params = removeParameter(params,'fc_2_B'); params = removeParameter(params,'fc_2_w'); params = removeParameter(params,'ConvStride1010'); params = removeParameter(params,'ConvDilationFactor1011'); params = removeParameter(params,'ConvPadding1012');
Display the updated learnable and nonlearnable parameters.
params.learnables
ans =带有字段的结构: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_3_w:[1×1×1×20×10 dlarray] fc_3_b:[10×1 dlarray]
params.Nonlearnables
ans =带有字段的结构:ConvStride1004: [2×1 dlarray] ConvDilationFactor1005: [2×1 dlarray] ConvPadding1006: [4×1 dlarray] ConvStride1007: [2×1 dlarray] ConvDilationFactor1008: [2×1 dlarray] ConvPadding1009: [4×1 dlarray] ConvStride1013: [2×1 dlarray] ConvDilationFactor1014: [2×1 dlarray] ConvPadding1015: [4×1 dlarray]
Modify the architecture of the model function to reflect the changes inparams
so you can use the network for prediction with the new parameters or retrain the network. Open the model functionSimpleEnetFCN
。Then, remove the fully connected layerfc_2
, 和change the input data of the convolution operationDLCONV
for layerfc_3
tovars.fc_1
。
openSimpleEnetFCN
The code below shows layersfc_1
和fc_3
。
% Conv:[weights, bias, stride, dilationFactor, padding, dataFormat, NumDims.fc_1] = prepareConvArgs(Vars.fc_1_W, Vars.fc_1_B, Vars.ConvStride1007, Vars.ConvDilationFactor1008, Vars.ConvPadding1009, 1, NumDims.relu1001, NumDims.fc_1_W); Vars.fc_1 = dlconv(Vars.relu1001, weights, bias,'Stride',大步“扩张量”, dilationFactor,'Padding', padding,“ dataformat',dataformat);% Conv:[权重,偏见,大步,扩张量,填充,填充,dataFormat,numdims.fc_3] = PrepareConvargs(vars.fc_3_w,vars.fc_3_b,vars.convstride1013,vars.convdilationfactor1014;vars.fc_3 = dlconv(vars.fc_1,striges,bias,'Stride',大步“扩张量”, dilationFactor,'Padding', padding,“ dataformat',dataformat);
Input Arguments
params
—网络参数
ONNXParameters
object
网络参数, specified as anONNXParameters
目的。params
contains the network parameters of the imported ONNX™ model.
name
—参数的名称
character vector|string scalar
Name of the parameter, specified as a character vector or string scalar.
例子:'conv2_W'
例子:'conv2_Padding'
Output Arguments
params
— Network parameters
ONNXParameters
object
网络参数, returned as anONNXParameters
目的。params
contains the network parameters updated by删除参数
。
Version History
See Also
开放示例
You have a modified version of this example. Do you want to open this example with your edits?
matlab命令
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择一个网站
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:。
您还可以从以下列表中选择一个网站:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
美洲
- AméricaLatina(Español)
- Canada(English)
- 美国(English)
欧洲
- Belgium(English)
- 丹麦(English)
- Deutschland(德意志)
- españa(Español)
- Finland(English)
- 法国(Français)
- 爱尔兰(English)
- 意大利(Italiano)
- Luxembourg(English)
- Netherlands(English)
- 挪威(English)
- Österreich(德意志)
- Portugal(English)
- Sweden(English)
- 瑞士
- 英国(English)