2

when I try to build a lstm model using keras. I see lots of models on internet that have a parameters setting like the code as follows:

from keras.models import Sequential
from keras.layers.recurrent import LSTM
model = Sequential()

model.add(LSTM(output_dim=32, 
               input_shape=(2, 3),
               activation='relu',
               return_sequences=True))
model.compile(loss='mae', optimizer='adam', metrics=['accuracy'])

but when i refer to the documents in the keras website, I didn't see any parameters named input shape or output dim. why is that? enter image description here

Xi Wan
  • 21
  • 1
  • 1
    check this one - https://stackoverflow.com/questions/44747343/keras-input-explanation-input-shape-units-batch-size-dim-etc – David Thery May 10 '21 at 10:11

1 Answers1

0

As given in the Tensorflow document https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM

tf.keras.layers.LSTM expects units as parameter for output_dim as a result of output number of neurons.

tf.keras.layers.LSTM(
    units, activation='tanh', recurrent_activation='sigmoid',
    use_bias=True, kernel_initializer='glorot_uniform',
    recurrent_initializer='orthogonal',
    bias_initializer='zeros', unit_forget_bias=True,
    kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None,
    activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None,
    bias_constraint=None, dropout=0.0, recurrent_dropout=0.0,
    return_sequences=False, return_state=False, go_backwards=False, stateful=False,
    time_major=False, unroll=False, **kwargs
)

Your code should be

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
model = Sequential()

model.add(LSTM(units=32, 
               input_shape=(2, 3),
               activation='relu',
               return_sequences=True))
model.compile(loss='mae', optimizer='adam', metrics=['accuracy'])