Programmatically determine which Deep Learning layer properties contain learnables

조회 수: 9 (최근 30일)
In the Deep Learning Toolbox, there are a variety of layer object types. The display methods of these objects indicate which object properties contain learnable parameter data. For example, for LSTMLayer objects, the learnable parameters are stored in the properties "InputWeights", "RecurrentWeights", and "Bias" as shown below. My question is, is there a way, given a layer object, to programmatically determine the subset of its properties that are learnable?
layer = lstmLayer(100,'Name','lstm1')
layer =
LSTMLayer with properties: Name: 'lstm1' InputNames: {'in'} OutputNames: {'out'} NumInputs: 1 NumOutputs: 1 HasStateInputs: 0 HasStateOutputs: 0 Hyperparameters InputSize: 'auto' NumHiddenUnits: 100 OutputMode: 'sequence' StateActivationFunction: 'tanh' GateActivationFunction: 'sigmoid' Learnable Parameters InputWeights: [] RecurrentWeights: [] Bias: [] State Parameters HiddenState: [] CellState: [] Use properties method to see a list of all properties.

채택된 답변

Pratyush Swain
Pratyush Swain 2023년 12월 14일
Hi Matt,
I understand you want to access the learnable properties of a deep learning layer object. There is no direct way fetch the trainable properties of a layer but a workaround can be to initialize a deep learning layer from the "lstm" layer object and then fetch its learnable parameters.
Please refer to the below example implementation:
% Define a lstm layer %
layer = lstmLayer(100,'Name','lstm1')
layer =
LSTMLayer with properties: Name: 'lstm1' InputNames: {'in'} OutputNames: {'out'} NumInputs: 1 NumOutputs: 1 HasStateInputs: 0 HasStateOutputs: 0 Hyperparameters InputSize: 'auto' NumHiddenUnits: 100 OutputMode: 'sequence' StateActivationFunction: 'tanh' GateActivationFunction: 'sigmoid' Learnable Parameters InputWeights: [] RecurrentWeights: [] Bias: [] State Parameters HiddenState: [] CellState: [] Use properties method to see a list of all properties.
% Define a deep learning network %
net = dlnetwork(layer,Initialize=false);
% Retreive the learnable properties %
properties = net.Learnables;
% Display the learnable properties %
disp(properties)
Layer Parameter Value _______ __________________ ____________ "lstm1" "InputWeights" {0×0 double} "lstm1" "RecurrentWeights" {0×0 double} "lstm1" "Bias" {0×0 double}
As we can observe, the "dlnetwork" forms a single layer network named as "lstm1" and we have successfully retreived its learnable properties as a table.
For more information , please refer to:
Hope this helps.
  댓글 수: 2
Matt J
Matt J 2023년 12월 14일
In order to use this technique, though, I must programmatically determine if the layer is a type of output layer, because dlnetworks may not contain output layers. Is there a way to detect that?

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

추가 답변 (1개)

Vishnu Keyen
Vishnu Keyen 2024년 11월 8일
Let's define the a network
layers = [sequenceInputLayer(32, 'Name', 'input')
lstmLayer(128, 'OutputMode', 'sequence', 'Name', 'lstm')]
layers =
2x1 Layer array with layers: 1 'input' Sequence Input Sequence input with 32 dimensions 2 'lstm' LSTM LSTM with 128 hidden units
since each layer is a nnet.cnn.layer type object, one can determine
you can determine the object type using class() command.
For example,
layerType = class(layers(2))
layerType = 'nnet.cnn.layer.LSTMLayer'
And then you can search for a match in that class for the type you are looking for
For example
contains(class(layers(2)),'lstm','IgnoreCase',true)
ans = logical
1
you can check for specific parameters like RecurrentWeights once you identify its an LSTM, or a GRU layer..

카테고리

Help CenterFile Exchange에서 Build Deep Neural Networks에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by