我正在使用keras 2.02和tensorflow 1.3来构建具有多种大小的过滤器的卷积网。代码如下所示。
flts=100
kernel_sizes=[2,3,4]
submodels = []
for kw in kernel_sizes: # kernel sizes
submodel = Sequential()
submodel.add(embedding_layer)
submodel.add(Dropout(0.2))
submodel.add(Conv1D(filters=flts,
kernel_size=kw,
padding='same',
activation='relu'))
submodel.add(MaxPooling1D(pool_size=kw))
submodels.append(submodel)
big_model = Sequential()
#concat = Concatenate(axis=1) #line B
#big_model.add(concat(submodels)) #line C
big_model.add(Merge(submodels, mode="concat", concat_axis=1)) #line A
big_model.add(LSTM(units=100, return_sequences=True))
big_model.add(GlobalMaxPooling1D())
#big_model.add(Dropout(0.2))
big_model.add(Dense(2, activation='softmax'))
big_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
在A行上使用已弃用的代码时,它可以正常工作。但是当它被行B,C替换时,会产生如下错误:
--> 141 big_model.add(concat(submodels))
big_model.add = <bound method Sequential.add of <keras.models.Sequential object>>
concat = <keras.layers.merge.Concatenate object>
submodels = [<keras.models.Sequential object>, <keras.models.Sequential object>, <keras.models.Sequential object>]
142 big_model.add(LSTM(units=100, return_sequences=True))
143 big_model.add(GlobalMaxPooling1D())
144 #big_model.add(Dropout(0.2))
145 big_model.add(Dense(2, activation='softmax'))
........
/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in int_shape(x=<keras.models.Sequential object>)
404 (2, 2)
405 ```
406 """
407 if hasattr(x, '_keras_shape'):
408 return x._keras_shape
--> 409 shape = x.get_shape()
shape = undefined
x.get_shape = undefined
410 try:
411 return tuple([i.__int__() for i in shape])
412 except ValueError:
413 return None
AttributeError: 'Sequential' object has no attribute 'get_shape'
___________________________________________________________________
我不知道该怎么做才能解决这个问题?
提前致谢
答案 0 :(得分:1)
当你的模型包含分支时,我建议使用功能API。 Concatenate
图层将张量列表作为输入。在这种情况下,列表应包含子模型的输出张量。
submodel_outputs = [model.output for model in submodels]
x = Concatenate(axis=1)(submodel_outputs)
x = LSTM(units=100, return_sequences=True)(x)
x = GlobalMaxPooling1D()(x)
x = Dropout(0.2)(x)
x = Dense(2, activation='softmax')(x)
big_model = Model(inputs=embedding_layer.input, outputs=x)
big_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
big_model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
embedding_1_input (InputLayer) (None, None) 0
__________________________________________________________________________________________________
embedding_1 (Embedding) (None, None, 64) 64000 embedding_1_input[0][0]
__________________________________________________________________________________________________
dropout_3 (Dropout) (None, None, 64) 0 embedding_1[0][0]
__________________________________________________________________________________________________
dropout_4 (Dropout) (None, None, 64) 0 embedding_1[0][0]
__________________________________________________________________________________________________
dropout_5 (Dropout) (None, None, 64) 0 embedding_1[0][0]
__________________________________________________________________________________________________
conv1d_4 (Conv1D) (None, None, 100) 12900 dropout_3[0][0]
__________________________________________________________________________________________________
conv1d_5 (Conv1D) (None, None, 100) 19300 dropout_4[0][0]
__________________________________________________________________________________________________
conv1d_6 (Conv1D) (None, None, 100) 25700 dropout_5[0][0]
__________________________________________________________________________________________________
max_pooling1d_4 (MaxPooling1D) (None, None, 100) 0 conv1d_4[0][0]
__________________________________________________________________________________________________
max_pooling1d_5 (MaxPooling1D) (None, None, 100) 0 conv1d_5[0][0]
__________________________________________________________________________________________________
max_pooling1d_6 (MaxPooling1D) (None, None, 100) 0 conv1d_6[0][0]
__________________________________________________________________________________________________
concatenate_7 (Concatenate) (None, None, 100) 0 max_pooling1d_4[0][0]
max_pooling1d_5[0][0]
max_pooling1d_6[0][0]
__________________________________________________________________________________________________
lstm_4 (LSTM) (None, None, 100) 80400 concatenate_7[0][0]
__________________________________________________________________________________________________
global_max_pooling1d_4 (GlobalM (None, 100) 0 lstm_4[0][0]
__________________________________________________________________________________________________
dropout_7 (Dropout) (None, 100) 0 global_max_pooling1d_4[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 2) 202 dropout_7[0][0]
==================================================================================================
Total params: 202,502
Trainable params: 202,502
Non-trainable params: 0
__________________________________________________________________________________________________
将Model
与Scikit-Learn API一起使用:
class MyKerasClassifier(KerasClassifier):
def predict(self, x, **kwargs):
kwargs = self.filter_sk_params(self.model.predict, kwargs)
proba = self.model.predict(x, **kwargs)
if proba.shape[-1] > 1:
classes = proba.argmax(axis=-1)
else:
classes = (proba > 0.5).astype('int32')
return self.classes_[classes]
最初在KerasClassifier.predict()
中,调用函数predict_classes()
以获取预测类。您可以覆盖此函数并根据概率计算类,以避免需要predict_classes()
。