Error while running importKerasNetwork
6 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
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')
==
See Also
Categories
Find more on Call Python from MATLAB 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!