【Keras-RNN】IMDb

2023-11-10 23:20
文章标签 keras rnn imdb

本文主要是介绍【Keras-RNN】IMDb,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1 下载数据集
  • 2 数据预处理
  • 3 建立模型
  • 4 训练模型
  • 5 评估模型的准确率
  • 6 预测概率和预测结果
  • 7 查看测试集中的文本和其预测结果
  • 8 测试新的影评
  • 9 保存模型


MLP或者CNN都只能依照当前的状态进行识别,如果处理时间序列的问题,就需要RNN、LSTM模型了。本博客使用 RNN 对 IMDb 数据集进行分析预测,用MLP进行预测可以参考这篇博客 【TensorFlow-MLP】MNIST

这里写图片描述

RNN结构如上图所示,

X t X_t Xt t t t 时间点神经网络的输入

O t O_t Ot t t t 时间点神经网络的输出

( U , V , W ) (U,V,W) U,V,W都是神经网络的参数

S t S_t St 是隐藏状态,代表着 “记忆”

S t = f ( [ U ] X t + [ W ] S t − 1 ) S_{t} = f\left ( [U]X_{t}+ [W]S_{t-1} \right ) St=f([U]Xt+[W]St1)

f f f 表示 ReLU

1 下载数据集

import urllib.request
import os
import tarfile#下载数据集
url="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"
filepath="data/aclImdb_v1.tar.gz"
if not os.path.isfile(filepath):result=urllib.request.urlretrieve(url,filepath)print('downloaded:',result)
# 解压
if not os.path.exists("data/aclImdb"):tfile = tarfile.open("data/aclImdb_v1.tar.gz", 'r:gz')result=tfile.extractall('data/')

2 数据预处理

同 【Keras-MLP】IMDb

from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
import re
re_tag = re.compile(r'<[^>]+>')def rm_tags(text):return re_tag.sub('', text)import os
def read_files(filetype):path = "data/aclImdb/"file_list=[]positive_path=path + filetype+"/pos/"for f in os.listdir(positive_path):file_list+=[positive_path+f]negative_path=path + filetype+"/neg/"for f in os.listdir(negative_path):file_list+=[negative_path+f]print('read',filetype, 'files:',len(file_list))all_labels = ([1] * 12500 + [0] * 12500) all_texts  = []for fi in file_list:with open(fi,encoding='utf8') as file_input:all_texts += [rm_tags(" ".join(file_input.readlines()))]return all_labels,all_texts

开始处理

# 读文件
y_train,train_text=read_files("train")
y_test,test_text=read_files("test")# 建立单词和数字映射的字典
token = Tokenizer(num_words=3800)
token.fit_on_texts(train_text)#将影评的单词映射到数字
x_train_seq = token.texts_to_sequences(train_text)
x_test_seq  = token.texts_to_sequences(test_text)# 让所有影评保持在380个数字
x_train = sequence.pad_sequences(x_train_seq, maxlen=380)
x_test  = sequence.pad_sequences(x_test_seq,  maxlen=380)

output

read train files: 25000
read test files: 25000

3 建立模型

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import SimpleRNNmodel = Sequential()model.add(Embedding(output_dim=32,input_dim=3800, input_length=380))
model.add(Dropout(0.35))# 加了一个简单的RNN层
model.add(SimpleRNN(units=16))model.add(Dense(units=256,activation='relu' ))
model.add(Dropout(0.35))model.add(Dense(units=1,activation='sigmoid' ))model.summary()

output

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, 380, 32)           121600    
_________________________________________________________________
dropout_1 (Dropout)          (None, 380, 32)           0         
_________________________________________________________________
simple_rnn_1 (SimpleRNN)     (None, 16)                784       
_________________________________________________________________
dense_1 (Dense)              (None, 256)               4352      
_________________________________________________________________
dropout_2 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 257       
=================================================================
Total params: 126,993
Trainable params: 126,993
Non-trainable params: 0
_________________________________________________________________

参数计算
3800×32 = 121600

RNN 输入32,输出16 可以理解为3层,入上图
32×16 + 16×16 + 16 = 784

16×256+256 = 4352

256×1 + 1 = 257

4 训练模型

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])train_history =model.fit(x_train, y_train,batch_size=100, epochs=10,verbose=2,validation_split=0.2)

参数说明请参考
【TensorFlow-MLP】MNIST 或者 Keras中文文档

output

