Undefined function handle error when loading a neural network that contain a function layer

Hi everyone,
I created a neural network that uses a functionLayer that I defined by :
functionLayer(@Feature_wise_LM,Name="FiLM_64",Formattable=1,NumInputs=2,NumOutputs=1)
However, when I save the trained neural network with :
save("./trained_networks/FiLM", "trained_network");
and reload it in the same script with :
trained_network = load("./trained_networks/FiLM.mat");
trained_network = trained_network.trained_network;
I get the error :
Warning: While loading an object of class 'dlnetwork':
Error using nnet.internal.cnn.layer.GraphExecutor/propagate (line 354)
Execution failed during layer(s) 'FiLM_64'.
Error in deep.internal.network.ExecutableNetwork/configureForInputsAndForwardOnLayer (line 347)
propagate(this, fcn, Xs, outputLayerIdx, outputLayerPortIdx);
Error in deep.internal.network.EditableNetwork/convertToDlnetwork (line 101)
[executableNetwork, layerOutputSizes] = configureForInputsAndForwardOnLayer(...
Error in dlnetwork.loadobj (line 741)
net = convertToDlnetwork(privateNet, exampleInputs, initializeNetworkWeights);
Caused by:
Undefined function handle.
Error in nnet.cnn.layer.FunctionLayer/predict (line 61)
[varargout{1:layer.NumOutputs}] = layer.PredictFcn(varargin{:});
I tried to save the function in a dedicated file "Feature_wise_LM.m" , but it didn't work

Answers (1)

I tried to save the function in a dedicated file "Feature_wise_LM.m" , but it didn't work
It should work. Make sure the location of that file is on the Matlab path.
Make sure as well that it is in the same location as when the functionLayer was created, though I'm not sure it is absolutely necessary.

6 Comments

Hello Matt,
Thank you for your answer!
I am quite sure that it doesn't work like this. The only workaround I found is that if I put
functionLayer(@(X1,X2) Feature_wise_LM(X1,X2),Name="FiLM_64",Formattable=1,NumInputs=2,NumOutputs=1)
it for some reason works.
However, a complete training of the model takes a very very long time, so I wonder if there is a way to recover the saved model that I trained by declaring the layer as
functionLayer(@Feature_wise_LM,Name="FiLM_64",Formattable=1,NumInputs=2,NumOutputs=1)
The problem has to be something in your environment. Here it works fine:
net=dlnetwork(functionLayer((@testFun),Name="FiLM_64",Formattable=1,NumInputs=2,NumOutputs=1), ...
dlarray(1,'SSC'),dlarray(2,'SSC'));
save('netfile.mat', 'net')
clear
load('netfile.mat', 'net')
net,
net =
dlnetwork with properties: Layers: [1×1 nnet.cnn.layer.FunctionLayer] Connections: [0×2 table] Learnables: [0×3 table] State: [0×3 table] InputNames: {'FiLM_64/in1' 'FiLM_64/in2'} OutputNames: {'FiLM_64'} Initialized: 1 View summary with summary.
function Y=testFun(X1,X2)
Y=X1+X2;
end
Hello Matt,
The code indeed works on matlab online.
I tested it on my personal computer, it worked well, and it even worked when I replaced
clear
by
clear all
However, if I turn matlab off, and reopen it, I have the usual error when executing
load('netfile.mat', 'net')
If so, it means your Matlab path changes when you close and reopen Matlab.
What happens, after reopening Matlab, when you do,
which Feature_wise_LM
from the command line?
It does give me the right path...
However, in your example the function is in the same script, and when defining the layer by "@(X1,X2) Feature_wise_LM(X1,X2)", it works even when turning matlab off and reopening it, so I think that matlab can access the function
However, in your example the function is in the same script.
The example didn't require that. Here it is again, with testFun defined in its own mfile, as you claim you are doing. It still works, even without wrapping the handle in an anonymous function:
net=dlnetwork(functionLayer(@testFun,Name="FiLM_64",Formattable=1,NumInputs=2,NumOutputs=1), ...
dlarray(1,'SSC'),dlarray(2,'SSC'));
save('netfile.mat', 'net')
clear
load('netfile.mat', 'net')
net,
net =
dlnetwork with properties: Layers: [1×1 nnet.cnn.layer.FunctionLayer] Connections: [0×2 table] Learnables: [0×3 table] State: [0×3 table] InputNames: {'FiLM_64/in1' 'FiLM_64/in2'} OutputNames: {'FiLM_64'} Initialized: 1 View summary with summary.

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 26 Jun 2025

Edited:

on 27 Jun 2025

Community Treasure Hunt

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

Start Hunting!