【发布时间】:2018-08-20 15:34:04
【问题描述】:
您好,我正在尝试使用嵌入进行多类分类,并使用双向 LSTM 堆叠 Conv1D,这是我的脚本:
embed_dim = 100
lstm_out = 128
max_features = 5000
model8 = Sequential()
model8.add(Embedding(max_features, embed_dim, input_length = X.shape[1]))
model8.add(Dropout(0.2))
model8.add(Conv1D(filters=100, kernel_size=3, padding='same', activation='relu'))
model8.add(MaxPooling1D(pool_size=2))
model8.add(Bidirectional(LSTM(lstm_out)))
model8.add(Dense(124,activation='softmax'))
model8.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
print model8.summary()
我收到如下错误消息:
TypeErrorTraceback (most recent call last)
<ipython-input-51-6c831fc4581f> in <module>()
9 model8.add(Embedding(max_features, embed_dim))
10 model8.add(Dropout(0.2))
---> 11 model8.add(Conv1D(filters=100, kernel_size=3, padding='same', activation='relu'))
12 model8.add(MaxPooling1D(pool_size=2))
13 model8.add(Bidirectional(LSTM(lstm_out)))
/jupyter/local/lib/python2.7/site-packages/tensorflow/python/training/checkpointable/base.pyc in _method_wrapper(self, *args, **kwargs)
362 self._setattr_tracking = False # pylint: disable=protected-access
363 try:
--> 364 method(self, *args, **kwargs)
365 finally:
366 self._setattr_tracking = previous_value # pylint: disable=protected-access
/jupyter/local/lib/python2.7/site-packages/tensorflow/python/keras/engine/sequential.pyc in add(self, layer)
128 raise TypeError('The added layer must be '
129 'an instance of class Layer. '
--> 130 'Found: ' + str(layer))
131 self.built = False
132 if not self._layers:
TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.convolutional.Conv1D object at 0x7f62907f8590>
我做错了什么?谢谢!
【问题讨论】:
-
您的代码在我的机器上运行没有任何错误。你使用的是什么版本的 Keras,即
print(keras.__version__)? -
我使用的是 Keras 2.2.2 和 Python 2.7
-
能否也将导入语句添加到您的帖子中?
-
from tensorflow.python.keras.preprocessing.sequence import pad_sequences from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Embedding, LSTM, Dropout, SpatialDropout1D, Bidirectional from sklearn.model_selection import train_test_split from tensorflow.python.keras import utils from tensorflow.python.keras.utils.np_utils import to_categorical
-
我在导入的图层中没有看到
Conv1D和MaxPooling1D?!确保它们也是从同一个模块导入的,即tensorflow.python.keras.layers。
标签: python keras conv-neural-network lstm embedding