Programmatically modify the hyperparameters of a deep network layerGraph

1 次查看(过去 30 天)
I have an un-initialized layerGraph object for a CNN. I wish to modify all of the convolutional layers in the CNN so that the NumFilters properties are reduced by half, but otherwise keep the graph connections and everything else the same. I have many layers, so doing this manually using deepnetworkDesigner would be tedious.
Doing it programmatically, however, is presenting obstacles, because the NumFilters and other hyperparameters of the layer objects in the graph are read-only. Also, the Layers property of the layerGraph object is read-only and it is not clear how to recreate a layerGraph from its known Layers and Connections properties.
Is it supposed to be this hard? Is there an easy way to change network hyperparameters programmatically?

回答(1 个)

Neha
Neha 2023-11-20
Hi Matt,
I understand that you want to programmatically set the "NumFilters" property of all the convolution layers of the layerGraph object. Since it's a read-only property, as a workaround you can recreate a new layerGraph object and add layers to it while modifying the "NumFilters" property using the "addLayers" function. You can then add connections to the new layerGraph object using the "connectLayers" function. Please refer to the following code snippet:
% Create a new layerGraph
newLayerGraph = layerGraph();
% Iterate through the layers of the original layerGraph
for i = 1:numel(layerGraphObj.Layers)
if isa(layerGraphObj.Layers(i), 'nnet.cnn.layer.Convolution2DLayer')
% Modify the NumFilters property
modifiedConvLayer = convolution2dLayer(layerGraphObj.Layers(i).FilterSize, ...
layerGraphObj.Layers(i).NumFilters/2, ...
'Name', layerGraphObj.Layers(i).Name,...
'Padding',layerGraphObj.Layers(i).PaddingMode); % Add other properties used in the original layerGraph
newLayerGraph = addLayers(newLayerGraph, modifiedConvLayer);
else
% Add non-convolutional layers as they are
newLayerGraph = addLayers(newLayerGraph, layerGraphObj.Layers(i));
end
end
% Reconnect the layers
for i = 1:numel(layerGraphObj.Connections.Source)
newLayerGraph = connectLayers(newLayerGraph, layerGraphObj.Connections.Source{i}, ...
layerGraphObj.Connections.Destination{i});
end
Please refer to the following documentation links for more information on the functions used in the above code snippet:
  1. "addLayers": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.addlayers.html
  2. "connectLayers": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.connectlayers.html
Hope this helps!
  1 个评论
Matt J
Matt J 2023-11-22
编辑:Matt J 2023-11-22
No, that doesn't work, because of "Add other properties used in the original layerGraph"
To do that, you need to somehow retrieve from the layer the constructor arguments that the layer was originally built with, and there appears to be no way to retrieve that except manually. The documentation doesn't even spell out the complete list of Name-Value pair arguments that can be set in the constructor. And if it did, there are settings like PaddingMode and PaddingSize that don't have a clear Name-Value input pair that sets them.
What is needed is for the layer objects to have a set() method.

请先登录,再进行评论。

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by