- "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!