keras - 在功能模式下使用 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:\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
_________________________________________________________________
解决方案
此代码应在功能模型中工作:
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)
推荐阅读
- javascript - 如何根据曲线上的当前时间计算圆的位置
- c++ - C++:有没有办法以编程方式检查我的打印机是喷墨打印机还是激光打印机?
- regex - 在第一个竖线 (|) 之间查找匹配项
- sorting - Haskell中列表输出中缺少第一个元素
- java - 从我的 Java Web 服务器向 Android 手机发送推送通知
- networking - Windows 10 上的 Ping 失败
- ssl - 导入根存储的非自签名证书是否需要(自签名)颁发者也导入根存储?
- c# - 使用 Nswag 控制台(来自 NPM 包)生成 C# 客户端
- azure-storage - 有没有办法以编程方式检查 Azure blob 存储的 SAS URL 是否过期?
- python - ValueError:检查输入时出错:预期 conv2d_9_input 的形状为 (64, 64, 3) 但得到的数组的形状为 (32, 32, 1)