how to extract the learned features from a dlnetwork

I wanted to extract the feature vector of training and testing data from a dlnetwork by using the activations and forward functions, matlab show me this error with any format of training and testing data, Incorrect number or types of inputs or outputs for function activations or forward function.
featureSet = forward(net,trainData,net.Layers(181).Name,'OutputAs','columns');
featureSet = activations(net,dltrainData,net.Layers(181).Name);
is there another running instruction with another format of training and testing data

6 Comments

Hi @Eline,

Addressing your query regarding, “I wanted to extract the feature vector of training and testing data from a dlnetwork by using the activations and forward functions, matlab show me this error with any format of training and testing data, Incorrect number or types of inputs or outputs for function activations or forward function.”

After analyzing the error message, it seems that forward and activations functions require specific input types. Here’s how you can resolve this issue. Convert your training and testing data to dlarray format if they are not already. For example:

dltrainData = dlarray(trainData, 'CB'); % 'C' for channels, 'B' for 
batch

Use the correct layer name when calling the functions. Make sure that the layer name you are using (in this case, net.Layers(181).Name) is valid and corresponds to an actual layer in your network. Here is example code snippet to illustrate it.

% Forward pass to get feature vector
featureSet = forward(net, dltrainData, net.Layers(181).Name, 'OutputAs', 
'columns');
% Using activations to extract features
featureSetActivations = activations(net, dltrainData, 
net.Layers(181).Name);

Let me know if these steps helped resolve your problem.

Going back to “is there another running instruction with another format of training and testing data”

Try the solution mentioned above, if the issue still persist, then I will approach to find a solution regarding another running instruction.

@eline I wonder what documentation you and @Umar are reading. There is no 'OutputAs','columns' name-value pair mentioned in the documentation for forward(). The syntax you are using doesn't resemble anything there.
Many thanks
here is the new message when executing the instruction:
dltrainData = dlarray(trainData, 'CB');
dlarray is supported only for full arrays of data type double, single, or logical, or for full gpuArrays of these data types.
Hi @eline,
Have you made any progress, please let us know.
No, I am working on a network that has an arcfaceLayer with two inputs
Hi @Elaine,
If you need any assistance with face recognition task or defining a custom layer that processes both inputs accordingly, please let us know.

Sign in to comment.

Answers (1)

featureSet = forward(net,trainData,'Outputs',net.Layers(181).Name);

2 Comments

matlab still throws this error message :
Incorrect number of network inputs. Network has 2 inputs, but 1 inputs were passed. To determine number and order of inputs, check network 'InputNames' property.
If your network has two inputs, the syntax is supposed to be like this,
featureSet = forward(net,In1,In2,'Outputs',net.Layers(181).Name);
where In1 and In2 are the two network inputs as dlarrays. If trainData is a datastore, you cannot give it to forward() directly. You will have to read a particular In1 and In2 from the datastore and convert them to dlarrays.

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 23 Aug 2024

Commented:

on 25 Aug 2024

Community Treasure Hunt

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

Start Hunting!