Programmatically modify the hyperparameters of a deep network layerGraph

조회 수: 3 (최근 30일)
Matt J
Matt J 2023년 11월 15일
편집: Matt J 2023년 11월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by