一.概述
CRNN(A C-LSTM Neural Network for Text Classification),是Chunting Zhou等提出的一种联合CNN和RNN的优点的混合神经网络,可称之为"卷积-递归"神经网络。
不同于RCNN先对文本句子上下文本信息、以及word-embedding本身特征的提取,CRNN中的CNN可以看成另外一种形式的句子信息的Embedding(char、word and n-gram),它具有实际意义上的解释性;也可以看成是对局部短语特征的提取,用于文本表达和文本分类。而LSTM便很善于捕捉文本的全局特征,在它接入CNN后,可以有效缓解CNN对于长文本句子信息捕获不足的缺点。
其实现在看来,CRN算是比较古老的版本的了,也没什么特别多可说的,不就是把CNN和RNN堆砌起来么,没什么那么多值得大惊小怪的。
github项目地址:
二. CRNN模型原理等
2.1 CRNN模型图
2.2 tricks等
初始化: 词向量dim=100, OOV词=随机初始化为均匀分布(uniform distribution [-0.25, 0.25])
规范化(Regularization): dropout 和 L2
似乎就没有了
三. CRNN代码实现
3.1 很简单的一个模型,就是把CNN和LSTM连在一起,似乎就好了
github项目地址:
3.2 核心代码
def create_model(self, hyper_parameters):
"""
构建神经网络
:param hyper_parameters:json, hyper parameters of network
:return: tensor, moedl
"""
super().create_model(hyper_parameters)
x = self.word_embedding.output
embedding_output_spatial = SpatialDropout1D(self.dropout_spatial)(x)
if self.rnn_units=="LSTM":
layer_cell = LSTM
elif self.rnn_units=="GRU":
layer_cell = GRU
elif self.rnn_units=="CuDNNLSTM":
layer_cell = CuDNNLSTM
elif self.rnn_units=="CuDNNGRU":
layer_cell = CuDNNGRU
else:
layer_cell = GRU
# CNN
convs = []
for kernel_size in self.filters:
conv = Conv1D(self.filters_num,
kernel_size=kernel_size,
strides=1,
padding='SAME',
kernel_regularizer=regularizers.l2(self.l2),
bias_regularizer=regularizers.l2(self.l2),
)(embedding_output_spatial)
convs.append(conv)
x = Concatenate(axis=1)(convs)
# Bi-LSTM, 论文中使用的是LSTM
x = Bidirectional(layer_cell(units=self.rnn_units,
return_sequences=True,
activation='relu',
kernel_regularizer=regularizers.l2(self.l2),
recurrent_regularizer=regularizers.l2(self.l2)
))(x)
x = Dropout(self.dropout)(x)
x = Flatten()(x)
# 最后就是softmax
dense_layer = Dense(self.label, activation=self.activate_classify)(x)
output = [dense_layer]
self.model = Model(self.word_embedding.input, output)
self.model.summary(120)
希望对你有所帮助!