Size of input variables in series network are different from the original keras network

2 views (last 30 days)
Hi!,
I've trained a keras sequential network defined like this:
model = Sequential()
model.add(layers.LSTM(8, input_shape=(10, 4)))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(2))
model.compile(optimizer=opt, loss='mse')
And saved the model in .h5 format. Then I've imported to matlab 2021b using:
net = importKerasNetwork('mymodel.h5')
SeriesNetwork with properties:
Layers: [6×1 nnet.cnn.layer.Layer]
InputNames: {'lstm_3_input'}
OutputNames: {'RegressionLayer_dense_11'}
1 'lstm_3_input' Sequence Input Sequence input with 4 dimensions
2 'lstm_3' LSTM LSTM with 8 hidden units
3 'dense_10' Fully Connected 32 fully connected layer
4 'dense_10_relu' ReLU ReLU
5 'dense_11' Fully Connected 2 fully connected layer
6 'RegressionLayer_dense_11' Regression Output mean-squared-error
The function seems to work properly, but the input size is now 4 intead of a matrix 10x4. I can obtain a prediction of the net, but it only needs a vector of 4 elements:
net.predict([-0.9, -0.9, -1.5, 0.42]')
ans =
1×2 single row vector
-1.4188 0.4475
Is the 'lstm_3_input' layer storing the sequence of data? In Keras a matrix of size (10,4) is needed to get the prediction of the net.
Any ideas?
thanks in advance

Answers (1)

Ayush
Ayush on 6 Oct 2023
The dimensions of the input layer in the imported network might be different from the original Keras model due to differences in the underlying frameworks and their implementations.
In your case, it seems that the input layer in the imported network has been converted to a sequence input layer with 4 dimensions, which is different from the original input shape of (10, 4). This conversion might be a result of the import process from Keras to MATLAB's deep learning framework.
To confirm the exact dimensions and structure of the imported network, you can use the following code:
net = importKerasNetwork('mymodel.h5');
analyzeNetwork(net);
The `analyzeNetwork` function will provide a visual representation of the network architecture, including the dimensions of each layer. This will help you verify the input shape and understand the structure of the imported network more accurately.
If the dimensions of the input layer are not as expected, you might need to reshape your input data to match the dimensions expected by the imported network.
To read more about analyzeNetwork kindly view the following documentation: https://www.mathworks.com/help/deeplearning/ref/analyzenetwork.html?s_tid=doc_ta
Hope this helps!
  1 Comment
Miguel Pagola
Miguel Pagola on 9 Oct 2023
Thanks for the answer.
I've found that matlab changes que input size from [10x4] to a sequence input of [4]. So, simply by changing the input layer to a new sequence input layer with the desired dimensions it works. Then, I import the RNN from Keras, next create a new layer:
input_layer = sequenceInputLayer(4, 'MinLength', 10);
and create a new layerGrapch with the new input_layer and the layers[2:end] of the imported network.
The output of this new network is similar to the one obtained in Python.

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!