一维卷积和LSTM模型的比较

本篇文章会分析比较LSTM模型和一维卷积模型的特点。
在进行比较之前,我们需要加载之前训练好的两个模型。

from keras.models import load_model
from keras.datasets import imdb
from keras.preprocessing.sequence import pad_sequences
model_conv = load_model("my_model_conv1d.h5")
model_lstm = load_model("my_model_lstm.h5")

下面提供了一个函数,用于编码数据

import numpy as np
#将字符串编码为整数序列
def encode_word(word):
	#下载imdb内置的单词整数映射字典
    word_index = imdb.get_word_index()
    word = word.replace(","," ").replace("."," ").replace("?"," ").replace("!"," ").replace("'s'"," is").replace('"',"").replace(":","").replace("  "," ").strip()
    word = word.lower()
    word_list = word.split(" ")
    return [(word_index.get(i,-3)+3) for i in word_list]

我们尝试编码这样一句话。

print(encode_word("I think it's a great movie. People want to see it again."))

输出结果如下:

[13, 104, 45, 6, 87, 20, 84, 181, 8, 67, 12, 174]

下面两个函数是用来使用一维卷积预测和LSTM模型预测的函数

#一维卷积模型预测
def conv1d_predict(word):
    word = encode_word(word)
    word = np.array(word)
    word[word>10000] = 0
    word = np.expand_dims(word,0)
    word = pad_sequences(word,maxlen=maxlen)
    imdb_score = model_lstm.predict(word)[0][0]
    if imdb_score>0.5:
        return "the score is:{}    good".format(imdb_score)
    return "the score is:{}    bad".format(imdb_score)
#LSTM模型预测
def lstm_predict(word):
    word = encode_word(word)
    word = np.array(word)
    word[word>10000] = 0
    word = np.expand_dims(word,0)
    word = pad_sequences(word,maxlen=maxlen)
    imdb_score = model_lstm.predict(word)[0][0]
    if imdb_score>0.5:
        return "the score is:{}    good".format(imdb_score)
    return "the score is:{}    bad".format(imdb_score)

准备工作做好了,下面来进行分析。
我们首先看这样一条影评:
这是一部很棒的电影。你无法想象我第一次看到它时的震惊。天哪!这是我出生以来看过的最好的电影。没有比这更好的了。我相信大多数人会和我保持同样的看法。
其英文翻译为:
It’s such a great movie. You can’t imagine the shock when I saw it for the first time. My God! It’s the best movie I’ve ever seen since I was born. There’s nothing better. I believe that most people will keep the same view as me.
使用了一维卷积的模型预测为:

print(conv1d_predict("It's such a great movie. You can't imagine the shock when I saw it for the first time. My God! It's the best movie I've ever seen since I was born. There's nothing better. I believe that most people will keep the same view as me."))

预测结果如下:

the score is:0.9934569001197815    good

模型预测为正面影评,给出了正面评论的概率为0.9934569001197815
再来看看LSTM模型的预测:

print(lstm_predict("It's such a great movie. You can't imagine the shock when I saw it for the first time. My God! It's the best movie I've ever seen since I was born. There's nothing better. I believe that most people will keep the same view as me."))

预测结果如下:

the score is:0.8504339456558228    good

模型预测为正面影评,给出了正面评论的概率为0.8504339456558228

然后再看第二条影评:
太糟糕了。我一秒钟都看不下去。这部电影只是制造麻烦。我不明白主任是怎么想的。这样的故事也可以编出来。是给孩子们看的。
其英文翻译为:
It’s too bad. I can’t watch it for a second. This movie is just making trouble. I don’t understand what the director thinks. This kind of story can also be made. It’s for children to watch.
使用了一维卷积的模型预测为:

print(conv1d_predict("It's too bad. I can't watch it for a second. This movie is just making trouble. I don't understand what the director thinks. This kind of story can also be made. It's for children to watch."))

预测结果如下:

the score is:0.02947079949080944    bad

模型预测为负面影评,给出了正面评论的概率为0.02947079949080944
再来看看LSTM模型的预测:

print(lstm_predict("It's too bad. I can't watch it for a second. This movie is just making trouble. I don't understand what the director thinks. This kind of story can also be made. It's for children to watch."))

预测结果如下:

the score is:0.20686979591846466    bad

模型预测为负面影评,给出了正面评论的概率为0.20686979591846466


从上面两个结果来看,似乎一维卷积模型的效果更加好, 因为它预测的概率更加准确。
我们分析一下第一条评论:
It’s such a great movie. You can’t imagine the shock when I saw it for the first time. My God! It’s the best movie I’ve ever seen since I was born. There’s nothing better. I believe that most people will keep the same view as me.
一维卷积模型捕获的特征是没有顺序的,它似乎捕获到了great movie,best movie,shock这些词汇,于是给出了较高的概率,而LSTM模型无法捕获这些词汇,它会从顺序的角度来看这条影评,它能捕获因果关系。
再来看第二条影评:
It’s too bad. I can’t watch it for a second. This movie is just making trouble. I don’t understand what the director thinks. This kind of story can also be made. It’s for children to watch.
同理,一维卷积模型似乎捕获到了bad,making trouble这类的负面词汇,于是给出了为正面评论较低的概率,而LSTM就没有这么聪明了,它只能依顺序来看,然后进行预测。
从结果上来看,两者的预测都是正确的,但是貌似一维卷积模型效果更好,它似乎更加聪明,只找了一些表达情绪的词汇就判断出了这条影评是正面还是负面。那么我们当然也认为一维卷积模型更加出色吗?

