Search code examples
pythontensorflowkerasrecurrent-neural-network

adding LSTM layer but getting required positional argument: 'units' error


I am trying to run my first machine learning model. However I am getting the error below.

return_sequences=True)) TypeError: init() missing 1 required positional argument: 'units'

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, LSTM, Dropout

model = Sequential()

model.add(LSTM(input_dim=1,
           output_dim=50,
           return_sequences=True))

model.add(Dropout(0.2))

model.add(LSTM(100, return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(output_dim=1))
model.add(Activation('linear'))

start = time.time()

model.compile(loss="mse", optimizer="rmsprop")

Since it said the parameter units was missing I have also tried the below line,

model.add(LSTM(100,
           input_dim=1,
           output_dim=50,
           return_sequences=True))

Then get this error message but I don't understand why that doesn't come in my first attempt. What am I missing?

TypeError: ('Keyword argument not understood:', 'input_dim')


Solution

  • units is the first parameter of LSTM, which represents the last dimension of the output data at this layer. It shows the first error because your code doesn't have units in your first attempt. units satisfies the condition so that it shows the second error in the second attempt.

    You should use the input_shape parameter to specify the shape of the first layer input in this case. Your first LSTM layer input_shape should have two data (timestep and feature,batch_size doesn't need to be filled in by default) since LSTM requires three dimensional input. Assuming your timestep is 10, your code should be changed to the following.

    from tensorflow.python.keras.models import Sequential
    from tensorflow.python.keras.layers import Dense, LSTM, Dropout,Activation
    
    model = Sequential()
    model.add(LSTM(units=100,input_shape=(10,1),return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(100, return_sequences=False))
    model.add(Dropout(0.2))
    model.add(Dense(units=1))
    model.add(Activation('linear'))
    model.compile(loss="mse", optimizer="rmsprop")
    print(model.summary())
    
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    lstm (LSTM)                  (None, 10, 100)           40800     
    _________________________________________________________________
    dropout (Dropout)            (None, 10, 100)           0         
    _________________________________________________________________
    lstm_1 (LSTM)                (None, 100)               80400     
    _________________________________________________________________
    dropout_1 (Dropout)          (None, 100)               0         
    _________________________________________________________________
    dense (Dense)                (None, 1)                 101       
    _________________________________________________________________
    activation (Activation)      (None, 1)                 0         
    =================================================================
    Total params: 121,301
    Trainable params: 121,301
    Non-trainable params: 0
    _________________________________________________________________