如何使用Keras Conv1D和Maxpooling1D提高模型的性能

时间:2018-04-10 21:25:35

标签: python keras max-pooling

我处理Facebook Babi Task的数据,并在使用LSTM模型(在此github https://github.com/fchollet/keras/blob/master/examples/babi_rnn.py上定义)之后。现在我想使用带有卷积和Maxpooling的模型来学习。但性能很低,我想知道是否有人有想法改进它。

embed_size = 50
batch_size=32
epochs=60
kernel_size = 3
pool_size = 2
strid = 1

story_model2 = Sequential()
    # input layers and convolutional layers
story_model2.add(Embedding(vocab_size,embed_size,input_length=story_max))
story_model2.add(Conv1D(embed_size, kernel_size=kernel_size, strides=strid, padding='valid', activation='relu'))
story_model2.add(MaxPooling1D(pool_size=pool_size))
story_model2.add(Dropout(.5))
story_model2.summary()


question_model2 = Sequential()
    # input layers and convolutional layers
question_model2.add(Embedding(vocab_size,embed_size,input_length=question_max))
question_model2.add(Conv1D(embed_size, kernel_size=kernel_size, strides=strid, padding='valid', activation='relu'))
question_model2.add(MaxPooling1D(pool_size=pool_size))
question_model2.add(Dropout(.5))
question_model2.summary()


mergedOut = Add()([story_model2.output,question_model2.output])
mergedOut = Flatten()(mergedOut)    
mergedOut = Dropout(.5)(mergedOut)
mergedOut = Dense(vocab_size, activation='relu')(mergedOut)
mergedOut = Dense(vocab_size, activation='softmax')(mergedOut)

newModel = Model([story_model2.input,question_model2.input], mergedOut)
newModel.compile(optimizer=Adam(lr=0.01),loss='categorical_crossentropy',metrics=['accuracy'])
newModel.fit([X, Xq], Y, batch_size=batch_size, epochs=epochs)

学习的acc值降低

Epoch 1/60
1000/1000 [==============================] - 3s 3ms/step - loss: 2.0393 - acc: 0.1710
Epoch 2/60
1000/1000 [==============================] - 0s 452us/step - loss: 1.7408 - acc: 0.2420
Epoch 3/60
1000/1000 [==============================] - 0s 341us/step - loss: 1.6526 - acc: 0.3020
Epoch 4/60
1000/1000 [==============================] - 1s 529us/step - loss: 1.6301 - acc: 0.3400
Epoch 5/60
1000/1000 [==============================] - 1s 629us/step - loss: 1.5853 - acc: 0.3420
Epoch 6/60
1000/1000 [==============================] - 0s 474us/step - loss: 1.5693 - acc: 0.3630
Epoch 7/60
1000/1000 [==============================] - 0s 401us/step - loss: 1.5620 - acc: 0.3440
Epoch 8/60
1000/1000 [==============================] - 1s 966us/step - loss: 1.5468 - acc: 0.3540
Epoch 9/60
1000/1000 [==============================] - 0s 458us/step - loss: 1.4980 - acc: 0.3870
Epoch 10/60
1000/1000 [==============================] - 0s 428us/step - loss: 1.5259 - acc: 0.3700
Epoch 11/60
1000/1000 [==============================] - 0s 378us/step - loss: 1.5112 - acc: 0.3760
Epoch 12/60
1000/1000 [==============================] - 0s 374us/step - loss: 1.4929 - acc: 0.3810
....

我的模型以性能形式返回:

1000/1000 [==============================] - 1s 1ms/step
Perte =  1.54148875618
Précision =  0.342

1 个答案:

答案 0 :(得分:0)

您的辍学率可能太高。尝试不使用它,只是为了进行比较。