! [ -e /content ] && pip install -Uqq fastai # 在Colab上升级fastai
文本迁移学习
from fastai.text.all import *
如何微调语言模型并训练分类器
在本教程中,我们将学习如何训练一个模型来对文本进行分类(这里基于其情感)。首先,我们将看到如何用几行代码快速实现,然后如何使用ULMFit 论文的方法获得最先进的结果。
我们将使用来自学习单词向量进行情感分析的IMDb数据集,该数据集包含几千条电影评论。
从预训练模型训练文本分类器
我们将尝试使用预训练模型训练分类器,这有点类似于我们在视觉教程中所做的。为了准备数据,我们将首先使用高级 API:
使用高级API
我们可以使用以下命令下载数据并解压缩:
= untar_data(URLs.IMDB)
path path.ls()
(#5) [Path('/home/sgugger/.fastai/data/imdb/unsup'),Path('/home/sgugger/.fastai/data/imdb/models'),Path('/home/sgugger/.fastai/data/imdb/train'),Path('/home/sgugger/.fastai/data/imdb/test'),Path('/home/sgugger/.fastai/data/imdb/README')]
/'train').ls() (path
(#4) [Path('/home/sgugger/.fastai/data/imdb/train/pos'),Path('/home/sgugger/.fastai/data/imdb/train/unsupBow.feat'),Path('/home/sgugger/.fastai/data/imdb/train/labeledBow.feat'),Path('/home/sgugger/.fastai/data/imdb/train/neg')]
数据遵循类似ImageNet的组织方式,在训练文件夹中,我们有两个子文件夹,pos
和neg
(分别用于正面评论和负面评论)。我们可以使用TextDataLoaders.from_folder
方法来收集这些数据。我们需要指定的唯一内容是验证文件夹的名称,应该是“test”(而不是默认的“valid”)。
= TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test') dls
我们可以使用 show_batch
方法查看数据:
dls.show_batch()
text | category | |
---|---|---|
0 | xxbos xxmaj match 1 : xxmaj tag xxmaj team xxmaj table xxmaj match xxmaj bubba xxmaj ray and xxmaj spike xxmaj dudley vs xxmaj eddie xxmaj guerrero and xxmaj chris xxmaj benoit xxmaj bubba xxmaj ray and xxmaj spike xxmaj dudley started things off with a xxmaj tag xxmaj team xxmaj table xxmaj match against xxmaj eddie xxmaj guerrero and xxmaj chris xxmaj benoit . xxmaj according to the rules of the match , both opponents have to go through tables in order to get the win . xxmaj benoit and xxmaj guerrero heated up early on by taking turns hammering first xxmaj spike and then xxmaj bubba xxmaj ray . a xxmaj german xxunk by xxmaj benoit to xxmaj bubba took the wind out of the xxmaj dudley brother . xxmaj spike tried to help his brother , but the referee restrained him while xxmaj benoit and xxmaj guerrero | pos |
1 | xxbos xxmaj warning : xxmaj does contain spoilers . \n\n xxmaj open xxmaj your xxmaj eyes \n\n xxmaj if you have not seen this film and plan on doing so , just stop reading here and take my word for it . xxmaj you have to see this film . i have seen it four times so far and i still have n't made up my mind as to what exactly happened in the film . xxmaj that is all i am going to say because if you have not seen this film , then stop reading right now . \n\n xxmaj if you are still reading then i am going to pose some questions to you and maybe if anyone has any answers you can email me and let me know what you think . \n\n i remember my xxmaj grade 11 xxmaj english teacher quite well . xxmaj | pos |
2 | xxbos i thought that xxup rotj was clearly the best out of the three xxmaj star xxmaj wars movies . i find it surprising that xxup rotj is considered the weakest installment in the xxmaj trilogy by many who have voted . xxmaj to me it seemed like xxup rotj was the best because it had the most profound plot , the most suspense , surprises , most xxunk the ending ) and definitely the most episodic movie . i personally like the xxmaj empire xxmaj strikes xxmaj back a lot also but i think it is slightly less good than than xxup rotj since it was slower - moving , was not as episodic , and i just did not feel as much suspense or emotion as i did with the third movie . \n\n xxmaj it also seems like to me that after reading these surprising reviews that | pos |
3 | xxbos xxup the xxup shop xxup around xxup the xxup corner is one of the sweetest and most feel - good romantic comedies ever made . xxmaj there 's just no getting around that , and it 's hard to actually put one 's feeling for this film into words . xxmaj it 's not one of those films that tries too hard , nor does it come up with the oddest possible scenarios to get the two protagonists together in the end . xxmaj in fact , all its charm is innate , contained within the characters and the setting and the plot … which is highly believable to boot . xxmaj it 's easy to think that such a love story , as beautiful as any other ever told , * could * happen to you … a feeling you do n't often get from other romantic comedies | pos |
4 | xxbos xxmaj the premise of this movie has been tickling my imagination for quite some time now . xxmaj we 've all heard or read about it in some kind of con - text . xxmaj what would you do if you were all alone in the world ? xxmaj what would you do if the entire world suddenly disappeared in front of your eyes ? xxmaj in fact , the last part is actually what happens to xxmaj dave and xxmaj andrew , two room - mates living in a run - down house in the middle of a freeway system . xxmaj andrew is a nervous wreck to say the least and xxmaj dave is considered being one of the biggest losers of society . xxmaj that alone is the main reason to why these two guys get so well along , because they simply only have each | pos |
5 | xxbos xxrep 3 * xxup spoilers xxrep 3 * xxrep 3 * xxup spoilers xxrep 3 * xxmaj continued … \n\n xxmaj from here on in the whole movie collapses in on itself . xxmaj first we meet a rogue program with the indication we 're gon na get ghosts and vampires and werewolves and the like . xxmaj we get a guy with a retarded accent talking endless garbage , two ' ghosts ' that serve no real purpose and have no character what - so - ever and a bunch of henchmen . xxmaj someone 's told me they 're vampires ( straight out of xxmaj blade 2 ) , but they 're so undefined i did n't realise . \n\n xxmaj the funny accented guy with a ridiculous name suffers the same problem as the xxmaj oracle , only for far longer and far far worse . | neg |
6 | xxbos xxmaj i 've rented and watched this movie for the 1st time on xxup dvd without reading any reviews about it . xxmaj so , after 15 minutes of watching xxmaj i 've noticed that something is wrong with this movie ; it 's xxup terrible ! i mean , in the trailers it looked scary and serious ! \n\n i think that xxmaj eli xxmaj roth ( mr . xxmaj director ) thought that if all the characters in this film were stupid , the movie would be funny … ( so stupid , it 's funny … ? xxup wrong ! ) xxmaj he should watch and learn from better horror - comedies such xxunk xxmaj night " , " the xxmaj lost xxmaj boys " and " the xxmaj return xxmaj of the xxmaj living xxmaj dead " ! xxmaj those are funny ! \n\n " | neg |
7 | xxbos xxup myra xxup breckinridge is one of those rare films that established its place in film history immediately . xxmaj praise for the film was absolutely nonexistent , even from the people involved in making it . xxmaj this film was loathed from day one . xxmaj while every now and then one will come across some maverick who will praise the film on philosophical grounds ( aggressive feminism or the courage to tackle the issue of xxunk ) , the film has not developed a cult following like some notorious flops do . xxmaj it 's not hailed as a misunderstood masterpiece like xxup scarface , or trotted out to be ridiculed as a camp classic like xxup showgirls . \n\n xxmaj undoubtedly the reason is that the film , though outrageously awful , is not lovable , or even likable . xxup myra xxup breckinridge is just | neg |
8 | xxbos xxmaj after reading the previous comments , xxmaj i 'm just glad that i was n't the only person left confused , especially by the last 20 minutes . xxmaj john xxmaj carradine is shown twice walking down into a grave and pulling the lid shut after him . i anxiously awaited some kind of explanation for this odd behavior … naturally i assumed he had something to do with the evil goings - on at the house , but since he got killed off by the first rising corpse ( hereafter referred to as xxmaj zombie # 1 ) , these scenes made absolutely no sense . xxmaj please , if someone out there knows why xxmaj carradine kept climbing down into graves -- let the rest of us in on it ! ! \n\n xxmaj all the action is confined to the last 20 minutes so xxmaj | neg |
我们可以看到,库自动处理了所有文本以将其分割成标记,并添加了一些特殊标记,如:
xxbos
表示文本的开始xxmaj
表示下一个单词是大写的
然后,我们可以在一行中定义一个适合文本分类的 Learner
:
= text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy) learn
我们使用了 AWD LSTM 架构,drop_mult
是一个控制模型中所有 dropout 强度的参数,我们使用 accuracy
来跟踪我们的表现。随后,我们可以对预训练模型进行微调:
4, 1e-2) learn.fine_tune(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.587251 | 0.386230 | 0.828960 | 01:35 |
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.307347 | 0.263843 | 0.892800 | 03:03 |
1 | 0.215867 | 0.226208 | 0.911800 | 02:55 |
2 | 0.155399 | 0.231144 | 0.913960 | 03:12 |
3 | 0.129277 | 0.200941 | 0.925920 | 03:01 |
4, 1e-2) learn.fine_tune(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.594912 | 0.407416 | 0.823640 | 01:35 |
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.268259 | 0.316242 | 0.876000 | 03:03 |
1 | 0.184861 | 0.246242 | 0.898080 | 03:10 |
2 | 0.136392 | 0.220086 | 0.918200 | 03:16 |
3 | 0.106423 | 0.191092 | 0.931360 | 03:15 |
不错!要查看我们的模型表现如何,我们可以使用 show_results
方法:
learn.show_results()
text | category | category_ | |
---|---|---|---|
0 | xxbos xxmaj there 's a sign on xxmaj the xxmaj lost xxmaj highway that says : \n\n * major xxup spoilers xxup ahead * \n\n ( but you already knew that , did n't you ? ) \n\n xxmaj since there 's a great deal of people that apparently did not get the point of this movie , xxmaj i 'd like to contribute my interpretation of why the plot makes perfect sense . xxmaj as others have pointed out , one single viewing of this movie is not sufficient . xxmaj if you have the xxup dvd of xxup md , you can " cheat " by looking at xxmaj david xxmaj lynch 's " top 10 xxmaj hints to xxmaj unlocking xxup md " ( but only upon second or third viewing , please . ) ;) \n\n xxmaj first of all , xxmaj mulholland xxmaj drive is | pos | pos |
1 | xxbos ( some spoilers included : ) \n\n xxmaj although , many commentators have called this film surreal , the term fits poorly here . xxmaj to quote from xxmaj encyclopedia xxmaj xxunk 's , surreal means : \n\n " fantastic or incongruous imagery " : xxmaj one need n't explain to the unimaginative how many ways a plucky ten - year - old boy at large and seeking his fortune in the driver 's seat of a red xxmaj mustang could be fantastic : those curious might read xxmaj james xxmaj kincaid ; but if you asked said lad how he were incongruous behind the wheel of a sports car , he 'd surely protest , " no way ! " xxmaj what fantasies and incongruities the film offers mostly appear within the first fifteen minutes . xxmaj thereafter we get more iterations of the same , in an | pos | neg |
2 | xxbos xxmaj hearkening back to those " good xxmaj old xxmaj days " of 1971 , we can vividly recall when we were treated with a whole xxmaj season of xxmaj charles xxmaj chaplin at the xxmaj cinema . xxmaj that 's what the promotional guy called it when we saw him on somebody 's old talk show . ( we ca n't recall just whose it was ; either xxup merv xxup griffin or xxup woody xxup woodbury , one or the other ! ) xxmaj the guest talked about xxmaj sir xxmaj charles ' career and how his films had been out of circulation ever since the 1952 exclusion of the former " little xxmaj tramp ' from xxmaj los xxmaj xxunk xxmaj xxunk on the grounds of his being an " undesirable xxmaj alien " . ( no xxmaj schultz , he 's xxup not from another | pos | pos |
3 | xxbos " buffalo xxmaj bill , xxmaj hero of the xxmaj far xxmaj west " director xxmaj mario xxmaj costa 's unsavory xxmaj spaghetti western " the xxmaj beast " with xxmaj klaus xxmaj kinski could only have been produced in xxmaj europe . xxmaj hollywood would never dared to have made a western about a sexual predator on the prowl as the protagonist of a movie . xxmaj never mind that xxmaj kinski is ideally suited to the role of ' crazy ' xxmaj johnny . xxmaj he plays an individual entirely without sympathy who is ironically dressed from head to toe in a white suit , pants , and hat . xxmaj this low - budget oater has nothing appetizing about it . xxmaj the typically breathtaking xxmaj spanish scenery around xxmaj almeria is nowhere in evidence . xxmaj instead , xxmaj costa and his director of photography | pos | pos |
4 | xxbos xxmaj if you 've seen the trailer for this movie , you pretty much know what to expect , because what you see here is what you get . xxmaj and even if you have n't seen the previews , it wo n't take you long to pick up on what you 're in for-- specifically , a good time and plenty of xxunk from this clever satire of ` reality xxup tv ' shows and ` buddy xxmaj cop ' movies , ` showtime , ' directed by xxmaj tom xxmaj dey , starring xxmaj robert xxmaj de xxmaj niro and xxmaj eddie xxmaj murphy . \n\n\t xxmaj mitch xxmaj preston ( de xxmaj niro ) is a detective with the xxup l.a.p.d . , and he 's good at what he does ; but working a case one night , things suddenly go south when another cop | pos | pos |
5 | xxbos * xxmaj some spoilers * \n\n xxmaj this movie is sometimes subtitled " life xxmaj everlasting . " xxmaj that 's often taken as reference to the final scene , but more accurately describes how dead and buried this once - estimable series is after this sloppy and illogical send - off . \n\n xxmaj there 's a " hey kids , let 's put on a show air " about this telemovie , which can be endearing in spots . xxmaj some fans will feel like insiders as they enjoy picking out all the various cameo appearances . xxmaj co - writer , co - producer xxmaj tom xxmaj fontana and his pals pack the goings - on with friends and favorites from other shows , as well as real xxmaj baltimore personages . \n\n xxmaj that 's on top of the returns of virtually all the members | neg | neg |
6 | xxbos ( caution : several spoilers ) \n\n xxmaj someday , somewhere , there 's going to be a post - apocalyptic movie made that does n't stink . xxmaj unfortunately , xxup the xxup postman is not that movie , though i have to give it credit for trying . \n\n xxmaj kevin xxmaj costner plays somebody credited only as " the xxmaj postman . " xxmaj he 's not actually a postman , just a wanderer with a mule in the wasteland of a western xxmaj america devastated by some unspecified catastrophe . xxmaj he trades with isolated villages by performing xxmaj shakespeare . xxmaj suddenly a pack of bandits called the xxmaj holnists , the self - declared warlords of the xxmaj west , descend upon a village that xxmaj costner 's visiting , and their evil leader xxmaj gen . xxmaj bethlehem ( will xxmaj patton | neg | neg |
7 | xxbos xxmaj in a style reminiscent of the best of xxmaj david xxmaj lean , this romantic love story sweeps across the screen with epic proportions equal to the vast desert regions against which it is set . xxmaj it 's a film which purports that one does not choose love , but rather that it 's love that does the choosing , regardless of who , where or when ; and furthermore , that it 's a matter of the heart often contingent upon prevailing conditions and circumstances . xxmaj and thus is the situation in ` the xxmaj english xxmaj patient , ' directed by xxmaj anthony xxmaj minghella , the story of two people who discover passion and true love in the most inopportune of places and times , proving that when it is predestined , love will find a way . \n\n xxmaj it 's xxup | pos | pos |
8 | xxbos xxmaj no one is going to mistake xxup the xxup squall for a good movie , but it sure is a memorable one . xxmaj once you 've taken in xxmaj myrna xxmaj loy 's performance as xxmaj nubi the hot - blooded gypsy girl you 're not likely to forget the experience . xxmaj when this film was made the exotically beautiful xxmaj miss xxmaj loy was still being cast as foreign vixens , often xxmaj asian and usually sinister . xxmaj she 's certainly an eyeful here . xxmaj it appears that her skin was darkened and her hair was curled . xxmaj in most scenes she 's barefoot and wearing little more than a skirt and a loose - fitting peasant blouse , while in one scene she wears nothing but a patterned towel . i suppose xxmaj i 'm focusing on xxmaj miss xxmaj loy | neg | neg |
我们可以很容易地对新的文本进行预测:
"I really liked that movie!") learn.predict(
('pos', tensor(1), tensor([0.0092, 0.9908]))
在这里我们可以看到模型将该评论视为正面。结果的第二部分是“pos”在我们数据词汇中的索引,最后一部分是分配给每个类别的概率(“pos”是99.1%,而“neg”是0.9%)。
现在轮到你了!写一篇你自己的迷你电影评论,或者从互联网上复制一篇,我们可以看看这个模型对此有什么看法。
使用数据块 API
我们还可以使用数据块 API 将数据放入 DataLoaders
中。这有点高级,如果您对学习新的 API 还不太舒服,可以随意跳过这一部分。
数据块是通过向 fastai 库提供一堆信息来构建的:
- 通过一个名为
blocks
的参数来指定使用的类型:这里我们有文本和类别,因此我们传递TextBlock
和CategoryBlock
。为了告知库我们的文本是文件夹中的文件,我们使用from_folder
类方法。 - 如何获取原始项目,这里是我们的函数
get_text_files
。 - 如何对这些项目进行标记,这里使用父文件夹。
- 如何拆分这些项目,这里使用祖父文件夹。
= DataBlock(blocks=(TextBlock.from_folder(path), CategoryBlock),
imdb =get_text_files,
get_items=parent_label,
get_y=GrandparentSplitter(valid_name='test')) splitter
这只是提供了一个关于如何组装数据的蓝图。要实际创建数据,我们需要使用 dataloaders
方法:
= imdb.dataloaders(path) dls
ULMFiT 方法
我们在前一节中使用的预训练模型称为语言模型。它是在维基百科上进行预训练的,任务是猜测下一个单词,在阅读之前的所有单词后。我们通过直接微调这个语言模型到电影评论分类器上取得了很好的结果,但通过一个额外的步骤,我们可以做得更好:维基百科的英语与IMDb的英语略有不同。因此,我们可以先对预训练的语言模型进行微调,使其适应IMDb语料库,然后再将其作为分类器的基础。
当然,一个原因是理解您使用的模型基础是有帮助的。但还有另一个非常实际的原因,那就是如果您在微调分类模型之前先微调(基于序列的)语言模型,您将获得更好的结果。例如,在IMDb情感分析任务中,数据集包括50,000条附有负面标签的电影评论,这些评论在未标记文件夹中没有任何正面或负面标签。我们可以使用所有这些评论来微调预训练的语言模型——这将导致一个特别擅长预测电影评论下一个单词的语言模型。相比之下,预训练模型仅在维基百科文章上训练。
整个过程由这张图片概括:
在IMDb上微调语言模型
我们可以很容易地获取适合语言模型的 DataLoaders
中的文本:
= TextDataLoaders.from_folder(path, is_lm=True, valid_pct=0.1) dls_lm
我们需要为 valid_pct
传递一些值,否则此方法将尝试使用祖父文件夹名称来拆分数据。通过传递 valid_pct=0.1
,我们告诉它从那些评论中随机选取10%作为验证集。
我们可以使用 show_batch
来查看我们的数据。在这里,任务是猜测下一个单词,因此我们可以看到目标都向右移动了一个单词。
=5) dls_lm.show_batch(max_n
text | text_ | |
---|---|---|
0 | xxbos xxmaj about thirty minutes into the film , i thought this was one of the weakest " xxunk ever because it had the usual beginning ( a murder happening , then xxmaj columbo coming , inspecting everything and interrogating the main suspect ) squared ! xxmaj it was boring because i thought i knew everything already . \n\n xxmaj but then there was a surprising twist that turned this episode into | xxmaj about thirty minutes into the film , i thought this was one of the weakest " xxunk ever because it had the usual beginning ( a murder happening , then xxmaj columbo coming , inspecting everything and interrogating the main suspect ) squared ! xxmaj it was boring because i thought i knew everything already . \n\n xxmaj but then there was a surprising twist that turned this episode into a |
1 | yeon . xxmaj these two girls were magical on the screen . i will certainly be looking into their other films . xxmaj xxunk xxmaj jeong - ah is xxunk cheerful and hauntingly evil as the stepmother . xxmaj finally , xxmaj xxunk - su xxmaj kim gives an excellent performance as the weary , broken father . \n\n i truly love this film . xxmaj if you have yet to see | . xxmaj these two girls were magical on the screen . i will certainly be looking into their other films . xxmaj xxunk xxmaj jeong - ah is xxunk cheerful and hauntingly evil as the stepmother . xxmaj finally , xxmaj xxunk - su xxmaj kim gives an excellent performance as the weary , broken father . \n\n i truly love this film . xxmaj if you have yet to see ' |
2 | tends to be tedious whenever there are n't any hideous monsters on display . xxmaj luckily the gutsy killings and eerie set designs ( by no less than xxmaj bill xxmaj paxton ! ) compensate for a lot ! a nine - headed expedition is send ( at hyper speed ) to the unexplored regions of space to find out what happened to a previously vanished spaceship and its crew . xxmaj | to be tedious whenever there are n't any hideous monsters on display . xxmaj luckily the gutsy killings and eerie set designs ( by no less than xxmaj bill xxmaj paxton ! ) compensate for a lot ! a nine - headed expedition is send ( at hyper speed ) to the unexplored regions of space to find out what happened to a previously vanished spaceship and its crew . xxmaj bad |
3 | movie just sort of meanders around and nothing happens ( i do n't mean in terms of plot - no plot is fine , but no action ? xxmaj come on . ) xxmaj in hindsight , i should have expected this - after all , how much can really happen between 4 teens and a bear ? xxmaj so although special effects , acting , etc are more or less on | just sort of meanders around and nothing happens ( i do n't mean in terms of plot - no plot is fine , but no action ? xxmaj come on . ) xxmaj in hindsight , i should have expected this - after all , how much can really happen between 4 teens and a bear ? xxmaj so although special effects , acting , etc are more or less on par |
4 | greetings again from the darkness . xxmaj writer / xxmaj director ( and xxmaj wes xxmaj anderson collaborator ) xxmaj noah xxmaj baumbach presents a semi - autobiographical therapy session where he unleashes the anguish and turmoil that has carried over from his childhood . xxmaj the result is an amazing insight into what many people go through in a desperate attempt to try and make their family work . \n\n xxmaj | again from the darkness . xxmaj writer / xxmaj director ( and xxmaj wes xxmaj anderson collaborator ) xxmaj noah xxmaj baumbach presents a semi - autobiographical therapy session where he unleashes the anguish and turmoil that has carried over from his childhood . xxmaj the result is an amazing insight into what many people go through in a desperate attempt to try and make their family work . \n\n xxmaj the |
接下来,我们有一个便捷的方法可以直接从中获取Learner
,使用像之前一样的AWD_LSTM
架构。我们使用准确率和困惑度作为指标(后者是损失的指数),并设置默认的权重衰减为0.1。to_fp16
将Learner
置于混合精度,这将有助于加快在具有张量核心的GPU上的训练速度。
= language_model_learner(dls_lm, AWD_LSTM, metrics=[accuracy, Perplexity()], path=path, wd=0.1).to_fp16() learn
默认情况下,预训练的 Learner
处于冻结状态,这意味着只有模型的头部会训练,而主体保持冻结。我们在这里展示 fine_tune
方法的背后原理,并使用 fit_one_cycle
方法来训练模型:
1, 1e-2) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | perplexity | time |
---|---|---|---|---|---|
0 | 4.120048 | 3.912788 | 0.299565 | 50.038246 | 11:39 |
这个模型需要一些时间来训练,因此这是一个讨论保存中间结果的好机会。
您可以像这样轻松保存模型的状态:
'1epoch') learn.save(
它将在 learn.path/models/
中创建一个名为 “1epoch.pth” 的文件。如果您希望在创建 Learner
后在另一台机器上加载您的模型,或者稍后恢复训练,可以使用以下命令加载该文件的内容:
= learn.load('1epoch') learn
我们可以在解冻后对模型进行微调:
learn.unfreeze()10, 1e-3) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | perplexity | time |
---|---|---|---|---|---|
0 | 3.893486 | 3.772820 | 0.317104 | 43.502548 | 12:37 |
1 | 3.820479 | 3.717197 | 0.323790 | 41.148880 | 12:30 |
2 | 3.735622 | 3.659760 | 0.330321 | 38.851997 | 12:09 |
3 | 3.677086 | 3.624794 | 0.333960 | 37.516987 | 12:12 |
4 | 3.636646 | 3.601300 | 0.337017 | 36.645859 | 12:05 |
5 | 3.553636 | 3.584241 | 0.339355 | 36.026001 | 12:04 |
6 | 3.507634 | 3.571892 | 0.341353 | 35.583862 | 12:08 |
7 | 3.444101 | 3.565988 | 0.342194 | 35.374371 | 12:08 |
8 | 3.398597 | 3.566283 | 0.342647 | 35.384815 | 12:11 |
9 | 3.375563 | 3.568166 | 0.342528 | 35.451500 | 12:05 |
完成后,我们保存我们所有的模型,除了将激活转换为我们词汇中每个标记概率的最终层。不包括最终层的模型称为编码器。我们可以通过save_encoder
保存它:
'finetuned') learn.save_encoder(
行话:编码器:不包括任务特定最终层的模型。当应用于视觉CNN时,其含义与主体相同,但在自然语言处理和生成模型中使用得更多。
在使用此模型对评论进行微调之前,我们可以利用我们的模型生成随机评论:由于它是经过训练来猜测句子中下一个单词,因此我们可以用它来撰写新的评论:
= "I liked this movie because"
TEXT = 40
N_WORDS = 2
N_SENTENCES = [learn.predict(TEXT, N_WORDS, temperature=0.75)
preds for _ in range(N_SENTENCES)]
print("\n".join(preds))
i liked this movie because of its story and characters . The story line was very strong , very good for a sci - fi film . The main character , Alucard , was very well developed and brought the whole story
i liked this movie because i like the idea of the premise of the movie , the ( very ) convenient virus ( which , when you have to kill a few people , the " evil " machine has to be used to protect
训练文本分类器
我们可以像以前一样几乎完全一样地收集我们的文本分类数据:
= TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test', text_vocab=dls_lm.vocab) dls_clas
主要区别在于我们必须使用与微调语言模型时完全相同的词汇,否则学习到的权重将没有任何意义。我们通过 text_vocab
传递该词汇。
然后我们可以像之前一样定义我们的文本分类器:
= text_classifier_learner(dls_clas, AWD_LSTM, drop_mult=0.5, metrics=accuracy) learn
不同之处在于我们在训练之前加载了之前的编码器:
= learn.load_encoder('finetuned') learn
最后一步是使用辨别学习率和逐渐解冻进行训练。在计算机视觉中,我们通常会一次性解冻模型,但对于NLP分类器,我们发现一次解冻几个层会产生实际的差异。
1, 2e-2) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.347427 | 0.184480 | 0.929320 | 00:33 |
在仅仅一个训练周期内,我们得到了与第一部分训练相同的结果,效果还不错!我们可以将 -2
传递给 freeze_to
,以冻结除最后两个参数组之外的所有参数组:
-2)
learn.freeze_to(1, slice(1e-2/(2.6**4),1e-2)) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.247763 | 0.171683 | 0.934640 | 00:37 |
然后我们可以再解冻一些,并继续训练:
-3)
learn.freeze_to(1, slice(5e-3/(2.6**4),5e-3)) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.193377 | 0.156696 | 0.941200 | 00:45 |
最后,整个模型!
learn.unfreeze()2, slice(1e-3/(2.6**4),1e-3)) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.172888 | 0.153770 | 0.943120 | 01:01 |
1 | 0.161492 | 0.155567 | 0.942640 | 00:57 |