简体   繁体   English

Keras遮罩层作为LSTM层的输入

[英]Keras masking layer as input to lstm layer

I'm trying to create a LSTM model. 我正在尝试创建一个LSTM模型。 Before passing the data to the first LSTM layer, I want to add a Masking layer. 在将数据传递到第一个LSTM层之前,我想添加一个Masking层。 I am able to do this using Sequential approach in Keras. 我可以在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.

You have forgotten to create an input layer. 您忘记了创建输入层。 First define the input layer and then pass the placeholder tensor to the Masking layer: 首先定义输入层,然后将占位符张量传递给Masking层:

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: 并且不要忘记通过将输入张量作为inputs参数来相应地更改模型定义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM