一维卷积和LSTM模型的比较

更新时间:2023-07-14 22:11:04 阅读: 评论:0

班级二三事⼀维卷积和LSTM模型的⽐较
本篇⽂章会分析⽐较LSTM模型和⼀维卷积模型的特点。
在进⾏⽐较之前,我们需要加载之前训练好的两个模型。
dels import load_model
from keras.datats import imdb
from keras.preprocessing.quence import pad_quences
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 = _word_index()
word = place(","," ").replace("."," ").replace("?"," ").replace("!"," ").replace("'s'"," is").replace('"',"").replace(":","").replace("  "," ").strip()    word = word.lower()
word_list = word.split(" ")
return[((i,-3)+3)for i in word_list]
我们尝试编码这样⼀句话。
print(encode_word("I think it's a great movie. People want to e 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_quences(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_quences(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 en 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 en 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 en since I wa s 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 cond. 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 cond. This movie is just making trouble. I don't understand what the director thinks. This kind of st ory 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 cond. 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 en 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 cond. 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 ries 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 ries love apartment is very good! But the movie is really, the plot is terrible. I have no idea what it's talking ab out. 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 b ad and too fake."))
预测结果如下:
the score is:0.7587981224060059    good
模型预测为正⾯影评,给出了正⾯评论的概率为0.7587981224060059
再来看看LSTM模型的预测:
普通话宣传标语print(lstm_predict("To be honest, the TV ries 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 ries 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这个转折,然后将后⾯的评价的权重放⼤,它理解了这个句⼦的前因后果,于是预测这条影评为负⾯。
到这⾥⼤家可以看出两个模型的区别的吧,两者看待问题的⽅式都不同,这是由于模型的结构所导致。
希望这篇⽂章能够帮助⼤家理解到两个模型的区别。

本文发布于:2023-07-14 22:11:04,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1081633.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模型   预测   卷积   影评   给出   电影   整数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图