text classification with RNN

简介: 本教程的目的是带领大家学会用 RNN 进行文本分类本次用到的数据集是 IMDB,一共有 50000 条电影评论,其中 25000 条是训练集,另外 25000 条是测试集

本教程的目的是带领大家学会用 RNN 进行文本分类

本次用到的数据集是 IMDB,一共有 50000 条电影评论,其中 25000 条是训练集,另外 25000 条是测试集

首先我们需要加载数据集,可以通过 TFDS 很简单的把数据集下载过来,如下代码所示

dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True)

train_dataset, test_dataset = dataset['train'], dataset['test']

train_dataset.element_spec

接下来我们需要创建 text encoder,可以通过 tf.keras.layers.experimental.preprocessing.TextVectorization 实现,如下代码所示

VOCAB_SIZE = 1000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
    max_tokens=VOCAB_SIZE
)
encoder.adapt(train_dataset.map(lambda text, label: text))

接下来我们需要搭建模型,下图是模型结构图

对应的代码如下所示

model = tf.keras.Sequential([
    encoder,
    tf.keras.layers.Embedding(
        input_dim=len(encoder.get_vocabulary()),
        output_dim=64,
        # Use masking to handle the variable sequence lengths
        mask_zero=True),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])

model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer=tf.keras.optimizers.Adam(1e-4),
              metrics=['accuracy'])

到这一步,我们就可以开始训练了,以及训练后进行模型评估

history = model.fit(train_dataset, epochs=10,
                    validation_data=test_dataset,
                    validation_steps=30)

test_loss, test_acc = model.evaluate(test_dataset)

print('Test Loss:', test_loss)
print('Test Accuracy:', test_acc)

上面是训练的结果记录图

代码地址: https://codechina.csdn.net/csdn_codechina/enterprise_technology/-/blob/master/text_classification_rnn.ipynb

目录
相关文章
|
数据采集 自然语言处理 数据可视化
Hidden Markov Model,简称 HMM
隐马尔可夫模型(Hidden Markov Model,简称 HMM)是一种统计模型,用于描述由隐藏的马尔可夫链随机生成观测序列的过程。它是一种生成模型,可以通过学习模型参数来预测观测序列的未来状态。HMM 主要包括以下几个步骤:
74 5
|
5月前
|
机器学习/深度学习 数据挖掘 API
[FastText in Text Classification]论文实现:Bag of Tricks for Efficient Text Classification
[FastText in Text Classification]论文实现:Bag of Tricks for Efficient Text Classification
31 2
|
机器学习/深度学习 数据挖掘
【论文解读】Co-attention network with label embedding for text classification
华南理工出了一篇有意思的文章,将标签和文本进行深度融合,最终形成带标签信息的文本表示和带文本信息的标签表示。
213 1
|
机器学习/深度学习 自然语言处理 数据可视化
SimCSE: Simple Contrastive Learning of Sentence Embeddings论文解读
本文介绍了SimCSE,一个简单的对比学习框架,极大地推进了最先进的句子嵌入。我们首先描述了一种无监督方法,该方法采用一个输入句子,并在一个对比目标中预测自己
256 0
|
机器学习/深度学习 编解码 自然语言处理
DeIT:Training data-efficient image transformers & distillation through attention论文解读
最近,基于注意力的神经网络被证明可以解决图像理解任务,如图像分类。这些高性能的vision transformer使用大量的计算资源来预训练了数亿张图像,从而限制了它们的应用。
501 0
|
数据可视化 数据挖掘
【论文解读】Dual Contrastive Learning:Text Classification via Label-Aware Data Augmentation
北航出了一篇比较有意思的文章,使用标签感知的数据增强方式,将对比学习放置在有监督的环境中 ,下游任务为多类文本分类,在低资源环境中进行实验取得了不错的效果
355 0
|
机器学习/深度学习 数据挖掘
【文本分类】ACT: an Attentive Convolutional Transformer for Efficient Text Classification
【文本分类】ACT: an Attentive Convolutional Transformer for Efficient Text Classification
184 0
【文本分类】ACT: an Attentive Convolutional Transformer for Efficient Text Classification
|
机器学习/深度学习 自然语言处理 数据挖掘
【文本分类】A C-LSTM Neural Network for Text Classification
【文本分类】A C-LSTM Neural Network for Text Classification
140 0
【文本分类】A C-LSTM Neural Network for Text Classification
|
机器学习/深度学习 存储 数据挖掘
【文本分类】Bag of Tricks for Efficient Text Classification
【文本分类】Bag of Tricks for Efficient Text Classification
【文本分类】Bag of Tricks for Efficient Text Classification
|
机器学习/深度学习 算法 数据挖掘
【多标签文本分类】Deep Learning for Extreme Multi-label Text Classification
【多标签文本分类】Deep Learning for Extreme Multi-label Text Classification
303 0
【多标签文本分类】Deep Learning for Extreme Multi-label Text Classification