在功能模式下使用LSTM时出现问题

时间:2019-05-03 06:45:39

标签: keras tf.keras

使用下面的功能模式创建LSTM层时,会引发错误

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2

用于重现问题的代码

import keras
from keras.layers import Input, LSTM, Dense
from keras.models import Model

import numpy as np


def build_lstm(lstm_input, hidden_dim=256):
    out = LSTM(hidden_dim, input_shape=(1, 129))(lstm_input)
    return out


def test_lstm():
    lstm_input = Input(shape=(129, ), name='lstm_input')
    out = build_lstm(lstm_input)

    predictions = Dense(256, activation='softmax')(out)
    model = Model(inputs=lstm_input, outputs=predictions)
    model.compile(loss='categorical_crossentropy',
        optimizer='rmsprop',
        metrics=['accuracy'])

    model.summary()

    return  

错误日志

(NPI_Arch_Keras) C:\workspaces\NPL\project\NPI_Arch_Keras\npi>python npi_lstm_test.py
Using TensorFlow backend.
Traceback (most recent call last):
  File "npi_lstm_test.py", line 35, in <module>
    test_lstm()
  File "npi_lstm_test.py", line 21, in test_lstm
    out = build_lstm(lstm_input)
  File "npi_lstm_test.py", line 15, in build_lstm
    out = LSTM(hidden_dim, input_shape=(1, 129))(lstm_input)
  File "C:\MyProgramFiles\Anaconda3\envs\NPI_Arch_Keras\lib\site-packages\keras\layers\recurrent.py", line 532, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)
  File "C:\MyProgramFiles\Anaconda3\envs\NPI_Arch_Keras\lib\site-packages\keras\engine\base_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "C:\MyProgramFiles\Anaconda3\envs\NPI_Arch_Keras\lib\site-packages\keras\engine\base_layer.py", line 311, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2

以下使用顺序模型的代码版本成功运行

import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense

import numpy as np


def build_lstm(hidden_dim=256, input_shape=None):
    model = Sequential()
    model.add(LSTM(hidden_dim, return_sequences=True, input_shape=input_shape))
    model.add(LSTM(hidden_dim))
    return model


def test_lstm():
    model = Sequential()
    model.add(build_lstm(input_shape=(1, 129)))
    model.add(Dense(256, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

    model.summary()

    return

成功日志

(NPI_Arch_Keras) C:\workspaces\NPL\project\NPI_Arch_Keras\npi>python npi_lstm_working.py
Using TensorFlow backend.
WARNING:tensorflow:From C:\MyProgramFiles\Anaconda3\envs\NPI_Arch_Keras\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
sequential_2 (Sequential)    (None, 256)               920576
_________________________________________________________________
dense_1 (Dense)              (None, 256)               65792
=================================================================
Total params: 986,368
Trainable params: 986,368
Non-trainable params: 0
_________________________________________________________________

1 个答案:

答案 0 :(得分:0)

此代码应在功能模型中起作用:

in = Input(shape=(1, 129))
lstm_1 = LSTM(256, return_sequences=True)(in)
lstm_2 = LSTM(256)(lstm_1)
out = Dense(256)(lstm_2)

model = Model(in, out)