本文是在学习tensorflow2.0官方教程时的一个笔记,原始教程请见文本情感分类

准备工作

1. 安装tensorflow并导入相关库

如果已经安装了可以略去此步
!pip install tensorflow

import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt

2. 准备数据集

2.1 导入数据集
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True,as_supervised=True)
train_data, test_data = dataset['train'], dataset['test']

数据集介绍
这是一个imdb的影评数据集。

tfds.core.DatasetInfo(
name=‘imdb_reviews’,
version=1.0.0,
description=‘Large Movie Review Dataset.
This is a dataset for binary sentiment classification containing substantially more data than previous benchmark datasets. We provide a set of 25,000 highly polar movie reviews for training, and 25,000 for testing. There is additional unlabeled data for use as well.’,
homepage=‘http://ai.stanford.edu/~amaas/data/sentiment/’,
features=FeaturesDict({
‘label’: ClassLabel(shape=(), dtype=tf.int64, num_classes=2),
‘text’: Text(shape=(None,), dtype=tf.int64, encoder=),
}),
total_num_examples=100000,
splits={
‘test’: 25000,
‘train’: 25000,
‘unsupervised’: 50000,
},
supervised_keys=(‘text’, ‘label’),
citation="""@InProceedings{maas-EtAl:2011:ACL-HLT2011,
author = {Maas, Andrew L. and Daly, Raymond E. and Pham, Peter T. and Huang, Dan and Ng, Andrew Y. and Potts, Christopher},
title = {Learning Word Vectors for Sentiment Analysis},
booktitle = {Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies},
month = {June},
year = {2011},
address = {Portland, Oregon, USA},
publisher = {Association for Computational Linguistics},
pages = {142–150},
url = {http://www.aclweb.org/anthology/P11-1015}
}""",
redistribution_info=,
)

因为数据集的info自带encoder,所以直接调用

# The dataset info includes the encoder
encoder = info.features['text'].encoder

测试encoder

sample_string = 'Hello world.'encoded_string = encoder.encode(sample_string)
print('Encoded string is {}'.format(encoded_string))
original_string = encoder.decode(encoded_string)
print('The original string: "{}"'.format(original_string))
assert original_string == sample_string
for index in encoded_string:print('{} ----> {}'.format(index, encoder.decode([index])))

运行结果为:

Encoded string is [4025, 222, 562, 7975]
The original string: “Hello world.”
4025 ----> Hell
222 ----> o
562 ----> world
7975 ----> .

2.2 数据集预处理

对数据进行shuffle防止过拟合,对数据进行padded_batch,便于训练。值得注意的是,tensorflow2.0的padded_batch不需要paaded_shape。

BUFFER_SIZE = 10000
BATCH_SIZE = 64
train_dataset = (train_data.shuffle(BUFFER_SIZE).padded_batch(BATCH_SIZE))test_dataset = (test_data.padded_batch(BATCH_SIZE))

3 创建模型

这里创建的是一个tf.keras.Sequential,模型如下图所示

embedding层的作用是生成词向量,作为神经网络的输入,这里的词向量选用的是64维,一般实际可能会更大一些。
LSTM层:长短程记忆单元,这里采用的是双向的,也就是走到最后一个词之后,倒着走到第一行。
dense层1:是一个全连接神经网络,64个unit
dense层2:是一个全连接神经网络,输出层
更多的理解可以参考循环神经网络学习笔记