Train on 20000 samples, validate on 5000 samples
Epoch 1/10- 26s - loss: 0.4915 - acc: 0.7604 - val_loss: 0.2876 - val_acc: 0.9006
Epoch 2/10- 23s - loss: 0.3209 - acc: 0.8685 - val_loss: 0.4203 - val_acc: 0.8004
Epoch 3/10- 22s - loss: 0.2829 - acc: 0.8879 - val_loss: 0.3567 - val_acc: 0.8744
Epoch 4/10- 22s - loss: 0.2391 - acc: 0.9065 - val_loss: 0.3321 - val_acc: 0.8536
Epoch 5/10- 22s - loss: 0.2187 - acc: 0.9158 - val_loss: 0.4706 - val_acc: 0.8358
Epoch 6/10- 21s - loss: 0.1867 - acc: 0.9290 - val_loss: 0.5693 - val_acc: 0.8118
Epoch 7/10- 22s - loss: 0.1622 - acc: 0.9378 - val_loss: 0.6032 - val_acc: 0.8078
Epoch 8/10- 22s - loss: 0.1785 - acc: 0.9307 - val_loss: 0.4812 - val_acc: 0.8424
Epoch 9/10- 21s - loss: 0.1424 - acc: 0.9478 - val_loss: 0.6116 - val_acc: 0.8094
Epoch 10/10- 22s - loss: 0.1196 - acc: 0.9548 - val_loss: 0.9426 - val_acc: 0.7768

可视化结果

import matplotlib.pyplot as plt
def show_train_history(train_history,train,validation):plt.plot(train_history.history[train])plt.plot(train_history.history[validation])plt.title('Train History')plt.ylabel(train)plt.xlabel('Epoch')plt.legend(['train', 'validation'], loc='upper left')plt.savefig('1.png')plt.show()

调用查看精度变化

show_train_history(train_history,'acc','val_acc')

这里写图片描述

调用查看损失变化

show_train_history(train_history,'loss','val_loss')

这里写图片描述

5 评估模型的准确率

scores = model.evaluate(x_test, y_test, verbose=1)
scores[1]

output

25000/25000 [==============================] - 18s 706us/step
0.84692

Note:scores[0] 为损失

6 预测概率和预测结果

查看输出的概率

probility=model.predict(x_test)
probility[:10]

output

array([[0.999658  ],[0.9902057 ],[0.99913883],[0.99437803],[0.9984084 ],[0.9932498 ],[0.99893945],[0.9921451 ],[0.9777389 ],[0.8260359 ]], dtype=float32)

查看输出的结果,大于0.5的为1.小于0.5的为0

predict=model.predict_classes(x_test)
predict[:10]

output

array([[1],[1],[1],[1],[1],[1],[1],[1],[1],[1]], dtype=int32)

7 查看测试集中的文本和其预测结果

SentimentDict={1:'正面的',0:'负面的'}
def display_test_Sentiment(i):print(test_text[i])print('label:',SentimentDict[y_test[i]],'预测结果:',SentimentDict[predict[i][0]])

调用

display_test_Sentiment(2)

output

BLACK WATER is a thriller that manages to completely transcend it’s limitations (it’s an indie flick) by continually subverting expectations to emerge as an intense experience.In the tradition of all good animal centered thrillers ie Jaws, The Edge, the original Cat People, the directors know that restraint and what isn’t shown are the best ways to pack a punch. The performances are real and gripping, the crocdodile is extremely well done, indeed if the Black Water website is to be believed that’s because they used real crocs and the swamp location is fabulous.If you are after a B-grade gore fest croc romp forget Black Water but if you want a clever, suspenseful ride that will have you fearing the water and wondering what the hell would I do if i was up that tree then it’s a must see.
label: 正面的 预测结果: 正面的

8 测试新的影评

http://www.imdb.com/title/tt2771200

def predict_review(input_text):# 影评转换为数字列表input_seq = token.texts_to_sequences([input_text])# 截断数字列表使得所有输入长度为380pad_input_seq  = sequence.pad_sequences(input_seq , maxlen=380)# 预测分类结果predict_result=model.predict_classes(pad_input_seq)# 输出结果print(SentimentDict[predict_result[0][0]])

调用

predict_review(’’’
As a fan of the original Disney film (Personally I feel it’s their masterpiece) I was taken aback to the fact that a new version was in the making. Still excited I had high hopes for the film. Most of was shattered in the first 10 minutes. Campy acting with badly performed singing starts off a long journey holding hands with some of the worst CGI Hollywood have managed to but to screen in ages.
A film that is over 50% GCI, should focus on making that part believable, unfortunately for this film, it’s far from that. It looks like the original film was ripped apart frame by frame and the beautiful hand-painted drawings have been replaced with digital caricatures. Besides CGI that is bad, it’s mostly creepy. As the little teacup boy will give me nightmares for several nights to come. Emma Watson plays the same character as she always does, with very little acting effort and very little conviction as Belle. Although I can see why she was cast in the film based on merits, she is far from the right choice for the role. Dan Stevens does alright under as some motion captured dead-eyed Beast, but his performance feels flat as well. Luke Evans makes for a great pompous Gaston, but a character that has little depth doesn’t really make for a great viewing experience. Josh Gad is a great comic relief just like the original movie’s LeFou. Other than that, none of the cast stands out enough for me to remember them. Human or CHI creature. I was just bored through out the whole experience. And for a project costing $160 000 000, I can see why the PR department is pushing it so hard because they really need to get some cash back on this pile of wet stinky CGI-fur!
All and all, I might be bias from really loving Disney’s first adaptation. That for me marks the high-point of all their work, perfectly combining the skills of their animators along with some CGI in a majestic blend. This film however is more like the bucket you wash off your paintbrush in, it has all the same colors, but muddled with water and to thin to make a captivating story from. The film is quite frankly not worth your time, you would be better off watching the original one more time.
‘’’)

