当前位置: 首页 > news >正文

NLP——电影评论情感分析

python-tensorflow2.0
numpy 1.19.1
tensorflow 2.0.0

导入库

数据加载

数据处理

构建模型

训练

评估

预测

1.基于2层dropout神经网络

2.基于LSTM的网络

#导入需要用到的库
import os
import tarfile
import urllib. request
import tensorflow as tf
import numpy as np
import re
import string
from random import randint
数据地址
ur1="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"
#数据存放路径
filepath="D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data\\aclImdb_v1.tar.gz"
#如果当前目录下不存在data文件夹,则建立
if not os. path.exists("D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data"):os.makedirs("D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data" )
#下载数据,80兆左右
if not os.path.isfile(filepath) :print('downloading...')result=urllib.request.urlretrieve(url, filepath)print('downloaded:',result)
else:print(filepath,'is existed!')
#解压数据
if not os.path.exists('D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data"):tfile=tarfile.open (filepath,"r:gz" )print('extracting...' )result=tfile.extractall("D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data\\")print("extraction completed")
else:print("data/aclImdb is existed!")

在这里插入图片描述

#将文本中不需要的字符清除,如html标签<br />
def remove_tags(text) :re_tag = re.compile(r'<[^>]+>')return re_tag.sub ('',text)
#读取文件
def read_files(filetype) :path ="D:\\课程学习\\深度学习\\深度学习应用开发-TensorFlow实践_浙江大学\\data\\aclImdb\\"file_list=[]#读取正面评价的文件的路径,存到file_list列表里positive_path=path + filetype+"\\pos\\"for f in os.listdir(positive_path):file_list+=[positive_path+f]pos_files_num=len(file_list)#读取负面评价的文件的路径,存到file_ list列表里negative_path=path + filetype+"\\neg\\"for f in os.listdir (negative_path) :file_list+=[negative_path+f]neg_files_num=len(file_list)-pos_files_numprint('read' , filetype,'files:', len(file_list))print(pos_files_num,'pos files in' , filetype,'files')print(neg_files_num,'neg files in' , filetype,'files')#得到所有标签。标签用one hot编码表示, 正面评价标签为[1 0], 负面评价标签为[0 1]all_labels = ([[1,0]] * pos_files_num + [[0,1]] * neg_files_num)#得到所有文本。all_texts=[]for fi in file_list:with open (fi, encoding='utf8' ) as file_input:#文本中有<br />这类html标签, 将文本传入remove_ tags函数#函数里使用正则表达式可以将这样的标签清除掉。all_texts += [remove_tags(" ". join(file_input.readlines()))]return all_labels,all_texts
#读取数据集
#得到训练与测试用的标签和文本
train_labels, train_texts=read_files("train" )
test_labels, test_texts=read_files("test" )

在这里插入图片描述

#查看数据、标签
print ("训练数据")
print("正面评价:")
print(train_texts[0])
print (train_labels[0])
print("负面评价:")
print (train_texts[12500])
print (train_labels[12500])
print ("测试数据")
print("正面评价:")
print(test_texts[0])
print (test_labels[0])
print("负面评价:")
print (test_texts[12500])
print (test_labels[12500])

在这里插入图片描述

数据处理

#建立词汇词典Token
#建立Token
token =tf.keras.preprocessing.text.Tokenizer(num_words=4000)
token.fit_on_texts(train_texts)
#查看token读取了多少文档
token.document_count

#将单词(字符串)映射为它们的排名或者索引
print(token.word_index)
#将单词(字符串)映射为它们在训练期间所出现的文档或文本的数量
token.word_docs

在这里插入图片描述

#查看Token中词汇出现的频次排名
print (token.word_counts)

在这里插入图片描述

#文字转数字列表
train_sequences = token.texts_to_sequences(train_texts)
test_sequences = token.texts_to_sequences(test_texts)
print (train_texts[0])
print (train_sequences[0])
print (len(train_sequences[0]))

在这里插入图片描述

#让转换后的数字列表长度相同 
x_train = tf.keras.preprocessing.sequence.pad_sequences (train_sequences,padding='post',truncating='post',maxlen=400)
x_test = tf.keras.preprocessing.sequence.pad_sequences (test_sequences,padding='post',truncating='post',maxlen=400)
x_train.shape

在这里插入图片描述

#填充后的数字列表
print(x_train[0])
print(len(x_train[0]))

在这里插入图片描述

y_train=np.array(train_labels)
y_test=np.array(test_labels)
print(y_train.shape)
print(y_test.shape)

在这里插入图片描述

构建模型

model = tf.keras.models.Sequential()
model.add (tf.keras.layers.Embedding (output_dim=32,## 输出词向量的维度input_dim=4000,## 输入词汇表的长度,最大词汇数+1input_length=400))# 输入Tensor的长度
model.add (tf.keras.layers.Flatten())
#用GlobalAveragePoolingID也起到平坦化的效果
# mode1. add (keras. layers. GlobalAveragePoolingIDO)
model.add (tf.keras.layers.Dense (units=256,activation='relu' ))
model.add (tf.keras.layers.Dropout (0.3))
model.add (tf.keras.layers.Dense (units=2, activation='softmax'))
model.summary()

在这里插入图片描述

#模型设置与训练
model.compile (optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
history = model.fit(x_train, y_train,validation_split=0.2,epochs=10, batch_size=128,verbose=1)

在这里插入图片描述

import matplotlib.pyplot as plt
acc = history.history['accuracy' ]
val_acc = history.history['val_accuracy' ]
loss = history.history['loss' ]
val_loss = history.history['val_loss' ]
epochs = range(1, len(acc) + 1)
plt.plot (epochs, loss, 'r',label='Training loss' )
plt.plot (epochs, val_loss, 'b' ,label='Validation loss' )
plt.title('Training and validation loss' )
plt.xlabel( 'Epochs' )
plt.ylabel('Loss' )
plt.legend ()
plt.show()
plt.clf()
# clear figure
acc_values = history.history['accuracy']
val_acc_values = history.history['val_accuracy']
plt.plot (epochs,acc,'r',label='Training acc' )
plt.plot (epochs,val_acc,'b',label='Validation acc' )
plt.title('Training and validation accuracy' )
plt.xlabel('Epochs' )
plt.ylabel('Accuracy' )
plt.legend()
plt.show()

在这里插入图片描述
在这里插入图片描述

#评估模型准确率
test_1oss,test_acc = model.evaluate(x_test, y_test,verbose=1)
print(' Test accuracy:',test_acc)

在这里插入图片描述

#执行模型预测
predictions = model.predict(x_test)
predictions[0]

在这里插入图片描述

#定义预测结果显示函数
sentiment_dict = {0:'pos', 1:'neg' }
def display_test_sentiment(i) :print(test_texts[i])print('label value:', sentiment_dict[np.argmax(y_test[i])],'predict value:' , sentiment_dict[np.argmax(predictions[i])])
#查看预测结果
display_test_sentiment(0)

在这里插入图片描述

#文本情感分析模型应用
review_text="So much amazing action and beautiful cinematography makes for such an enlightening experience! In The Empire Strikes Back you know who everyone is which is great plus Yoda is introduced! I love this movie the music is soothing, there's romance, more of Darth Vader, and introduces Emperor Palpatine what more can you ask for? A lot to relish and get excited about; it's such a classic gem."input_seq = token.texts_to_sequences([review_text])
pad_input_seq =tf.keras.preprocessing.sequence.pad_sequences(input_seq,padding='post',truncating='post' ,maxlen=400)
pred = model.predict (pad_input_seq)
print('predict value:', sentiment_dict[np.argmax(pred)])

在这里插入图片描述

sentiment_dict = {0:' pos',1:'neg' }
def display_text_sentiment (text):print(text)input_seq = token.texts_to_sequences([text])pad_input_seq =tf.keras.preprocessing.sequence.pad_sequences(input_seq, padding='post',truncating='post' ,maxlen=400)pred = model.predict(pad_input_seq)print('predict value:', sentiment_dict[np.argmax(pred)])
display_text_sentiment(review_text) 

在这里插入图片描述

基于LSTM结构的模型构建

#建立模型
model = tf.keras.models.Sequential()
model.add (tf.keras.layers.Embedding (output_dim=32,input_dim=4000,input_length=400) )
#用RNN,不用把词嵌入层平坦化
# mode1. add (keras. layers. SimpleRNV(units=16))
model.add (tf.keras.layers.Bidirectional (tf.keras.layers.LSTM(units=8)))
model.add (tf.keras.layers.Dense (units=32,activation='relu' ))
model.add (tf.keras.layers.Dropout (0.3))
model.add (tf.keras.layers.Dense (units=2, activation='softmax' ))
model.summary()

在这里插入图片描述

#模型设置与训练
#标签是One -Hot编码的多分类模型,损失函数用categorical crossentropy
#标签不是0ne -Hot编码的多分类模型,损失函数用sparse. categorical .crossentropy
#标签是二分类,损失函数用binary_ crossentropy
model.compile(optimizer='adam',loss='categorical_crossentropy', #二二 分类metrics=['accuracy' ])
history = model.fit(x_train, y_train,validation_split=0.2,epochs=6,batch_size=128,verbose=1)

在这里插入图片描述

#评估模型准确率
import matplotlib.pyplot as plt
acc = history.history['accuracy' ]
val_acc = history.history['val_accuracy' ]
loss = history.history['loss' ]
val_loss = history.history['val_loss' ]
epochs = range(1, len(acc) + 1)
plt.plot (epochs, loss, 'r',label='Training loss' )
plt.plot (epochs, val_loss, 'b' ,label='Validation loss' )
plt.title('Training and validation loss' )
plt.xlabel( 'Epochs' )
plt.ylabel('Loss' )
plt.legend ()
plt.show()
plt.clf()
# clear figure
acc_values = history.history['accuracy']
val_acc_values = history.history['val_accuracy']
plt.plot (epochs,acc,'r',label='Training acc' )
plt.plot (epochs,val_acc,'b',label='Validation acc' )
plt.title('Training and validation accuracy' )
plt.xlabel('Epochs' )
plt.ylabel('Accuracy' )
plt.legend()
plt.show()

在这里插入图片描述
在这里插入图片描述

#评估模型准确率
test_1oss,test_acc = model.evaluate(x_test, y_test,verbose=1)
print(' Test accuracy:',test_acc)

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • nginx配置https协议(测试环境)
  • PDF格式分析(八十五)——水印注释(Watermark)
  • 如何开发一个直播APP:功能介绍与开发步骤详解
  • 实时通信websocket和sse
  • 关于自学编程的9点忠告
  • 【BeX5】知识中心
  • Android启动流程
  • 在 Windows 操作系统中,可以通过命令行工具来杀死进程
  • Matlab|基于主从博弈的智能小区代理商定价策略及电动汽车充电管理
  • Google Chrome谷歌浏览器怎么立刻更新书签
  • MySQL与PostgreSQL关键对比三(索引类型)
  • 高效处理海量慢SQL日志文件:Java与JSQLParser去重方案详解
  • Linux:多线程的操作
  • 边缘计算(Edge Computing)_关键概念/优势/应用场景
  • 【SkiaSharp绘图03】SKPaint详解(一)BlendMode混合模式、ColorFilter颜色滤镜
  • 【Linux系统编程】快速查找errno错误码信息
  • 4个实用的微服务测试策略
  • docker python 配置
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • JS+CSS实现数字滚动
  • JS基础之数据类型、对象、原型、原型链、继承
  • Mithril.js 入门介绍
  • mysql innodb 索引使用指南
  • python docx文档转html页面
  • Rancher如何对接Ceph-RBD块存储
  • Vue.js-Day01
  • WebSocket使用
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 我的zsh配置, 2019最新方案
  • 一份游戏开发学习路线
  • hi-nginx-1.3.4编译安装
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​queue --- 一个同步的队列类​
  • ​批处理文件中的errorlevel用法
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #NOIP 2014#Day.2 T3 解方程
  • #每日一题合集#牛客JZ23-JZ33
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (19)夹钳(用于送货)
  • (2022 CVPR) Unbiased Teacher v2
  • (24)(24.1) FPV和仿真的机载OSD(三)
  • (C语言)逆序输出字符串
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (力扣)循环队列的实现与详解(C语言)
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (四)c52学习之旅-流水LED灯
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)Google的Objective-C编码规范
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法