深度学习实战之【电影评论分类】:二分类问题


二分类问题可能是应用最广泛的机器学习问题。在这个例子中,将学习根据电影评论的文字内容将其划分为正面或负面。

此次的数据集被分为正面评论和负面评论,都各占一半,训练集和测试集都各有25000条。

一. IMDB数据集

本节使用 IMDB 数据集,它包含来自互联网电影数据库(IMDB)的 50 000 条严重两极分化的评论。数据集被分为用于训练的 25 000 条评论与用于测试的 25 000 条评论,训练集和测试集都包含 50% 的正面评论和 50% 的负面评论。

IMDB 数据集内置于 Keras 库。它已经过预处理:评论(单词序列)已经被转换为整数序列,其中每个整数代表字典中的某个单词。

1.1 例行看一下keras的版本。

import keras
keras.__version__
 Out[1]: '2.9.0'

1.2 加载 IMDB 数据集

from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

这里是加载imdb数据集的操作,其中 num_words=10000 代表只取出前10000个经常用的词汇,来保证学习的合理性。

train_data[0]
train_labels[0]
max([max(sequence) for sequence in train_data])

输出为

 Out[2]: [1, 14, 22, 16, ... 178, 32]  # train_data 是评论组成的列表,每条评论又是单词索引组成的列表(表示一系列单词)。
 Out[3]: 1  # train_labels 是 0 和 1 组成的列表,其中 0 代表负面 (negative),1 代表正面 (positive)。
 Out[4]: 9999  # 由于限定为前 10000 个最常见的单词,单词索引都不会超过 10000。

这里看一下第一句话和标签,还有索引的最大值,可以很容易的看出,第一个输出的是第一句话的单词索引,是一行数字,第二个是对应的标签,1为积极,0为消极,第三句话是最大索引,因为只有10000个单词,所以最大索引毫无疑问是9999.

word_index = imdb.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])

这三行是用于解析出来相应的评论语句用的。第一行是获取索引字典,第二行是将键值颠倒,最后一行是解码。因为前三个是预先保留的索引,所以跳过。

decoded_review

看一下这个句子,就会明白评论是什么:“? this film was just brilliant casting location scenery story direction everyone’s really suited the part they played and you could just imagine being there robert ? is an amazing actor and now the same being director ? father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for ? and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also ? to the two little boy’s that played the ? of norman and paul they were just brilliant children are often left out of the ? list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don’t you think the whole story was so lovely because it was true and was someone’s life after all that was shared with us all”
太长了说实话人工分辨都要费一点时间。

二. 数据准备

你不能将整数序列直接输入神经网络。你需要将列表转换为张量。转换方法主要有填充列表、对列表进行one-hot编码两种方式,下面采用one-hot编码的方法,将数据向量化。

2.1 将整数序列编码为二进制矩阵

import numpy as np

def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))  # 创建一个形状为(len(sequences), dimension)的零矩阵
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1  # 将results[i]的指定索引设为1
    return results

x_train = vectorize_sequences(train_data)  # 将训练数据向量化
x_test = vectorize_sequences(test_data)  # 将测试数据向量化

y_train = np.asarray(train_labels).astype('float32')  # 将标签向量化
y_test = np.asarray(test_labels).astype('float32')

惯例,这里对数据使用one hot编码,目的是将数据矩阵化,目的是简化数字,让它从0到9999变为0和1,同时把标签也向量化了。
现在可以将数据输入到神经网络中。

三. 构建网络

3.1 模型定义

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

这里开始搭建网络结构了。前两层为relu函数,它是非线性的函数,目的是得到更加丰富的假设空间,这样多层的结构才有意义,后面用sigmoid函数输出0到1的值,就是最终的判断。

3.2 编译模型

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

上述代码将优化器、损失函数和指标作为字符串传入,这是因为 rmsprop 、binary_crossentropy 和 accuracy 都是 Keras 内置的一部分。有时你可能希望配置自定义优化器的参数,或者传入自定义的损失函数或指标函数。前者可通过向 optimizer 参数传入一个优化器类实例来实现;后者可通过向 loss 和 metrics 参数传入函数对象来实现。

3.3 配置优化器

from keras import optimizers

model.compile(optimizer=optimizers.RMSprop(lr=0.001),
              loss='binary_crossentropy',
              metrics=['accuracy'])

3.4 使用自定义的损失和指标

from keras import losses
from keras import metrics

model.compile(optimizer=optimizers.RMSprop(lr=0.001),
              loss=losses.binary_crossentropy,
              metrics=[metrics.binary_accuracy])

到此,网络搭建,前期准备都完成了,这个例子和第一个例子非常接近,只是数据量变大,评估方法更加优化了。这些下一期再谈。
参考链接
https://blog.csdn.net/qq_40195614/article/details/89764055#comments_13002286

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于深度学习的文本分类任务是指利用深度学习模型对文本进行情感分类。在这个任务中,我们使用了CNN和RNN模型来进行文本分类数据集包含了15万余项英文文本,情感分为0-4共五类情感。任务的流程如下:输入数据→特征提取→神经网络设计→结果输出。 在特征提取阶段,我们使用了词嵌入(Word embedding)技术。词嵌入是一种将单词映射到低维向量空间的方法,它可以将单词的语义信息编码为向量表示。在本次任务中,我们参考了博客\[NLP-Beginner 任务二:基于深度学习的文本分类\](https://pytorch.org/Convolutional Neural Networks for Sentence Classification)中的方法,使用了预训练的词嵌入模型。 神经网络设计阶段,我们采用了卷积神经网络(CNN)和循环神经网络(RNN)的结合。具体来说,我们使用了四个卷积核,大小分别为2×d, 3×d, 4×d, 5×d。这样设计的目的是为了挖掘词组的特征。例如,2×d的卷积核用于挖掘两个连续单词之间的关系。在模型中,2×d的卷积核用红色框表示,3×d的卷积核用黄色框表示。 最后,我们将模型的输出结果进行分类,得到文本的情感分类结果。这个任务的目标是通过深度学习模型对文本进行情感分类,以便更好地理解和分析文本数据。 #### 引用[.reference_title] - *1* *3* [NLP-Brginner 任务二:基于深度学习的文本分类](https://blog.csdn.net/m0_61688615/article/details/128713638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [NLP基本任务二:基于深度学习的文本分类](https://blog.csdn.net/Mr_green_bean/article/details/90480918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值