在功能模式下使用LSTM时要发行



使用以下错误的功能模式创建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:workspacesNPLprojectNPI_Arch_Kerasnpi>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:MyProgramFilesAnaconda3envsNPI_Arch_Keraslibsite-packageskeraslayersrecurrent.py", line 532, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)
  File "C:MyProgramFilesAnaconda3envsNPI_Arch_Keraslibsite-packageskerasenginebase_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "C:MyProgramFilesAnaconda3envsNPI_Arch_Keraslibsite-packageskerasenginebase_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:workspacesNPLprojectNPI_Arch_Kerasnpi>python npi_lstm_working.py
Using TensorFlow backend.
WARNING:tensorflow:From C:MyProgramFilesAnaconda3envsNPI_Arch_Keraslibsite-packagestensorflowpythonframeworkop_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
_________________________________________________________________

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

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)

最新更新