由于某种原因,我无法向模型添加LSTM层:
embed_size=8
LSTM=Sequential()
LSTM.add(Embedding(max_words,embed_size,input_length=max_len))
LSTM.add(LSTM(30, return_sequences=True,name='lstm_layer'))
LSTM.add(GlobalMaxPool1D())
...
我收到以下错误:
3 LSTM.add(Embedding(max_words,embed_size,input_length=max_len))
----> 4 LSTM.add(LSTM(30, return_sequences=True,name='lstm_layer'))
5 LSTM.add(GlobalMaxPool1D())
6 LSTM.add(Dropout(0.1))
C:\anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
438 # Raise exceptions in case the input is not compatible
439 # with the input_spec set at build time.
--> 440 self.assert_input_compatibility(inputs)
441
442 # Handle mask propagation.
C:\anaconda3\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
283 'Received type: ' +
284 str(type(x)) + '. Full input: ' +
--> 285 str(inputs) + '. All inputs to the layer '
286 'should be tensors.')
287
ValueError: Layer sequential_6 was called with an input that isn't a symbolic tensor. Received type: <class 'int'>. Full input: [30]. All inputs to the layer should be tenso
是什么意思?我的嵌入和LSTM之间是否存在尺寸问题?
由于某种原因,如果我使用以下“符号”,则事情似乎可以正常进行:
inp = Input(shape=(800, )) #maxlen=200 as defined earlier for
embed_size = 256
x = Embedding(20000, embed_size)(inp) #maximum dictionary ###outputs a 3D-Sensor
x = LSTM(120, return_sequences=True,name='lstm_layer')(x)
有什么问题吗?
谢谢
KS
答案 0 :(得分:1)
只是您的命名空间中的一个问题,您覆盖了导入的LSTM层。
将模型名称中的LSTM
更改为lstm
。
from keras import Sequential, Model
from keras.layers import Embedding,LSTM, GlobalMaxPool1D
embed_size=8
max_len = 1000
max_words = 10
lstm=Sequential()
lstm.add(Embedding(max_words,embed_size,input_length=max_len))
lstm.add(LSTM(30, return_sequences=True,name='lstm_layer'))
lstm.add(GlobalMaxPool1D())
工作正常
对新手的详细说明:
import语句设置名为LSTM
的本地引用,该引用是实现Keras层的类。然后,它在语句LSTM=Sequential()
中被覆盖。现在,名称LSTM
是Keras顺序模型的实例。最后,在语句LSTM.add(LSTM(...))
中,内部操作LSTM(..)
是对模型的调用,由__call__
类的Sequential
方法实现(此功能是python固有的)。因此抛出的错误表明 sequential_6被调用的输入不是符号张量... ,这意味着Sequential
类的实例(自动命名为框架调用了serial_6 ),但其输入与实现不兼容。此断言在__call__
类的Sequential
实现中。