TextCNN
简要的说TextCNN提供了处理NLP的CNN框架,即卷积层等的搭配方案
关于TextCNN的介绍,网上也很详细
这里记录下Keras如何使用
#参数可调
maxlen=200
max_features=2000
embed_size=32
comment_seq = Input(shape=[maxlen])
# Embedding
emb_comment = Embedding(max_features, embed_size, input_shape=[maxlen])()
#卷积层
convs = []
filter_sizes = [2, 3, 4, 5]
for filter in filter_sizes:
conv = Conv1D(filters=100, kernel_size=filter, activation='relu')(emb_comment)
pool = MaxPooling1D(maxlen - filter + 1)(conv)
pool = Flatten()(pool)
convs.append(pool)
merge = concatenate(convs, axis=1)
out = Dropout(0.2)(merge)
output = Dense(32, activation='relu')(out)
output = Dense(1, activation='sigmoid')(output)
model = Model([comment_seq], output)
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
参考资料
【源码】https://github.com/dennybritz/cnn-text-classification-tf
【代码参考】https://github.com/hongweijun811/wjgit/blob/master/text_cnn_demo.py