model = tf.keras.Sequential([tf.keras.layers.Embedding(encoder.vocab_size, 64),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'])

4 模型训练

tf.keras可以直接调用fit函数进行训练,训练的epoch为10次,验证数据为测试机,每30步一次。

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

Epoch 1/10
391/391 [] - 44s 112ms/step - loss: 0.6572 - accuracy: 0.5434 - val_loss: 0.4859 - val_accuracy: 0.7865
Epoch 2/10
391/391 [
] - 43s 110ms/step - loss: 0.3448 - accuracy: 0.8572 - val_loss: 0.3440 - val_accuracy: 0.8458
Epoch 3/10
391/391 [] - 43s 110ms/step - loss: 0.2618 - accuracy: 0.8952 - val_loss: 0.3378 - val_accuracy: 0.8458
Epoch 4/10
391/391 [
] - 43s 111ms/step - loss: 0.2110 - accuracy: 0.9204 - val_loss: 0.3278 - val_accuracy: 0.8594
Epoch 5/10
391/391 [] - 43s 110ms/step - loss: 0.1867 - accuracy: 0.9322 - val_loss: 0.3563 - val_accuracy: 0.8510
Epoch 6/10
391/391 [
] - 43s 110ms/step - loss: 0.1624 - accuracy: 0.9432 - val_loss: 0.3610 - val_accuracy: 0.8615
Epoch 7/10
391/391 [] - 43s 110ms/step - loss: 0.2073 - accuracy: 0.9308 - val_loss: 0.3900 - val_accuracy: 0.8578
Epoch 8/10
391/391 [
] - 43s 109ms/step - loss: 0.1370 - accuracy: 0.9542 - val_loss: 0.4124 - val_accuracy: 0.8641
Epoch 9/10
391/391 [] - 44s 112ms/step - loss: 0.1222 - accuracy: 0.9597 - val_loss: 0.4238 - val_accuracy: 0.8641
Epoch 10/10
391/391 [
] - 44s 113ms/step - loss: 0.1205 - accuracy: 0.9600 - val_loss: 0.4685 - val_accuracy: 0.8568

测试集上

test_loss, test_acc = model.evaluate(test_dataset)
print('Test Loss: {}'.format(test_loss))
print('Test Accuracy: {}'.format(test_acc))

Test Loss: 0.44925960898399353
Test Accuracy: 0.8586400151252747

随着epoch变化,准确度和,loss的变化

4 改进模型

1、使用双层的RNN神经网络,循环单元为仍然为lstm
2、增加一个dropout

model = tf.keras.Sequential([tf.keras.layers.Embedding(encoder.vocab_size, 64),tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64,  return_sequences=True)),tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),tf.keras.layers.Dense(64, activation='relu'),tf.keras.layers.Dropout(0.5),tf.keras.layers.Dense(1)
])

其余同上

测试结果

Test Loss: 0.5735374093055725
Test Accuracy: 0.829039990901947

从准确性来看,并没有升高,这可能是因为数据量对于这个模型来说太少了,所以造成了过拟合,结果较差。

5 实际预测

现在我们随便输入一个句子,使用模型对其情感进行分类。当分数大于等于0.5时是积极的评价,小于0.5时是负面的评价

因为输入的句子的长度可能是不一样的,我们需要对输入的句子用0进行padding(补全)

