嗨,我正在尝试使用嵌入进行多类分类,并将Conv1D与双向LSTM堆叠在一起,这是我的脚本:
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>
我做错了什么?谢谢!
答案 0 :(得分:0)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_8 (Embedding) (None, 100, 100) 500000
_________________________________________________________________
dropout_5 (Dropout) (None, 100, 100) 0
_________________________________________________________________
conv1d_3 (Conv1D) (None, 100, 100) 30100
_________________________________________________________________
max_pooling1d_3 (MaxPooling1 (None, 50, 100) 0
_________________________________________________________________
bidirectional_7 (Bidirection (None, 256) 234496
_________________________________________________________________
dense_7 (Dense) (None, 124) 31868
=================================================================
Total params: 796,464
Trainable params: 796,464
Non-trainable params: 0
_________________________________________________________________
None
打印模型摘要,没有任何错误:
x = x[x > para[1]]