Convolutional Neural Networks for Sentence Classification笔记

首先,什么是卷积神经网络CNN,参考https://www.zhihu.com/question/22298352?rf=21686447
个人理解:过去时刻的输入也会对现在产生影响,卷积使用kernel在输入上(对现在时刻和非现在时刻)提取特征,每次移动提取的特征组成feature map。

文章整体架构:
在这里插入图片描述

  1. 首先最左边是输入层,包含一句话的n个词,每个词表示为embedding为k维的词向量。注意这里有static和non-static两个通道,分别表示:
  • non-static 通道: 词向量使用预训练wordvec,但是会在训练中进行微调。
  • static 通道: 词向量使用预训练wordvec,在训练中不改变。
  1. 然后是卷积层,这里使用m个不同size的kernel(kernel宽都是k,高为h,h不同)进行卷积,比如红色的kernel高为2,黄色的kernel高为3,这样可以捕捉不同范围内词之间的关系。卷积后得到m个(n-h)*1的feature map。

  2. 接下来是max-pooling层,对m个(n-h)1的feature map每个选择最大(最重要的)的特征留下来,得到m1的feature map。(思想:一个kernel只捕捉一句话中最重要的特征)

  3. 最后是带dropout和softmax的全连接层,softmax得到概率最大的预测类别。dropout是为了防止过拟合,在模型训练时随机让网络某些隐含层节点的权重不工作,但是它的权重得保留下来(只是暂时不更新而已),因为下次样本输入时它可能又得工作了。

文章结论:
本文证明即使是一个简单的CNN结构也能在分类任务上取得很好的效果。除此之外,通过无监督学习方法得到的词向量在NLP任务中有着重要的作用。

代码参考
import logging
%tensorflow_version 2.x
import tensorflow as tf
from tensorflow.keras.layers import Conv1D, MaxPool1D, Dense, Flatten, concatenate, Embedding,Input
from tensorflow.keras.models import Model
from tensorflow.keras.utils import plot_model

def textcnn(max_sequence_length, max_token_num, embedding_dim, output_dim, model_img_path=None, embedding_matrix=None):
#max_sequence_length:句子的长度
#max_token_num:词汇表的长度
#embedding_dim:嵌入矩阵的维度
#output_dim:嵌入矩阵处理后的词向量维度
#1.构建embedding层
x_input = Input(shape=(max_sequence_length,))#输入一个长度为max_sequence_length的句子
logging.info(“x_input.shape: %s” % str(x_input.shape)) # (?, 60)
if embedding_matrix is None:
x_emb = Embedding(input_dim=max_token_num, output_dim=embedding_dim, input_length=max_sequence_length)(x_input)
else:
x_emb = Embedding(input_dim=max_token_num, output_dim=embedding_dim, input_length=max_sequence_length,weights=[embedding_matrix], trainable=True)(x_input)
logging.info(“x_emb.shape: %s” % str(x_emb.shape)) # (?, 60, 300)
#2.构建卷积层和池化层
pool_output = []
kernel_sizes = [2, 3, 4]
for kernel_size in kernel_sizes:
c = Conv1D(filters=2, kernel_size=kernel_size, strides=1)(x_emb)#卷积
p = MaxPool1D(pool_size=int(c.shape[1]))©#池化
pool_output.append§
logging.info(“kernel_size: %s \t c.shape: %s \t p.shape: %s” % (kernel_size, str(c.shape), str(p.shape)))
pool_output = concatenate([p for p in pool_output])
logging.info(“pool_output.shape: %s” % str(pool_output.shape)) # (?, 1, 6)

#3.展平+输出
x_flatten = Flatten()(pool_output) # (?, 6)
y = Dense(output_dim, activation=‘softmax’)(x_flatten) # (?, 2)
logging.info(“y.shape: %s \n” % str(y.shape))

model = Model([x_input], outputs=[y])
if model_img_path:
plot_model(model, to_file=model_img_path, show_shapes=True, show_layer_names=False)
model.summary()
return model

textcnn(max_sequence_length=60, max_token_num=5000, embedding_dim=100,output_dim=100)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值