def pad_to_size(vec, size):zeros = [0] * (size - len(vec))vec.extend(zeros)return vec
def sample_predict(sample_pred_text, pad):encoded_sample_pred_text = encoder.encode(sample_pred_text)if pad:encoded_sample_pred_text = pad_to_size(encoded_sample_pred_text, 64)encoded_sample_pred_text = tf.cast(encoded_sample_pred_text, tf.float32)predictions = model.predict(tf.expand_dims(encoded_sample_pred_text, 0))return (predictions)
sample_pred_text = ('The movie was cool. The animation and the graphics ''were out of this world. I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad=False)
print(predictions)
predictions = sample_predict(sample_pred_text, pad=True)
print(predictions)

[[0.10079887]]
[[0.06816088]]

从结果可以看出padding可以使得结果更加准确。

tensorflow笔记-文本情感分类相关推荐

  1. 李宏毅ML作业笔记4: RNN文本情感分类

    更新中... 目录 任务介绍 文本情感分类 句子喂入RNN的方式 半监督 data格式 代码思路 加载数据集 正确个数计算 word embedding 数据预处理 RNN模型构建 RNN模型训练 改 ...

  2. NLP之基于TextCNN的文本情感分类

    TextCNN 文章目录 TextCNN 1.理论 1.1 基础概念 **最大汇聚(池化)层:** ![请添加图片描述](https://img-blog.csdnimg.cn/10e6e1ed6bf ...

  3. 基于Bert的文本情感分类

    详细代码已上传到github: click me 摘  要:     情感分类是对带有感情色彩的主观性文本进行分析.推理的过程,即分析说话人的态度,推断其所包含的情感类别.传统机器学习在处理情感分类问 ...

  4. 疫情微博文本情感分类 (简化版SMP2020赛题)

    编者按 代码仅供参考,欢迎交流:请勿用于任何形式的课程作业.如有任何错误,敬请批评指正~ Pytorch系列文章: Pytorch实验一:从零实现Logistic回归和Softmax回归 Pytorc ...

  5. 基于Keras搭建LSTM网络实现文本情感分类

    基于Keras搭建LSTM网络实现文本情感分类 一.语料概况 1.1 数据统计 1.1.1 查看样本均衡情况,对label进行统计 1.1.2 计句子长度及长度出现的频数 1.1.3 绘制句子长度累积 ...

  6. 循环神经网络实现文本情感分类之使用LSTM完成文本情感分类

    循环神经网络实现文本情感分类之使用LSTM完成文本情感分类 1. 使用LSTM完成文本情感分类 在前面,使用了word embedding去实现了toy级别的文本情感分类,那么现在在这个模型中添加上L ...

  7. 循环神经网络实现文本情感分类之Pytorch中LSTM和GRU模块使用

    循环神经网络实现文本情感分类之Pytorch中LSTM和GRU模块使用 1. Pytorch中LSTM和GRU模块使用 1.1 LSTM介绍 LSTM和GRU都是由torch.nn提供 通过观察文档, ...

  8. python 多分类情感_文本情感分类(一):传统模型

    前言:四五月份的时候,我参加了两个数据挖掘相关的竞赛,分别是物电学院举办的"亮剑杯",以及第三届 "泰迪杯"全国大学生数据挖掘竞赛.很碰巧的是,两个比赛中,都有 ...

  9. 二十一、文本情感分类二

    1.1 文本训练化概述 深度学习构建模型前需要将文本转化为向量表示(Word Embedding).首先需要将文本转化为数字(文本序列化),在把数字转化为向量. 可以考虑把文本中的每个词语和其对应的数 ...

最新文章

  1. 《强化学习周刊》第7期:强化学习应用之游戏
  2. 解决Fckeditor删除所有上传页面如何上传
  3. adb shell 修改文件名_从零开始学Linux运维|27.Shell编程(函数与参数的传递)
  4. 为什么启动hbase shell后,创建按create 'test', 'cf'失败?
  5. 使用JSLint提高JS代码质量
  6. canvas保存为data:image扩展功能的实现
  7. Spring框架参考手册(4.2.6版本)翻译——第三部分 核心技术 6.4.4 延迟初始化的bean...
  8. 【Ubuntu】VMware下Ubuntu和主机的共享文件夹
  9. 深度学习自学(二十):SmoothL1 和 Softmax交叉熵
  10. python 去重 排序_python中sorted()和set()去重,排序
  11. 服务器的mdf文件怎么打开,mdf文件,教您mdf文件怎么打开
  12. 光纤交换机巡检配置常用命令
  13. 【经验】SQL Server 2008 R2 安装教程
  14. linux中rm件命令,Linux rm命令详解
  15. 用JS生成声音,实现钢琴演奏
  16. 窥视Google Chrome OS
  17. html5中插入样式表方法,如何插入css样式?
  18. 分享给Python程序员的自学路线,不再背负小白名称
  19. 启动redis出现闪退(已解决)
  20. cad图形不见了怎么办_CAD画图突然消失 怎么找回

热门文章

  1. linux新建用户退格键(删除键)无法正常使用的问题总结
  2. 操作系统:什么是中断?
  3. 计算机网络层之 P2P
  4. 基于京东云服务器来搭建javaWeb 运行环境
  5. 如何计算机闲置虚拟机算法_利用闲置计算机的最佳方法
  6. USB Type-C和USB PD
  7. 《通信原理》AM/DSBSC/SSB信号的调制与解调仿真
  8. 计算几何(二) by邓俊辉老师
  9. MindAR初体验——一款js实现的AR库
  10. Tensorflow2.6.0-MKL for C++