口说无凭,我们再接着往下看。

下面我们用一条爱情公寓电影的影评来看看两个模型的效果(博主这里只是借用别人的评论,本人对此不发表任何看法)
这是第三条影评:
老实说,电视连续剧《爱情公寓》还是很不错的!但这部电影真的,情节太糟糕了。我完全不明白它在说什么。一会儿是现实,一会儿是科幻小说(主角的光环),一会儿是现实。只是没有焦点。太频繁了。结局太差太假。
其英文翻译如下:
To be honest, the TV series love apartment is very good! But the movie is really, the plot is terrible. I have no idea what it’s talking about. One moment is reality, one moment is science fiction (the aura of the protagonist), one moment is reality. Just no focus. Too often. The ending is too bad and too fake.
使用了一维卷积的模型预测为:

print(conv1d_predict("To be honest, the TV series love apartment is very good! But the movie is really, the plot is terrible. I have no idea what it's talking about. One moment is reality, one moment is science fiction (the aura of the protagonist), one moment is reality. Just no focus. Too often. The ending is too bad and too fake."))

预测结果如下:

the score is:0.7587981224060059    good

模型预测为正面影评,给出了正面评论的概率为0.7587981224060059
再来看看LSTM模型的预测:

print(lstm_predict("To be honest, the TV series love apartment is very good! But the movie is really, the plot is terrible. I have no idea what it's talking about. One moment is reality, one moment is science fiction (the aura of the protagonist), one moment is reality. Just no focus. Too often. The ending is too bad and too fake."))

预测结果如下:

the score is:0.21752914786338806    bad

模型预测为负面影评,给出了正面评论的概率为0.21752914786338806

到这里大家就看出差别了。
一维卷积模型给出了错误的预测,它预测结果为正面,给出了正面的概率为0.7587981224060059,而LSTM模型给出了正确的预测,它预测结果为负面,给出了正面的概率为0.21752914786338806。那么为什么会出现这样的情况了?刚刚我们不是觉得一维卷积模型的预测更为准确吗?这是由于模型的特性不同。
且看评论:
To be honest, the TV series love apartment is very good! But the movie is really, the plot is terrible. I have no idea what it’s talking about. One moment is reality, one moment is science fiction (the aura of the protagonist), one moment is reality. Just no focus. Too often. The ending is too bad and too fake.
这里一维卷积模型似乎是捕获到了very good!同时也捕获到了is terrible,在我们看来人家明明是说的TV好,但电影差,可是一维卷积模型就没有这么聪明了,它只聚焦到了这些词上,虽然这两个词的意思是相反的,但是它似乎觉得正面的概率还是稍微高一点,于是给出了0.7587981224060059的预测概率。我们可以从概率上看到它似乎也有一点纠结,不像前两次预测都是那么肯定。
而LSTM模型可是从顺序来看这条影评的啊,它意识到了But这个转折,然后将后面的评价的权重放大,它理解了这个句子的前因后果,于是预测这条影评为负面。

到这里大家可以看出两个模型的区别的吧,两者看待问题的方式都不同,这是由于模型的结构所导致。

希望这篇文章能够帮助大家理解到两个模型的区别。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二维CNN和LSTM模型是一种常用的深度学习模型结构,用于处理具有时序信息的二维数据。在这种模型中,CNN用于提取输入数据的空间特征,而LSTM则用于捕捉输入数据的时序关系。 具体来说,二维CNN模型通过卷积层、池化层和全连接层等组件,对输入的二维数据进行特征提取和降维。这些层可以通过多个卷积核和滤波器来学习不同的特征。CNN模型可以有效地捕捉到输入数据的空间局部性和层次结构。 而LSTM模型则是一种递归神经网络,专门用于处理序列数据。LSTM通过门控单元的设计,可以有效地捕捉到输入数据的时序依赖关系。它可以记忆和更新长期记忆,并根据当前输入和之前的记忆状态来生成输出。 在二维CNN和LSTM模型中,通常会将CNN模型的输出作为LSTM模型的输入。这样可以将CNN提取到的空间特征传递给LSTM模型,以便LSTM模型能够更好地理解输入数据的时序关系。通过这种结构,模型可以同时考虑到输入数据的空间和时序信息,从而更好地进行预测和分类任务。 总结起来,二维CNN和LSTM模型是一种结合了空间特征提取和时序关系建模的深度学习模型。它可以应用于多种任务,如图像分类、视频分析和自然语言处理等领域。通过合理设计和训练,这种模型可以在处理具有时序信息的二维数据时取得较好的性能。 #### 引用[.reference_title] - *1* *2* [LSTM 06:Keras实现CNN-LSTM模型](https://blog.csdn.net/weixin_39653948/article/details/104982310)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [LSTM+CNN模型厄尔尼诺事件预测](https://blog.csdn.net/qq_45957458/article/details/127564034)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值