LSTM模型们

LSTM模型们

注:是在用户情感比赛中想试试而做了几个····只能跑通目前没啥实际用处···

0x00 概述

提高循环神经网络的性能和泛华能力的技巧

0x01 双向LSTM

#【样本,每个样本30单词,每个单词100维度】
from keras import layers
from keras.models import Sequential
# 定义模型
model = Sequential()
model.add(layers.Embedding(max_words, embedding_dims, input_length=maxlen))#构建词嵌入,每个单词
model.add(layers.Bidirectional(layers.LSTM(32)))
#model.add(layers.Flatten())# 3 -> 2
model.add(layers.Dense(32, activation='relu'))#负责单词之间的练习和语义
model.add(layers.Dense(3, activation='softmax'))
model.summary()

0x02 dropout正则化的堆叠LSTM模型(/GRU)

#【样本,每个样本30单词,每个单词100维度】
from keras import layers
from keras.models import Sequential
# 定义模型
model_1 = Sequential()
model_1.add(layers.Embedding(max_words, embedding_dims, input_length=maxlen))#构建词嵌入,每个单词
model_1.add(layers.LSTM(32,
                      dropout=0.1,
                      recurrent_dropout=0.5,
                      return_sequences=True,
                     ))
model_1.add(layers.LSTM(64,
                     activation='relu',
                      dropout=0.1,
                      recurrent_dropout=0.5,
                     ))
#model.add(layers.Flatten())# 3 -> 2
model_1.add(layers.Dense(32, activation='relu'))#负责单词之间的练习和语义
model_1.add(layers.Dense(3, activation='softmax'))
model_1.summary()

0x03 Attention_CNN_LSTM


from keras.models import Sequential
from keras import layers
filters = 128
kernel_size = 3
right_branch = Sequential()
right_branch.add(layers.Embedding(max_words, embedding_dims, input_length=maxlen))
right_branch.add(layers.Conv1D(filters=filters,
kernel_size=kernel_size,
padding=’valid’,
activation=’relu’,
))
right_branch.add(layers.MaxPooling1D(3))
left_branch = Sequential()
left_branch.add(layers.Embedding(max_words, embedding_dims, input_length=maxlen))
left_branch.add(layers.LSTM(128, return_sequences=True))
merged = layers.Merge([left_branch, right_branch], mode=’dot’, output_shape=lambda x: x[0])
final_model = Sequential()
final_model.add(merged)
final_model.add(layers.GlobalAveragePooling1D())
final_model.add(layers.Dense(3, activation=’softmax’))
final_model.summary()

#right_branch.summary()

#model.add(layers.MaxPooling1D(3))

代码参考

我敲的不好也不规范···见谅