Why do I need to add "+1" to the input of my Keras LSTM-model?

1 view (last 30 days)
The LSTM-model is trained in Keras. The input comes from 'datfile'. If the trained net is imported to Matlab and the inputs from 'datfile' are added, the outputs of the layers do not match. If '1' is added to the inputs, then they match. Why must '1' be added for the same results?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 May 2022
Edited: MathWorks Support Team on 31 May 2022
Essentially, a 'wordEmbeddingLayer' acts as a lookup table. We assign each word a single index. And that index is used by the lookup table in order to get the corresponding embedding vector. Now the reason why this +1 is required is due to the fact that MATLAB uses 1-based indexing rather than 0-based indexing like python. Because of this, each index is shifted by 1 when we load in the lookup table, so the input data should also be shifted by 1 to accommodate this.
This has also been documented in the "Tips" section for importKerasNetwork, importKerasLayers, importTensorFlowNetwork, and importTensorFlowLayers:
https://www.mathworks.com/help/deeplearning/ref/importkerasnetwork.html#d123e88361

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!