Size of input variables in series network are different from the original keras network
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Ayush
2023-10-6
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!