Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras masking layer as input to lstm layer

I'm trying to create a LSTM model. Before passing the data to the first LSTM layer, I want to add a Masking layer. I am able to do this using Sequential approach in Keras. See example. However when I try to code it differently I get a value error (see below). Any Idea on how to fix this?

import keras


def network_structure(window_len, n_features, lstm_neurons):

    masking = keras.layers.Masking(

        mask_value=0.0, input_shape=(window_len, n_features)

    )

    lstm_h1 = keras.layers.LSTM(lstm_neurons)(masking)

    lstm_h2 = keras.layers.LSTM(lstm_neurons)(lstm_h1)

    cte = keras.layers.Dense(
        1,
        activation='linear',
        name='CTE',
    )(lstm_h2)

    ate = keras.layers.Dense(
        1,
        activation='linear',
        name='ATE',
    )(lstm_h2)

    pae = keras.layers.Dense(
        1,
        activation='linear',
        name='PAE',
    )(lstm_h2)

    model = keras.models.Model(
        inputs=masking,
        outputs=[cte, ate, pae]
    )

    model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae'])

    model.summary()

    return model


model = network_structure(32, 44, 125)   

Error Message:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\keras\engine\topology.py", line 442, in assert_input_compatibility
    K.is_keras_tensor(x)
  File "C:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 468, in is_keras_tensor
    raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
ValueError: Unexpectedly found an instance of type `<class 'keras.layers.core.Masking'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Master Tk/PycharmProjects/FPL/testcompile.py", line 46, in <module>
    model = network_structure(32, 44, 125)
  File "C:/Users/Master Tk/PycharmProjects/FPL/testcompile.py", line 12, in network_structure
    lstm_h1 = keras.layers.LSTM(lstm_neurons)(masking)
  File "C:\Python35\lib\site-packages\keras\layers\recurrent.py", line 499, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)
  File "C:\Python35\lib\site-packages\keras\engine\topology.py", line 575, in __call__
    self.assert_input_compatibility(inputs)
  File "C:\Python35\lib\site-packages\keras\engine\topology.py", line 448, in assert_input_compatibility
    str(inputs) + '. All inputs to the layer '
ValueError: Layer lstm_1 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.core.Masking'>. Full input: [<keras.layers.core.Masking object at 0x000002224683A780>]. All inputs to the layer should be tensors.
like image 508
mastersom Avatar asked Sep 14 '25 03:09

mastersom


1 Answers

You have forgotten to create an input layer. First define the input layer and then pass the placeholder tensor to the Masking layer:

inp = Input(shape=(window_len, n_features))
masking = keras.layers.Masking(mask_value=0.0)(inp)
lstm_h1 = keras.layers.LSTM(lstm_neurons)(masking)

And don't forget to change the model definition accordingly by passing the input tensor as the inputs argument:

model = keras.models.Model(inputs=inp, outputs=[cte, ate, pae])
like image 167
today Avatar answered Sep 15 '25 18:09

today