- "addLayers": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.addlayers.html
- "connectLayers": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.connectlayers.html
Programmatically modify the hyperparameters of a deep network layerGraph
3 次查看(过去 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?
0 个评论
回答(1 个)
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:
Hope this helps!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!