output

负面的

9 保存模型

model_json = model.to_json()
with open("SaveModel/Imdb_RNN_model.json", "w") as json_file:json_file.write(model_json)model.save_weights("SaveModel/Imdb_RNN_model.h5")
print("Saved model to disk")

声明:代码源于《TensorFlow+Keras深度学习人工智能实践应用》 林大贵版,引用、转载请注明出处,谢谢,如果对书本感兴趣,买一本看看吧!!!

这篇关于【Keras-RNN】IMDb的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/385936

相关文章

RNN发展(RNN/LSTM/GRU/GNMT/transformer/RWKV)

RNN到GRU参考: https://blog.csdn.net/weixin_36378508/article/details/115101779 tRANSFORMERS参考: seq2seq到attention到transformer理解 GNMT 2016年9月 谷歌,基于神经网络的翻译系统(GNMT),并宣称GNMT在多个主要语言对的翻译中将翻译误差降低了55%-85%以上, G

Kaggle刷比赛的利器,LR,LGBM,XGBoost,Keras

刷比赛利器,感谢分享的人。 摘要 最近打各种比赛,在这里分享一些General Model,稍微改改就能用的 环境: python 3.5.2 XGBoost调参大全: http://blog.csdn.net/han_xiaoyang/article/details/52665396 XGBoost 官方API: http://xgboost.readthedocs.io/en

白话RNN系列(七)

本文,探讨下LSTM的一些高级应用,比如双向LSTM。 前面的探讨过程中, 我们使用到的RNN或者LSTM都是单向的,即按照时间顺序排列的一维序列;而在实际应用中,双向的RNN由于考虑到更充足的上下文,往往能起到更好的效果: Bi-RNN又叫双向RNN,是采用了两个方向的RNN网络。 RNN网络擅长的是对于连续数据的处理,既然是连续的数据规律,我们不仅可以学习它的正向规律,还可以学习它的反向规

白话RNN系列(六)

上文给出了一个LSTM使用的具体例子,但其中依旧存在一些东西说的不是很清楚明白,接下来,我们会针对LSTM使用中更加细致的一些东西,做一些介绍。 本人目前使用的基本都是TensorFlow进行开发。 lstm_cell = tf.nn.rnn_cell.LSTMCell(n_hidden, forget_bias=1.0, name='basic_lstm_cell')outputs, st

白话RNN系列(五)

前文,对于LSTM的结构进行了系统的介绍,本文,通过一个MNIST_data的例子,争取能够把LSTM的基本使用来吃透。 import tensorflow as tfimport input_data# 导入 MINST 数据集# from tensorflow.examples.tutorials.mnist import input_data# one_hot=True,代表输入的

白话RNN系列(四)

本文,谈谈RNN的一个变种,也是目前使用比较广泛的神经网络LSTM,我们首先描述下LSTM的基本结构,然后给出一个具体的使用LSTM的例子,帮助大家尽快掌握LSTM的原理和基本使用方法; 这可能是一张大家熟悉地不能再熟悉的图片了。 我们可以将其与RNN的基本结构进行对比:  我们可以看到区别:RNN中,每个循环体会产生一份输出,即隐藏状态;最终输出由此隐藏状态产出,同时,隐藏状态会保

白话RNN系列(三)

紧接上文,白话RNN系列(二)。 通过generateData得到我们的样本数据之后,我们开始搭建自己的RNN: # 每个批次输入的数据,这里定义为5,即每个批次输入5个数据batch_size = 5# RNN中循环的次数,即时间序列的长度# 这里取长度为15的时间序列truncated_backprop_length = 15# 与时间序列相对应,占位符的维度为 5 * 15#

白话RNN系列(二)

紧接白话谈RNN系列(一) 上文讨论了基础的全连接神经网络,本文,我们来说说RNN。 首先,RNN相比于普通的神经网络,有什么改进? 两点比较突出:权值共享和隐层神经元节点的有序连接。 直接上图,浅显易懂: 上图,摘自深度学习(花书),左侧图和右侧图表达了相同的含义,我们以右侧图为例,并配合实例,对RNN进行透彻的分析,我尽可能以很通俗移动的方式把RNN讲明白。 从本图中,我们很清

白话RNN系列(一)

RNN,循环神经网络,全称Recurrent Neural Network。 本文,从RNN的基本原理讲起,会探讨RNN的前向传播和反向传播,并通过一些浅显易懂的小例子,展示RNN这个东东的神奇之处,尽最大可能以通俗易懂的方式,让看到本文的童鞋都能够掌握RNN。 1:RNN的基本原理 即便是RNN,也依旧脱离不了神经网络的基本架构,换句话说,我们看RNN的时候,一定要记住一句,它不过是高级一

深度学习 之 keras

注意使用keras 首先压迫安装theano 或者tensorflow,keras默认使用tensorflow   首先创建一个moel from keras.models import Sequentialmodel = Sequential()   然后添加神经层及激活函数 from keras.layers import Dense, Activationmodel.ad