Error while running importKerasNetwork

3 views (last 30 days)
Hello,
I have a Matlab script (version 2020b) in which I import a neural network from Python using the next command:
importKerasNetwork(modelName)
Before the script was working well. After reinstalling my Matlab (same version, 2020b), I get the following error:
SWITCH expression must be a scalar or a character vector.
Error in nnet.internal.cnn.keras.LayerSpec>DAGOutputLayer (line 166)
switch Loss
Error in nnet.internal.cnn.keras.LayerSpec/maybeAppendOutputLayer (line 148)
NNTLayers{end+1} = DAGOutputLayer(OutputTensors{pos}, TrainingConfig, isRNN);
Error in nnet.internal.cnn.keras.LayerSpec/translateDAGLayer (line 64)
NNTLayers = maybeAppendOutputLayer(this, NNTLayers, OutputTensors, TrainingConfig, isRNN);
Error in
nnet.internal.cnn.keras.AssembledModel>@(LSpec)translateDAGLayer(LSpec,AM.OutputTensors,AM.TrainingConfig,TranslateWeights,TranslateTrainingParams,UserImageInputSize,isRNN(AM))
(line 226)
NNTLayerGroups = cellfun(@(LSpec)translateDAGLayer(LSpec, AM.OutputTensors, AM.TrainingConfig, TranslateWeights,...
Error in nnet.internal.cnn.keras.AssembledModel/translateDAGModel (line 226)
NNTLayerGroups = cellfun(@(LSpec)translateDAGLayer(LSpec, AM.OutputTensors, AM.TrainingConfig, TranslateWeights,...
Error in nnet.internal.cnn.keras.AssembledModel/translateAssembledModel (line 57)
Layers = translateDAGModel(this, TranslateWeights, TranslateTrainingParams, UserImageInputSize);
Error in nnet.internal.cnn.keras.importKerasNetwork (line 30)
LayersOrGraph = translateAssembledModel(AM, TrainingConfig, ImportWeights, TrainOptionsRequested, UserImageInputSize);
Error in importKerasNetwork (line 91)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
The error is given in a line of a scrpit that I haven't programmed. Could anyone help me?
Thank you!

Answers (1)

David Willingham
David Willingham on 5 Nov 2021
Hi,
This has been observed before. I don't believe it's due to reinstalling MATLAB.
Please check if the following work around will work:
==
If one provides the loss value using a Python list (shown below), this issue will occur:
model.compile(loss=['categorical_crossentropy'],
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
Although Keras accepts a list for the loss input, this type is not mentioned as one of the accepted type in Keras documentation https://keras.io/losses/.
If the loss is specified this way the problem should go away:
model.compile(loss='mean_squared_error')
==
  1 Comment
Irati Ibañez Hidalgo
Irati Ibañez Hidalgo on 5 Nov 2021
My keras model has the loss defined like:
model.compile(
optimizer=optimizer,
loss=['mean_squared_error'],
metrics = ['mse'])ç
Is it the problem?
Anyway, what I don't understand is why before the reinstallation the script was working with the same neurla network that I'm trying to upload now and now it isn't working.
I changed the line 166 in this file nnet.internal.cnn.keras.LayerSpec > DAGOutputLayer to:
switch Loss{1}
And after that it seems to work correctly.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!