When I imported Pretrained Keras model into the Matlab windowing size don't remain same?

1 view (last 30 days)
Hi,
I've trained the model with window size 50, and my model input is (num_window,50,5). I've saved the model .h5 extended and imported it into the MATLAB with importKerasNetwork func. But when i use the window size 50 in MATLAB, following error is raising
'Error using DAGNetwork/predict
The prediction sequences are of feature dimension 50 but the input layer expects sequences of feature dimension 5.'
What could be reason? Please help.

Answers (1)

Jaimin
Jaimin on 19 Sep 2024
Based on the model description, I have created a sample model using Keras (version 2.6.0), which is as follows.
from keras.models import Sequential
from keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(64, input_shape=(50, 5), return_sequences=True))
model.add(LSTM(32))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.save('model.h5')
It seems the error occurred because the model expects input with dimensions (50, 5), but the input was provided with dimensions (num_window, 50, 5). I have found a workaround to resolve the given error. You can iterate through each window one by one, so the model input becomes (50, 5).
Here I have attach a code snippet for better understanding.
net = importKerasNetwork('model.h5');
% Define the dimensions
num_window = 100; % You can change this to the desired number of windows
% Generate random data
% This will create a 3D array with dimensions (num_window, 50, 5)
data = rand(num_window, 5, 50);
% Display the size of the generated data to confirm
disp(size(Data));
% Make predictions using the imported Keras model
for i=1:num_window
predictions = predict(net, reshape(data(i,:,:),5,50))
end
I hope this will be helpful.

Community Treasure Hunt

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

Start Hunting!