Making custom neural network layer with state, doesn't accept unformatted dlarray

Made a custom nnet layer, with a state property. Tried following the documentation on how to pass state etc through the functions.
Unfortunately, when testing the layer with
checkLayer(mweLayer,1);
I get the error
``` Error using 'predict' in Layer mweLayer. 'property' must be a real-valued finite numeric array or an unformatted dlarray object.```
As you can see in this example, even with a small unformatted dlarray, the error is thrown. Am I not allowed dlarrays in state properties at all? Is there somewhere else the error is coming from?
classdef mweLayer < nnet.layer.Layer
Illegal use of reserved keyword "classdef".
properties (State)
property
end
methods
function layer = mweLayer(name)
arguments
name = "mweLayer"
end
layer.Name = name;
layer.property = dlarray([1 2 3]);
end
function [Z,state] = predict(layer,X)
Z = X;
state.property = dlarray([1 2 3]);
end
function layer = resetState(layer)
layer.property = dlarray([1 2 3]);
end
end
end

 Accepted Answer

It's not an issue with the dlarray formatting. It is an issue with the predict() syntax, which should be,
function [Z,property] = predict(layer,X)
Z = X;
property = dlarray([1 2 3]);
end

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 9 Nov 2026 at 16:22

Edited:

on 9 Nov 2026 at 20:16

Community Treasure Hunt

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

Start Hunting!