其他分享
首页 > 其他分享> > 【510】NLP实战系列(七)—— 进阶版(dropout/stacking/BiLSTM)

【510】NLP实战系列(七)—— 进阶版(dropout/stacking/BiLSTM)

作者:互联网

参考:Bidirectional 层

  进阶版包含以下技术:

1. Bidirectional 层

1.1 语法

keras.layers.Bidirectional(layer, merge_mode='concat', weights=None)

1.2 参数

2. 举例

2.1 Recurrent dropout

from keras.layers import LSTM

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(LSTM(32,
               dropout=0.2,
               recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=20,
                    batch_size=128,
                    validation_split=0.2)

  

2.2 Stacking recurrent layers

from keras.layers import LSTM

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(LSTM(32,
               dropout=0.2,
               recurrent_dropout=0.2,
               return_sequences=True))
model.add(LSTM(32,
               dropout=0.2,
               recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=20,
                    batch_size=128,
                    validation_split=0.2)

  

2.3 Bidirectional recurrent layers

from keras.layers import LSTM
from keras.layers import Bidirectional

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(Bidirectional(LSTM(32,
                             dropout=0.2,
                             recurrent_dropout=0.2,
                             return_sequences=True)))
model.add(Bidirectional(LSTM(32,
                             dropout=0.2,
                             recurrent_dropout=0.2)))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=5,
                    batch_size=128,
                    validation_split=0.2)

 

标签:layers,NLP,recurrent,进阶,BiLSTM,dropout,0.2,add,model
来源: https://www.cnblogs.com/alex-bn-lee/p/14200376.html