MATLAB Answers

What to set 'ClassNames' to when using importKerasNetwork() to import network and weights for a regression model?

16 views (last 30 days)
MM on 19 Dec 2018
Commented: Ben Wetherill on 28 Aug 2019
I have trained a regression model in Keras/Tensorflow and exported the network achitecure and weights to .json and .h5 files. I would like to import my regression model to Matlab (R2018a) and looks like importKerasNetwork() does just that. However, it keeps throwing the following error: "Reference to non-existent field 'class_name'." I don't pass any 'Classes' (R2018b) or 'ClassNames' (R2018a) becasue I'm doing regression not classification. This is the how I call the function:
net = importKerasNetwork('model_architecture.json','WeightFile','my_model_weights.h5', 'OutputLayerType','regression');
The documentation (R2018b as well as R2018a) doesn't state what to pass in 'Classes' when you are dealing with a regression problem. I tried passing an empty array but got the same error. Any ideas how to solve this problem?


Sign in to comment.

Accepted Answer

Don Mathis
Don Mathis on 20 Dec 2018
There were a few updates to that support package after the initial release. Try downloading it and installing it again through the Add-Ons icon in MATLAB. Also if you can upload your model files here I can take a look.


Show 9 older comments
Yousaf Rahman
Yousaf Rahman on 3 Apr 2019
I am having the same issue, I am using the latest version of the importer (downloaded 4/2/2019), but I am using MATLAB 2017b. I had trouble installing the importer in 2018b, and have a Mathworks Support case ongoing for that.

Sign in to comment.

More Answers (2)

Sriramkrishnan Muralikrishnan
I have the same problem with importing
net = importKerasNetwork(modelfile,'OutputLayerType','regression')
Error using importKerasNetwork (line 77)
Reference to non-existent field 'class_name'
I am using R2017b and just installed the add on for Keras. Could you please let me know where I can find the update which might resolve this problem.

  1 Comment

MM on 2 Mar 2019
Click on the little drop-down arrow on the Add-Ons icon --> Check for updates --> Add-on fetaures. This usually worked for me.

Sign in to comment.

Ben Wetherill
Ben Wetherill on 27 Aug 2019
Edited: Ben Wetherill on 27 Aug 2019
Hello. I'm also having this issue. I recently downloaded the tool for 2017b via the offline installation method. After some digging and use of HDFViewer I can see the issue.
My NN is built as follows:
model = Sequential()
model.add(Dense(200, input_shape=(input_size,), activation='relu'))
model.add(Dense(160, activation='relu'))
model.add(Dense(output_size, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
history =, output_data_train, validation_data = (input_data_test,output_data_test),
epochs=100, batch_size=64)
output_data_pred = model.predict(input_data_test)'unvalidated_input_data.h5')
Calling importKerasLayers() from the Matlab prompt leads to the following line in KerasLayerInsideSequentialModel.m throwing the error.
this.ClassName = Struct.class_name;
When I look at the contents of the struct at this point, I'm dealing with an object called 'name' with a value of 'sequential_9'. This was easy to find in HDFViewer. See below for the object dump.
{"class_name": "Sequential", "config": {"name": "sequential_9", "layers": [{"class_name": "Dense", "config": {"name": "dense_24", "trainable": true, "batch_input_shape": [null, 248], "dtype": "float32", "units": 62, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_25", "trainable": true, "units": 108, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}}
So the cause is clear, but I don't know if its my version of the importer or the way I've setup the model in Keras. From this thread others have had a similar issue but have fixed it with a tool update. My version should include this update. I'm using Keras version 2.2.4.
Any thoughts?


Don Mathis
Don Mathis on 27 Aug 2019
I'm pretty sure this is due to a breaking change made by Keras in version 2.2.3 that changed their representation of Sequential models. That change was made in late 2018. The earliest Keras importer version that supports the breaking change was R2018b. You could try to save the Keras model in a version earlier than 2.2.3.
Ben Wetherill
Ben Wetherill on 28 Aug 2019
Yup, that fixed it. Well, I say fixed it - now I have an issue in that the binary_crossentropy loss function isn't implemented in the 2017b version of the importer and my IT department hasn't released 2018b yet. :/ Yay for Mordac, The Preventer of Information Services. ;)

Sign in to comment.

Sign in to answer this question.