Reputation: 427
So I have build Neural Network with the following code:
import tensorflow as tf
tf_model = tf.keras.Sequential()
tf_model.add(tf.keras.layers.LSTM(50, activation='relu'))
tf_model.add(tf.keras.layers.Dense(20, activation='relu'))
tf_model.add(tf.keras.layers.Dense(10, activation='relu'))
tf_model.add(tf.keras.layers.Dense(1, activation='linear'))
tf_model.compile(optimizer='Adam', loss='mse')
My training set is shaped as follows:
>> ts_train_X.shape
(16469, 3, 21)
I have read numerous articles and questions here on stackoverflow in order to bring the data frame in the right shape for the LSTM. Almost every of the pages I found specified the input_shape
parameter and passed it either to LSTM(..) or Sequential(..).
When I look at the LSTM API I cannot find a reference to this parameter. I also had a glimpse on the source code and to me it seems that the shape is somehow automatically inferred, but I am not sure about this.
This leads me to my question: Why does my code work? How can the LSTM layer as the first layer know the shape of my inputs, if I don't specify the input_shape parameter?
edit: change title as per suggestion in comments.
Upvotes: 2
Views: 467
Reputation: 56417
The parameter input_shape
can be given to the constructor of any keras Layer
subclass, as this is how the API is defined.
The code works because input_shape
is passed as a keyword argument (the **kwargs
), then these keyword arguments are passed by the LSTM
constructor to the Layer
constructor, which then proceeds to store the information for later use. This effectively means that the input_shape
parameter does not have to be defined in each layer, and it is passed as a keyword argument instead.
I think the issue is that since keras
has been moved to tensorflow
, the documentation might not be complete. You can find more information about the input_shape
parameter in the Guide to the Sequential API.
Upvotes: 3